Request object in jsp:
The request object can be used not only to set and obtain request range variables, but also to obtain client request parameters, request sources, headers, cookies, and so on.
GetParameter () method to obtain the client request parameter value
01
<% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
02
<! DOCTYPE html>
03
<Html>
04
<Head>
05
<Title> Request object receiving parameters </title>
06
</Head>
07
<Body>
08
<% -- Receive parameters through getParameter of the Request object -- %>
09
<%
10
Request. setCharacterEncoding ("gb2312"); // prevents Chinese garbled characters
11
String strName = (String) request. getParameter ("username"); // get the value of the name attribute
12
%>
13
<% = "Username:" + strName %>
14
</Body>
15
</Html>
01
<% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
02
<! DOCTYPE html>
03
<Html>
04
<Head>
05
<Title> form </title>
06
</Head>
07
<Body>
08
<Form action = "RequestDemo. jsp" method = "post">
09
Username: <input type = "text" name = "username"/>
10
<Input type = "submit" value = "submit"/>
11
</Form>
12
</Body>
13
</Html>
The getParameterNames () method obtains the names of all parameters.
01
<% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
02
<! DOCTYPE html>
03
<Html>
04
<Head>
05
<Title> form </title>
06
</Head>
07
<Body>
08
<Form action = "RequestDemo2.jsp" method = "post">
09
Username: <input type = "text" name = "username"/>
10
User password: <input type = "password" name = "usernamepassword"/>
11
<Input type = "submit" value = "submit"/>
12
</Form>
13
</Body>
14
</Html>
01
<% @ Page language = "java" contentType = "text/html; charset = gb2312" import = "java. util. *" %>
02
<! DOCTYPE html>
03
<Html>
04
<Head>
05
<Title> retrieve the names of all parameters using the Request object </title>
06
</Head>
07
<Body>
08
<% -- Receive parameters through getParameterNames of the Request object -- %>
09
<%
10
Enumeration e = request. getParameterNames (); // obtain the names of all parameters
11
While (e. hasMoreElements () {// traverse Enumeration
12
String str = (String) e. nextElement (); // retrieves the next element.
13
Out. println (str); // name of the output Element
14
}
15
%>
16
</Body>
17
</Html>
Below we can output the parameters and values together
01
<% @ Page language = "java" contentType = "text/html; charset = gb2312" import = "java. util. *" %>
02
<! DOCTYPE html>
03
<Html>
04
<Head>
05
<Title> retrieve the names of all parameters using the Request object </title>
06
</Head>
07
<Body>
08
<% -- Receive parameters through getParameterNames of the Request object -- %>
09
<%
10
Enumeration e = request. getParameterNames (); // obtain the names of all parameters
11
While (e. hasMoreElements () {// traverse Enumeration
12
String str = (String) e. nextElement (); // retrieves the next element.
13
String StrRequest = (String) request. getParameter (str); // gets the element value.
14
Out. println ("parameter" + str + "value:" + StrRequest); // name of the output Element
15
}
16
%>
17
</Body>
18
</Html>