`

Servlet中文乱码问题的解决方案

阅读更多

1、post方式中文乱码问题的解决

request.setCharacterEncoding("UTF-8");

 

2、get方式中文乱码问题的解决
(1)方式一:在Tomcat\conf\server.xml中配置编码URIEncoding="UTF-8"

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

 

(2)方式二:将请求得到的参数重新构造成一个字符串进行解码

String username = request.getParameter("username");
String uname = new String(username.getBytes("ISO8859-1"),"UTF-8");

 

(3)方式三:将请求得到的参数通过URLEncoder进行解码

String username = request.getParameter("username");
String uname = URLDecoder.decode(username, "UTF-8");

 

3、返回给浏览器的中文乱码问题的解决

response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics