使用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