Java web jsp中關於request , get / post 中文字符 亂碼的問題解決方式

首先,我們新建一個項目。然後在WebContent中新建jsp文件命名爲regist.jsp 這個界面爲用戶提供輸入信息的功能,代碼如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 註冊界面 -->
	<h1>註冊新用戶</h1>
	<!-- action 提交請求的地址 method 處理請求的方式 -->
	<form action="userService.jsp"method="get">
		
		<table>
			<tr>
				<td>用戶名</td>
				<td>
				<input type="text"name="userName"/>
				</td>
			</tr>
			<tr>
				<td>密碼</td>
				<td>
				<input type="password"name="userPwd"/>
				</td>
			</tr>
			<tr>
				<td>愛好</td>
				<td>
				<input type="checkbox"name="hobby"value="1"/>看書
				<input type="checkbox"name="hobby"value="2"/>打球
				<input type="checkbox"name="hobby"value="3"/>健身
				</td>
			</tr>
			<tr>
				<td>
				<input type="submit"/>
				</td>
			</tr>
		</table>
	
	</form>
</body>
</html>

效果如下圖:
在這裏插入圖片描述
在上面的代碼中,我們看到我們將數據提交到了userService.jsp
處理請求的方式被設定爲get

	<form action="userService.jsp"method="get">

我們同路徑新建jsp文件,名字叫userService.jsp 代碼如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 處理註冊的jsp文件 -->
<%
	String userName = request.getParameter("userName");
	String userPwd = request.getParameter("userPwd");
	
	out.print("用戶名:" + userName);
	out.print("密碼:" + userPwd);
%>
</body>
</html>

我們用userName接收了用戶提交的用戶名,userPwd接受用戶提交的密碼,然後就是簡單的打印出來,第一次輸入用戶名我們用英文來測試:

密碼爲
運行結果如圖:
在這裏插入圖片描述
沒有任何問題,第二次我們用中文用戶名測試:
在這裏插入圖片描述
點擊提交:
在這裏插入圖片描述
發現用戶名亂碼,接下來是解決方案:

方案一(手動解決):
userName = new String(userName.getBytes("ISO-8859-1") , "UTF-8");

將userService.jsp中的userName進行處理用getBytes方法括號裏面爲原來的編碼類型 , 後面爲目標編碼類型 ,修改後代碼如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 處理註冊的jsp文件 -->
<%
	String userName = request.getParameter("userName");
	//手動處理中文字符的問題
	userName = new String(userName.getBytes("ISO-8859-1") , "UTF-8");
	String userPwd = request.getParameter("userPwd");
	
	out.print("用戶名:" + userName);
	out.print("密碼:" + userPwd);
%>
</body>
</html>

運行結果:
在這裏插入圖片描述
成功解決!但是這種方法一次只能處理一條數據,要想讓他每次都自動處理要怎麼做呢?請看方案二:

方案二(配置tomcat文件):
	找到tomcat安裝路徑,打開其中的 conf 文件夾,編輯其中的名字叫做 server.xml 的文件找到第70行左右的叫做Connector的內容進行修改:

在這裏插入圖片描述

配置請求的字符集,加入此句話:
URIEncoding="UTF-8"

在這裏插入圖片描述
保存,重新部署服務器,啓動項目,提交後即可解決。

若用post方式,則應該設置請求字符集:
request.setCharacterEncoding("UTF-8");

如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 處理註冊的jsp文件 -->
<%
	//設置請求的字符集
	request.setCharacterEncoding("UTF-8");
	String userName = request.getParameter("userName");

	String userPwd = request.getParameter("userPwd");
	
	out.print("用戶名:" + userName);
	out.print("密碼:" + userPwd);
%>
</body>
</html>

以上便是解決方法。如有錯誤,歡迎指出,感謝閱讀!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章