当前位置:网站首页>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 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 .

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

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 .
边栏推荐
- Educational Codeforces Round 114 (Rated for Div. 2) D
- 二分图King
- JVM class loader; Parental delegation mechanism
- Educational Codeforces Round 114 (Rated for Div. 2) D
- 一步步把 SAP UI5 应用部署到 SAP BTP Kyma 运行环境中去
- As a senior abap consultant, which SAP technology can be selected as the main direction in the next step?
- How to use RPA robot to start the first step of digital transformation of freight forwarding industry?
- Redis basic data type (set)
- Educational codeforces round 111 (rated for Div. 2) C Supplement
- Why is rpa+ low code a powerful tool to accelerate the digital transformation of finance?
猜你喜欢

LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)

JVM | runtime data area; Program counter (PC register);

Jenkins+allure integrated report construction

LeetCode-104-二叉树的最大深度

Syntax of SQL

即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍

JVM | virtual machine stack (local variable table; operand stack; dynamic link; method binding mechanism; method call; method return address)

JS performs non empty judgment on various data types of the returned data.

LabVIEW Arduino electronic weighing system (project Part-1)
![BZOJ3189 : [Coci2011] Slika](/img/46/c3aa54b7b3e7dfba75a7413dfd5b68.png)
BZOJ3189 : [Coci2011] Slika
随机推荐
为什么需要微服务
bzoj3188 Upit
LeetCode-43-字符串相乘
How to import workflows provided on SAP API hub to sap BTP
JVM | local method interface; Native Method Stack
Educational Codeforces Round 114 (Rated for Div. 2) D
LeetCode-110-平衡二叉树
杭电中超9 1006 Guess the Weight
2021-09-11 训练场补题
Educational Codeforces Round 111 (Rated for Div. 2) C 补题
Bug -- coredump usage
Redis basic data type (set)
Deploy SAP ui5 applications to the sap BTP kyma operating environment step by step
JVM|类加载器;双亲委派机制
js对返回的数据的各种数据类型进行非空判断。
Leetcode-129- sum of numbers from root node to leaf node
JVM|前言介绍
LabVIEW控制Arduino实现超声波测距(进阶篇—5)
Expérience 10 génération de courbes bezier - amélioration expérimentale - génération de courbes B - spline par point de contrôle
Leetcode-104- maximum depth of binary tree