使用jsp内置对象request获取表单提交中文内容乱码的解决办法

page1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>page1</title>
</head>
<body>
<form action="page1_1.jsp" method="post">
<input type="text" name="username" />
<input type="password" name="userpwd" />
<input type="submit" value="提交" />
</form>
</body>
</html>

page1_1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>page1_1</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String userpwd = request.getParameter("userpwd");
out.println("username:" + username + "<br/>");
out.println("userpwd:" + userpwd + "<br/>");
%>
</body>
</html>

从page1.jsp页面提交表单到page1_1.jsp页面,注意看page1_1.jsp标红的内容,设置request编码方式为utf-8可以防止获取表单数据为中文时乱码

posted @ 2018-09-25 16:06  魔豆  阅读(1307)  评论(1编辑  收藏  举报