当前位置:网站首页>Servlet get form data

Servlet get form data

2022-06-11 21:40:00 Situ Xiaoye

Java servlet obtain form The form data ( Parameters )

stay Servlet How to use HttpServletRequest Get request parameters and request Objects are used to transfer data .

1. Get request parameters

In actual development , It is often necessary to obtain the form data submitted by users , For example, user name and password , For the convenience of obtaining the request parameters in the form , stay HttpServletRequest Interface's parent class ServletRequest A series of methods to obtain request parameters are defined in ,

As shown in the table 1 Shown .

Method statement Function description
String getParameter(String name) This method is used to get the parameter value of a specified name . If the request message does not contain a parameter with the specified name , be getParameter() Method returns null. If the parameter with the specified name exists but no value is set , An empty string is returned . If the request message contains more than one parameter with the specified name , be getParameter() Method returns the first parameter value .
String [] getParameterValues (String name)HTTP There can be multiple parameters with the same name in the request message ( Usually consists of a field element that contains multiple fields with the same name form Form generation ), If you want to get HTTP All parameter values corresponding to the same parameter name in the request message , Then we should use getParameterValues() Method , This method is used to return a String An array of types .
Enumeration getParameterNames() Method is used to return a containing the names of all parameters in the request message Enumeration object , On this basis , All parameters in the request message can be traversed .
Map getParameterMap()getParameterMap() Method is used to load all parameter names and values in the request message into one Map Object .

In the table 1 in ,getParameter() Method is used to get a specified parameter , and getParameterValues() Method is used to obtain multiple parameters with the same name . The use of these two methods is explained through specific cases :

stay servletDemo02 Project WebContent Create a form file under the root directory form.html, Edit it as follows .

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="/servletDemo02/RequestsParamServlet" method="POST">
		 user name :<input type="text" name="username"><br />
		 The secret &nbsp;&nbsp;&nbsp;&nbsp; code :<input type="password" name="password" /><br />
		<br />  hobby : <input type="checkbox" name="hobby" value="sing" /> Sing a song  <input type="checkbox" name="hobby" value="dance" /> dance  <input type="checkbox" name="hobby" value="game" /> Play a game  <input type="submit" value=" Submit " />
	</form>
</body>
</html>

stay com.mengma.request Write a name in the package RequestParamsServlet Of Servlet class , Use this Servlet Get request parameters , As shown below .

package com.mengma.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestParamsServlet extends HttpServlet {
    

    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
    
        String name = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(" user name " + name);
        System.out.println(" password " + password);
        //  Get the parameter named "hobby" Value 
        String[] hobbys = request.getParameterValues("hobby");
        System.out.println(" hobby :");
        for (int i = 0; i < hobbys.length; i++) {
    
            System.out.println(hobbys[i] + ",");
        }

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    
        doGet(request, response);
    }
}

stay html in , Because the parameter name is hobby The value of may have more than one , therefore , Need to use getParameterValues() Method to obtain the values of multiple parameters with the same name , Return to one String An array of types , By traversing the array , Output each hobby The corresponding value of the parameter .

start-up Tomcat The server , Enter the address in the address bar of the browser http://localhost:8080/servletDemo02/form.html visit form.html page , And fill in the relevant information of the form , The completed page is shown in the figure 1 Shown .

 Running results
chart 1 Running results

Click on the graph 1 Submit button in , stay MyEclipse The console outputs information about each parameter , Pictured 2 Shown .

 Running results
chart 2 Running results

2. adopt Request Object passing data

Request Object can not only get a series of data , You can also pass data through attributes .ServletRequest Interface defines a series of methods for operating attributes .

1)setAttribute() Method

This method is used to associate an object with a name and store it in ServletRequest In the object , Its complete syntax is defined as follows :

public void setAttribute(java.lang.String name,java.lang.Object o);

It should be noted that , If ServletRequest An attribute with the specified name already exists in the object , be setAttribute() Method will first delete the original attribute , Then add a new attribute . If passed to setAttribute() The property value object of the method is null, Then delete the attribute with the specified name , The effect is equivalent to removeAttribute() Method .

2)getAttribute() Method

This method is used to obtain the information from ServletRequest Object returns the property object with the specified name , Its complete syntax is defined as follows :

public java.lang.Object getAttribute(java.lang.String name);

3)removeAttribute() Method

This method is used to obtain the information from ServletRequest Object to delete the property with the specified name , Its complete syntax is defined as follows :

public void removeAttribute(java.lang.String name);

4)getAttributeNames() Method

This method is used to return a containing ServletRequest Of all property names in the object Enumeration object , On this basis , It can be done to ServletRequest All properties in the object are traversed .getAttributeNames() The complete syntax definition of the method is as follows :

public java.util.Enumeration getAttributeNames();

It should be noted that , Only data belonging to the same request can pass through ServletRequest Object passing data .

link :http://c.biancheng.net/view/4010.html

原网站

版权声明
本文为[Situ Xiaoye]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112128373986.html