溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Javaweb接收表單數(shù)據(jù)并處理中文亂碼的方法

發(fā)布時(shí)間:2020-07-23 09:16:23 來(lái)源:億速云 閱讀:292 作者:小豬 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了Javaweb接收表單數(shù)據(jù)并處理中文亂碼的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

前端表單數(shù)據(jù)

常見(jiàn)的表單項(xiàng)的傳值,如:

  • 普通input
  • 單選radio
  • 多選checkbox
  • select下拉選擇
  • textarea文本域
     

普通 input : name屬性值為后臺(tái)接收時(shí)的參數(shù)值。

用戶(hù)名:<input type="text" name="username"><br>
密碼:<input type="password" name="password"><br>

單選 radio :?jiǎn)芜x按鈕的 name 值相同才能實(shí)現(xiàn)只能點(diǎn)擊一個(gè)。

性別:

<input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女

多選checkbox :name值相同。

愛(ài)好:

<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)通過(guò)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文本域可通過(guò)此方法獲取。

String[] hobbies = request.getParameterValues("hobby");

多選checkbox可通過(guò)此方法獲取。

中文亂碼處理

GET方式提交的數(shù)據(jù)

先通過(guò) 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ù)組中。

解釋?zhuān)和ㄟ^(guò)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ù)器端向客戶(hù)端發(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ù)器端需要向客戶(hù)端發(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ì)Javaweb接收表單數(shù)據(jù)并處理中文亂碼的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI