JSP开发中tomcat访问URL带有中文名称的HTML文件显示404错误

出现的情况

在JSP开发中,可能会出现tomcat访问带有中文名称的HTML文件显示404错误的情况,如下图所示:在这里插入图片描述

出现这种状况的原因:

出现这种状况的原因是tomcat将URL中的中文字符经过Base64解析

解决思路:

打开Eclipse,点击Servers,找到Server.xml,打开这个文件
在这里插入图片描述
找到如下代码:

 <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define an SSL HTTP/1.1 Connector on port 8443
         This connector uses the BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

在如下代码中

<Connector connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8443" />

插入以下代码:

useBodyEncodingForURI="true" URIEncoding="UTF-8"

结果如图所示:

<Connector connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true" URIEncoding="UTF-8"/>

注意:redirectPort=“8443” 与useBodyEncodingForURI=“true” URIEncoding=”UTF-8″之间有个空格
保存该文件,重新启动tomcat,再次打开带有中文的URL时即可正确访问


版权声明:本文为qq_42092076原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。