通过post和get两种方式提交表单数据。
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>提交表单</title>
</head>
<body>
<h2>Get请求</h2>
<form action="/servlet_war_exploded/form" method="get">
<!--文本框-->
名字:<input type="text" name="username"><br/>
<!--密码框-->
密码:<input type="password" name="password"><br/>
<!--下拉框-->
地址:
<select name="address">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="深圳">深圳</option>
</select><br>
<!--单选框-->
性别:<input type="radio" name="sex" value="male" checked>男
<input type="radio" name="sex" value="female">女<br>
<!--复选框-->
爱好:<br>
篮球<input type="checkbox" name="hobby" value="篮球" checked="checked" />
足球<input type="checkbox" name="hobby" value="足球" />
乒乓球<input type="checkbox" name="hobby" value="乒乓球"/><br>
<!--文本域-->
说明:<br>
<textarea name="desc" rows="3" cols="20"></textarea><br>
<input type="submit" value="get提交">
</form>
<hr>
<h2>Post请求</h2>
<form action="/servlet_war_exploded/form" method="post">
<!--文本框-->
名字:<input type="text" name="username"><br/>
<!--密码框-->
密码:<input type="password" name="password"><br/>
<!--下拉框-->
地址:
<select name="address">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="深圳">深圳</option>
</select><br>
<!--单选框-->
性别:<input type="radio" name="sex" value="male" checked>男
<input type="radio" name="sex" value="female">女<br>
<!--复选框-->
爱好:<br>
篮球<input type="checkbox" name="hobby" value="篮球" checked="checked" />
足球<input type="checkbox" name="hobby" value="足球" />
乒乓球<input type="checkbox" name="hobby" value="乒乓球"/><br>
<!--文本域-->
说明:<br>
<textarea name="desc" rows="3" cols="20"></textarea><br>
<input type="submit" value="post提交">
</form>
</body>
</html>
servlet
通过servlet读取表单数据时,可以通过request的
getParameter(String name)
方法获取表单中的值。当表单中一个name对应多个值时(如多选框),可以使用
getParameterValues(String name)
方法获取值。另外需要注意post请求乱码问题。
package com.zjw;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
@WebServlet(name = "ReadFormServlet" , value = "/form")
public class ReadFormServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post请求的request数据会有乱码
request.setCharacterEncoding("UTF-8");
System.out.println(getFormData(request));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get请求的request数据在tomcat8后不会出现乱码
System.out.println(getFormData(request));
}
//获取form表单中的数据
private String getFormData(HttpServletRequest request){
//文本框
String username = request.getParameter("username");
//密码框
String password = request.getParameter("password");
//下拉框
String address = request.getParameter("address");
//单选框
String sex = request.getParameter("sex");
//复选框
String[] hobbies = request.getParameterValues("hobby");
//文本域
String desc = request.getParameter("desc");
return "username="+username+"
"
+"password="+password+"
"
+"address="+address+"
"
+"sex="+sex+"
"
+"hobbies="+ Arrays.toString(hobbies) +"
"
+"desc="+desc ;
}
}
提交表单,后台打印结果
username=root
password=123456
address=北京
sex=female
hobbies=[篮球, 足球, 乒乓球]
desc=我是说明文字