Get request parameters of HttpServletRequest application

In actual development, it is often necessary to obtain the form data submitted by the form, such as user name, password, email, etc.

How to get request parameters

String getParameter(String name)
This method is used to obtain the parameter value of the specified name. If there are multiple parameters with the specified name, the method returns the first parameter value.

String [] getParameterValues(String name) The
HTTP request message contains multiple parameters with the same name. If you want to get all the parameter values ​​corresponding to the same parameter in the HTTP request message, you should use String [] getParameterValues(String name) , This method is used to return an array of type String.

Enumeration getParameterNames() is
used to return a request message containing all the parameters.

Map getParameterMap()
This method is used to load all the parameter names and values ​​in the request message into a Map object and return.

Case

form.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/Test4/RequestParamsServlet" method="post">
    用户名:<input type="text" name = "username"></br>
     密码:<input type="password" name="password"></br>
    爱好:
    <input type="checkbox" name="hobby" value="sing">唱歌
    <input type="checkbox" name="hobby" value="dance">跳舞
    <input type="checkbox" name="hobby" value="football">足球</br>
    <input type="submit" value="提交">
      

</form>
</body>
</html>

RequestParameterServlet.java

package Test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RequestParamsServlet
 */
public class RequestParamsServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println("用户名"+username);
		System.out.println("密码"+password);
		//获取参数名为“hobby”的值
		String []hobbys = request.getParameterValues("hobby");
		System.out.println("爱好:");
		
		for(int i = 0 ; i<hobbys.length;i++) {
			System.out.println(hobbys[i]);
		}
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}


Experimental results:


Insert picture description here

to sum up:

To obtain a parameter value of a specified name, use the request.getParameter(String name) method. To obtain multiple parameter values ​​of a specified name, use the request.getParameterValues(String name) method to return multiple parameter values.

Guess you like

Origin blog.csdn.net/weixin_43553142/article/details/105794858