Javaweb接收表單數(shù)據(jù)并處理中文亂碼
前端表單數(shù)據(jù)
常見的表單項(xiàng)的傳值,如:
- 普通input
- 單選radio
- 多選checkbox
- select下拉選擇
- textarea文本域
普通 input : name屬性值為后臺(tái)接收時(shí)的參數(shù)值。
用戶名:<input type="text" name="username"><br>
密碼:<input type="password" name="password"><br>
單選 radio :單選按鈕的 name 值相同才能實(shí)現(xiàn)只能點(diǎn)擊一個(gè)。
性別:
<input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女
多選checkbox :name值相同。
愛好:
<input type="checkbox" name="hobby" value="唱">唱
<input type="checkbox" name="hobby" value="跳舞">跳舞
<input type="checkbox" name="hobby" value="rap">rap
<input type="checkbox" name="hobby" value="籃球">籃球
select下拉選擇 :后臺(tái)通過degree作為參數(shù),獲取選中的那個(gè)option的value值。
下拉選擇:
<select name="degree">
<option value="">---請(qǐng)選擇---</option>
<option value="大一">大一</option>
<option value="大二">大二</option>
<option value="大三">大三</option>
<option value="大四">大四</option>
</select>
textarea文本域 :rows定義顯示的行數(shù),cols定義的是顯示的列數(shù)。
文本域:<br><textarea name="other" rows="10" cols="30"></textarea><br>
后臺(tái)接收數(shù)據(jù)
接收表單數(shù)據(jù):
String 表單name= request.getParameter(表單name);
普通input、單選radio、select下拉選擇、textarea文本域可通過此方法獲取。
String[] hobbies = request.getParameterValues("hobby");
多選checkbox可通過此方法獲取。
中文亂碼處理
GET方式提交的數(shù)據(jù)
先通過 String username = request.getParameter(username) 獲得該表單的值,此時(shí)是亂碼的。
使用String new_username = new String(username.getBytes("iso8859-1"), "utf-8") 進(jìn)行編碼轉(zhuǎn)換
相關(guān)APi :
String(byte[] bytes, Charset charset) 構(gòu)造一個(gè)新的String,由指定的字節(jié)的數(shù)組轉(zhuǎn)化為指定編碼的字節(jié)數(shù)組。
getBytes(Charset charset)使用指定的編碼方式將該String編碼為字節(jié)序列,將結(jié)果存儲(chǔ)到新的字節(jié)數(shù)組中。
解釋:通過get方式提交的數(shù)據(jù)的編碼方式為iso8859-1, 先獲取該編碼方式的字節(jié)數(shù)組,再將該字節(jié)數(shù)組轉(zhuǎn)化為utf-8編碼的字節(jié)數(shù)組,然后將該字節(jié)數(shù)組轉(zhuǎn)換為字符串。
POST方式提交的數(shù)據(jù)
request.setCharacterEncoding("utf-8");
服務(wù)器端向客戶端發(fā)送的數(shù)據(jù)
response.setContentType("text/html;charset=utf-8");
以下是全部代碼:
GET提交方式:
@WebServlet(name = "RegisterServlet",urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get提交方式處理中文亂碼
String username = request.getParameter("username");
String new_username = new String(username.getBytes("iso8859-1"), "utf-8");
String password = request.getParameter("password");
String new_password = new String(password.getBytes("iso8859-1"), "utf-8");
String gender = request.getParameter("gender");
String new_gender = new String(gender.getBytes("iso8859-1"), "utf-8");
String[] hobbies = request.getParameterValues("hobby");
for (int i = 0; i < hobbies.length; i++) {
hobbies[i]=new String(hobbies[i].getBytes("iso8859-1"), "utf-8");
}
String degree = request.getParameter("degree");
String new_password = new String(password.getBytes("iso8859-1"), "utf-8");
String other = request.getParameter("other");
String new_password = new String(password.getBytes("iso8859-1"), "utf-8");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
POST提交方式:
@WebServlet(name = "RegisterServlet",urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post提交方式的中文亂碼解決方法
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String gender = request.getParameter("gender");
String[] hobbies = request.getParameterValues("hobby");
String degree = request.getParameter("degree");
String other = request.getParameter("other");
//如果服務(wù)器端需要向客戶端發(fā)送的數(shù)據(jù)
response.setContentType("text/html;charset=utf-8");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Javaweb中Request獲取表單數(shù)據(jù)的四種方法詳解
- JavaWeb實(shí)現(xiàn)表單提交的示例詳解
- 解決Javaweb 提交表單到servlet時(shí)出現(xiàn)空白頁面,但網(wǎng)站不報(bào)錯(cuò)問題
- Java后臺(tái)防止客戶端重復(fù)請(qǐng)求、提交表單實(shí)現(xiàn)原理
- JavaWeb表單注冊(cè)界面的實(shí)現(xiàn)方法
- JavaWeb表單及時(shí)驗(yàn)證功能在輸入后立即驗(yàn)證(含用戶類型,性別,愛好...的驗(yàn)證)
- Javaweb獲取表單數(shù)據(jù)的多種方式
- Java后臺(tái)開發(fā)之表單提交之前驗(yàn)證
- java后臺(tái)防止表單重復(fù)提交方法詳解
相關(guān)文章
騰訊云部署javaWeb項(xiàng)目的實(shí)現(xiàn)步驟
本文主要介紹了騰訊云部署javaWeb項(xiàng)目的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
java項(xiàng)目中讀取jdbc.properties文件操作
這篇文章主要介紹了java項(xiàng)目中讀取jdbc.properties文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
SpringCloud之Config配置中心與Redis分布式鎖詳解
這篇文章主要給大家介紹了SpringCloud Alibaba中Config配置中心,Redis分布式鎖,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考閱讀2023-05-05
SpringAOP中基于注解實(shí)現(xiàn)通用日志打印方法詳解
這篇文章主要介紹了SpringAOP中基于注解實(shí)現(xiàn)通用日志打印方法詳解,在日常開發(fā)中,項(xiàng)目里日志是必不可少的,一般有業(yè)務(wù)日志,數(shù)據(jù)庫日志,異常日志等,主要用于幫助程序猿后期排查一些生產(chǎn)中的bug,需要的朋友可以參考下2023-12-12
Mybatis generator修改Mapper.java文件實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Mybatis generator修改Mapper.java文件實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Java數(shù)據(jù)結(jié)構(gòu)之有向圖設(shè)計(jì)與實(shí)現(xiàn)詳解
有向圖是具有方向性的圖,由一組頂點(diǎn)和一組有方向的邊組成,每條方向的邊都連著一對(duì)有序的頂點(diǎn)。本文為大家介紹的是有向圖的設(shè)計(jì)與實(shí)現(xiàn),需要的可以參考一下2022-11-11
SpringBoot?AOP中JoinPoint的使用方式和通知切點(diǎn)表達(dá)式
這篇文章主要介紹了SpringBoot?AOP中JoinPoint的使用方式和通知切點(diǎn)表達(dá)式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05

