码迷,mamicode.com
首页 > 其他好文 > 详细

不同Tomcat版本对get、post请求,中文乱码问题

时间:2015-05-15 19:56:33      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

Tomcat8

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>public class dd extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		 Tomcat8
		resp.setContentType("text/html;UTF-8");
		req.setCharacterEncoding("UTF-8");
		System.out.println(req.getParameter("name"));
		
	}
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		 System.out.println(req.getParameter("name")); //不用进行ISO转码

	}
}</strong></span>


Tomcat8.x 以前版本

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>public class ee extends HttpServlet {



	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		System.out.println(request.getParameter("name")); //??????
		
		//需要进行 ISO 解码 ,utf 编码
		String name = request.getParameter("name");
		System.out.println(new String(name.getBytes("ISO-8859-1"),"UTF-8"));//你好
		
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		System.out.println(request.getParameter("name"));	// post请求   
	}
}</strong></span>



测试:jsp

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong><body>


    <a href="${pageContext.request.contextPath}/dd?name=你好">GET</a>
   
    <form action="${pageContext.request.contextPath}/dd" method="post">
    <span style="white-space:pre">	</span><input type="text" name="name" />
    <span style="white-space:pre">	</span><input type="submit" value="提交post" /> 
    </form>
   
    <form action="${pageContext.request.contextPath}/dd" method="get">
    <span style="white-space:pre">	</span><input type="text" name="name" />
    <span style="white-space:pre">	</span><input type="submit" value="提交get"/> 
    </form>
  </body>
</strong></span>



也可以使用直接修改配置文件的办法:

再Tomcat根目录下的config.xml文件,找到<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

再其中添加:URIEncoding="UTF-8" 和 useBodyEncodingForURI=“true”

useBodyEncodingForURI (默认false):参数表示是否用 与 request.setCharacterEncoding一致的编码 
如果是true,参数对URL提交的数据和表单中GET方式提交的数据进行重新编码。

URIEncoding (默认ISO8859-1)参数指定对所有GET方式请求进行统一的重新编码(解码)的编码。


再Tomcat8中,关于URIEncoding的描述做出了更改:http://localhost:8080/docs/config/http.html

URIEncoding

This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, UTF-8 will be used unless the org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property is set to true in which case ISO-8859-1 will be used.



PS:极其不建议在Tomcat的配置文件上做编码的修改,因为你不知道用户用的什么版本或根本就不是Tomcat,总不能和用户说:你用tomcat 吧,再顺便那个配置文件改改


不同Tomcat版本对get、post请求,中文乱码问题

标签:

原文地址:http://blog.csdn.net/wjw0130/article/details/45748549

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!