Java Web中前端提交的表單兩種方式(處理中文亂碼問題)


在前端HTML、JSP中,一般都是使用form表單,然后在表單中設置用戶名信息框,再設置一個按鈕,並把這個按鈕設置為submit類型。形如:

<form action="/hello/LoginServlet" method="post"> 用戶名:<input type="text" name="username"><br> 密碼:<input type="password" name="password"><br> <!--注意性別只能選擇一個,所以下面兩個標簽名字都是gender--> 性別:<input type="radio" name="gender" value="男">男 <input type="radio" name="gender" value="女">女<br> <!--設置一個提交按鈕--> <input type="submit" value="提交"> </form> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在這里插入圖片描述
當我們點擊提交按鈕時,html文件把用戶提交的信息提交到指定的Servlet。

一般大家都會使用post請求發送表單信息,形如上面的示例,然而表單還可以使用get請求方式提交,下面分別對兩種方式提交方式進行演示。

一、get方式提交表單

index.html文件

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>xxx登錄</title> </head> <body> <!-- 表單提交方式為get,提交到 /Form_Process/LoginServlet--> <form action="/Form_Process/LoginServlet" method="get"> 用戶名:<input type="text" name="username"><br> 密碼:<input type="password" name="password"><br> <!-- 性別只能有一個,所以為單選框 --> 性別:<input type="radio" name="gender" value="男">男 <input type="radio" name="gender" value="女">女<br> <!-- 愛好可能同時有多個,所以需要使用復選框 --> 愛好:<input type="checkbox" name="hobby" value="運動">運動 <input type="checkbox" name="hobby" value="賺錢">賺錢 <input type="checkbox" name="hobby" value="編碼">編碼<br> <input type="submit" value="提交"> </form> </body> </html> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

LoginServlet.java文件

package cn.hestyle.web.servlet; import java.io.IOException; import java.util.Arrays; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { /** * 處理前端get請求 */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); String gender = request.getParameter("gender"); //注意hobby可能有多個,所以需要使用字符串數組獲取 String[] hobbies = request.getParameterValues("hobby"); System.out.println(username); System.out.println(password); System.out.println(gender); System.out.println(Arrays.toString(hobbies)); } /** * 處理前端post請求 */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("post請求處理未實現!"); } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

運行結果:
訪問index.html資源,並且填寫表單信息
在這里插入圖片描述
點擊提交按鈕后
在這里插入圖片描述

二、post方式提交表單

項目目錄結構:
在這里插入圖片描述
index.html文件

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>xxx登錄</title> </head> <body> <!-- 表單提交方式為post,提交到 /Form_Process/LoginServlet--> <form action="/Form_Process/LoginServlet" method="post"> 用戶名:<input type="text" name="username"><br> 密碼:<input type="password" name="password"><br> <!-- 性別只能有一個,所以為單選框 --> 性別:<input type="radio" name="gender" value="男">男 <input type="radio" name="gender" value="女">女<br> <!-- 愛好可能同時有多個,所以需要使用復選框 --> 愛好:<input type="checkbox" name="hobby" value="運動">運動 <input type="checkbox" name="hobby" value="賺錢">賺錢 <input type="checkbox" name="hobby" value="編碼">編碼<br> <input type="submit" value="提交"> </form> </body> </html> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

LoginServlet.java文件

package cn.hestyle.web.servlet; import java.io.IOException; import java.util.Arrays; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { /** * 處理前端get請求 */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("get請求處理未實現!"); } /** * 處理前端post請求 */ 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"); //注意hobby可能有多個,所以需要使用字符串數組獲取 String[] hobbies = request.getParameterValues("hobby"); System.out.println(username); System.out.println(password); System.out.println(gender); System.out.println(Arrays.toString(hobbies)); } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

運行結果:
訪問index.html資源,並且填寫表單信息
在這里插入圖片描述
請求信息:
在這里插入圖片描述
控制台輸出:
在這里插入圖片描述
get方式提交表單,瀏覽器會表單信息放到url中,post是提交表單,瀏覽器會將表單信息放到請求體中。但是實際開發中不能明文傳輸密碼,以上只是方便演示,一般會加上一些加密操作,大多數都是使用post方式提交表單。

中文亂碼問題處理:

在上面的演示中都沒有出現亂碼問題,但是很多初學者都會遇到表單數據亂碼問題。

get方式提交的表單信息中文亂碼

在Tomcat8、Tomcat9一般不是出現亂碼問題,但是Tomcat7.0會出現亂碼,這是由於Tomcat7的接收參數時采用了IOS-8859-1編碼,Tomcat8、9默認是UTF-8編碼集。所以在Servlet中需要對表單信息進行轉碼。比如獲取性別時

//將ISO-8859-1編碼轉換為UTF-8編碼 String gender = new String(request.getParameter("gender").getBytes("ISO-8859-1"), "UTF-8"); 
  • 1
  • 2

上面演示使用的Tomcat9,所以沒有進行轉碼也沒有出現中文亂碼。

post方式提交的表單信息中文亂碼

我們需要在Servlet中設置request的編碼集。

request.setCharacterEncoding("utf-8"); 
  • 1

如果還出現了中文亂碼,可能是前端html、jsp編碼出現問題,修改為UTF-8。

 

原文:https://blog.csdn.net/qq_41855420/article/details/101850258


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM