使用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可以防止获取表单数据为中文时乱码


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM