jsp 编码问题
2012-07-13 11:34:50 来源:WEB开发网<!--
<link rel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
<form action=servlet/Loginmethod=post>
用户名:<input type=text name="username"size=20><br>
密码:<input type="password" name="password"size=20><br>
<input type=submitname="submit">
</form>
</body>
</html>
-------------------------------------------------------
在Login.java Servlet源文件中:
public void doPost(HttpServletRequest request, HttpServletResponseresponse)
throws ServletException,IOException {
response.setContentType("text/html;charset=utf-8");
//response.setContentType("text/html;charset=GBK");//也可以正常显示中文
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.print("<H>welcome you comehere!</H>");
out.print("<br>");
out.println(request.getParameter("username"));
out.print("<br>");
out.println(request.getParameter("password"));
out.print("<br>");
out.flush();
System.out.println(response.getCharacterEncoding());
out.close();
}
因为在index.jsp中
<%@ pagecontentType="text/html;charset=utf-8"%>有这句话,所以JSP默认使用的pageEncoding=utf-8
所以在Login Servlet你必须加上:
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
这两句话,才能正常显示中文!
好了,关于Tomcat中文乱码问题就总结到这里!总之,出了问题只要明白Tomcat的工作原理,并从以上三个注意点出发分析,总能找到问题的答案。
更多精彩
赞助商链接