jsp 编码问题(3)
<html>
<head>
<basehref="<%=basePath%>">
<title>My JSP 'index.jsp' startingpage</title>
<meta http-equiv="pragma"content="no-cache">
<meta http-equiv="cache-control"content="no-cache">
<meta http-equiv="expires"content="0">
<meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is mypage">
<!--
<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");
这两句话,才能正常显示中文!
作者:mark_qi
相关新闻>>
- 发表评论
-
- 最新评论 更多>>