Tomcat:URL中文编码设置:修订间差异

来自WHY42
imported>Soleverlee
无编辑摘要
imported>Soleverlee
第7行: 第7行:
               URIEncoding="UTF-8" />
               URIEncoding="UTF-8" />
</pre>
</pre>
=测试TOMCAT编码=
来写个小程序测试下:
<source lang="java">
public class HelloServlet extends HttpServlet{
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response){
        System.out.println("=>GET");
        System.out.println(request.getRequestURI());
        this.printParam(request);
    }
   
    void printParam(HttpServletRequest request){
        Map<String, String[]> params = request.getParameterMap();
        for(String k:params.keySet()){
            System.out.print("\t?" + k + "=");
            for(String v:params.get(k))
                System.out.println("(" + v + ")");
        }
    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response){
        System.out.println("=>POST");
        this.printParam(request);
    }
}
</source>
web.xml中配置:
<source lang="java">
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>com.riguz.tc.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</source>
=GET乱码=
=GET乱码=
<source lang="java">
</source>
<source lang="java">
</source>
<source lang="java">
</source>
=POST乱码=
=POST乱码=
[[Category:Linux/Unix]]
[[Category:Linux/Unix]]

2016年3月21日 (一) 16:05的版本

tomcat中,如果不设置URL编码,URL中带有中文字符时会导致乱码。解决方法是对URL进行编码,并在tomcat中进行设置。 server.xml

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

测试TOMCAT编码

来写个小程序测试下:

public class HelloServlet extends HttpServlet{
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response){
        System.out.println("=>GET");
        System.out.println(request.getRequestURI());
        this.printParam(request);
    }
    
    void printParam(HttpServletRequest request){
        Map<String, String[]> params = request.getParameterMap();
        for(String k:params.keySet()){
            System.out.print("\t?" + k + "=");
            for(String v:params.get(k))
                System.out.println("(" + v + ")");
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response){
        System.out.println("=>POST");
        this.printParam(request);
    }
}

web.xml中配置:

<servlet>
	<servlet-name>HelloWorldServlet</servlet-name>
	<servlet-class>com.riguz.tc.HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>HelloWorldServlet</servlet-name>
	<url-pattern>/*</url-pattern>
</servlet-mapping>

GET乱码

POST乱码