Servlet中文乱码问题解决办法
首先对于源jsp网站和servlet里面的字符集要一样,一般支持中文的字符集为UTF-8最好采用这个字符集(除此之外还有gb2312);
对于源jsp文件的代码中需要设置
设置你的page里面的字符集
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
设置html文件里面的字符集
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
如果对于参数通过get方法进行传参的话,有两种方法:
方法一:
在tomcat里面设置字符集为UTF-8
修改server.xml
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8"/>
* 必须有修改tomcat服务器配置文件权限
方法二:通过对servlet里面的进行设置:
response.setHeader("Content-Type", "text/html;charset=UTF-8");//设置UTF-8的显示页面的类型和字符集 username = new String(username.getBytes("ISO8859-1"),"utf-8");//根据你的tomcat里面的字符集进行对中文进行转换,将iso转换为UTF-8
如果对于参数通过post方法进行传参的话,则设置为:
response.setHeader("Content-Type", "text/html;charset=UTF-8");//设置UTF-8的显示页面的类型和字符集 request.setCharacterEncoding("utf-8"); //设置通过post方法进行传参的字符集
在这里需要注意的是:
1.对于post和get传参的时候解决中文乱码问题的时候一定要分清楚是哪种方式传参,是post就得用post不然会没用的
2.对于两种不同的地方不能合用,乱码问题解决不了的。
下面附上一个例子:
get.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="servlet/test" method="get"> <input type="text" name="getV"> <button type="submit">测试get请求</button> </form> </body> </body> </html>
post.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="servlet/test" method="post"> <input type="text" name="postV"> <button type="submit">测试post请求</button> </form> </body> </html>
test.java
package testcharset; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Content-Type", "text/html;charset=UTF-8");//设置UTF-8的显示页面的类型和字符集 PrintWriter out=response.getWriter(); try { String username=request.getParameter("getV"); username = new String(username.getBytes("ISO8859-1"),"utf-8");//根据你的tomcat里面的字符集进行对中文进行转换,将iso转换为UTF-8 out.print("获取的get请求:"); out.print(username); } catch (Exception e) { e.printStackTrace(); // TODO: handle exception }finally{ try { out.close(); } catch (Exception e2) { e2.printStackTrace(); // TODO: handle exception } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Content-Type", "text/html;charset=UTF-8");//设置UTF-8的显示页面的类型和字符集 request.setCharacterEncoding("utf-8"); //设置通过post方法进行传参的字符集 PrintWriter out=response.getWriter(); try { out.print("获取的Post请求:"); out.print(request.getParameter("postV")); } catch (Exception e) { e.printStackTrace(); // TODO: handle exception }finally{ try { out.close(); } catch (Exception e2) { e2.printStackTrace(); // TODO: handle exception } } } }