日期:2014-05-20  浏览次数:20823 次

tomcate中servlet显示中文乱码,如何解决?
public class MyServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html; charset=gbk");
response.setCharacterEncoding("gbk");

out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r\n");
out.write("<html>");
out.write("<head>");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=gbk\"/>");
out.write("</head>");
out.write("<body>");
out.write("中华人民共和国");
out.write("</body>");
out.write("</html>");
}

------解决方案--------------------
把charset换成gb2312看看,
------解决方案--------------------
String s="中华人民共和国"; 
byte[] tmpbyte=s.getBytes("ISO8859_1"); 
s=new String(tmpbyte); 
out.write(s); 
试下看看吧


------解决方案--------------------
String str="中华人民共和国";
try {
str = new String(str.getBytes("ISO-8859-1"),"GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
out.write(str);
试一下好使吗
------解决方案--------------------
不要在Servlet中直接输出页面,这样做很不规范,应该建一个JSP页面,把参数传进去。