request通用的代碼
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
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 RequestDemo6
?*/
@WebServlet("/requestDemo7")
public class RequestDemo7 extends HttpServlet {
?? ?private static final long serialVersionUID = 1L;
????? ?
??? /**
???? * @see HttpServlet#HttpServlet()
???? */
??? public RequestDemo7() {
??????? super();
??????? // TODO Auto-generated constructor stub
??? }
?? ?/**
?? ? * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
?? ? */
?? ?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?//get 獲取請求參數(shù)
//?? ??? ?String username = request.getParameter("username");
//?? ??? ?System.out.println("get");
//?? ??? ?System.out.println(username);
?? ??? ?
?? ??? ?//根據(jù)參數(shù)名稱獲取參數(shù)值的數(shù)組
//?? ??? ?String[] hobbies = request.getParameterValues("hobby");
//?? ??? ?for (String hobby : hobbies) {
//?? ??? ??? ?System.out.println(hobby);
//?? ??? ?}
?? ??? ?
?? ??? ?//獲取所有請求的參數(shù)名稱
//?? ??? ?Enumeration<String> parameterNames = request.getParameterNames();
//?? ??? ?while(parameterNames.hasMoreElements()) {
//?? ??? ??? ?String name = parameterNames.nextElement();//獲取參數(shù)名
//?? ??? ??? ?System.out.println(name);
//?? ??? ??? ?String value = request.getParameter(name);//通過參數(shù)名獲取參數(shù)值
//?? ??? ??? ?System.out.println(value);
//?? ??? ??? ?System.out.println("-----------");
//?? ??? ?}
?? ??? ?//獲取所有參數(shù)的Map集合??? 為了防止復(fù)選框獲取不到其他值的情況
?? ??? ?Map<String, String[]> parameterMap = request.getParameterMap();
?? ??? ?//遍歷
?? ??? ?Set<String> keyset = parameterMap.keySet();
?? ??? ?for (String name : keyset) {
?? ??? ??? ?//根據(jù)鍵獲取值
?? ??? ??? ?String[] values = parameterMap.get(name);
?? ??? ??? ?System.out.println(name);
?? ??? ??? ?for (String value : values) {
?? ??? ??? ??? ?System.out.println(value);
?? ??? ??? ?}
?? ??? ??? ?System.out.println("---------");
?? ??? ?}
?? ?}
?? ?/**
?? ? * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
?? ? */
?? ?protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?//post 獲取請求參數(shù)
//?? ??? ?String username = request.getParameter("username");
//?? ??? ?System.out.println("post");
//?? ??? ?System.out.println(username);
?? ??? ?this.doGet(request, response);
?? ?}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注冊頁面</title>
</head>
<body>
?? ?<form action="requestDemo7" method="get">
?? ??? ?<input type="text"?? placeholder="請輸入用戶名" name="username"><br>
?? ??? ?<input type="text"? placeholder="請輸入密碼" name="password"><br>
?? ??? ?
?? ??? ?<input type="checkbox" name="hobby" value="game">游戲
?? ??? ?<input type="checkbox" name="hobby" value="study">學(xué)習(xí)<br>
?? ??? ?
?? ??? ?
?? ??? ?<input type="submit" value="注冊">
?? ??? ?
?? ?
?? ?</form>
</body>
</html>
標(biāo)簽: