Request object in jsp

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.