Difference between request. getparameter () and request. getattribute ()

Source: Internet
Author: User

Request. getparameter
Is used to accept parameters from the get or POST method
<Form method = post>
<Form method = get>
<A href = "1.jsp? Id = 1 "> OK </a>
Only java. Lang. string can be accepted.
That is, string parameter _id = request. getparameter ("parameter _id ");
Request. getattribute
Is used to accept variables or actions from servlet (in fact, action is a special servlet)
In action, request. setattribute ("RET", RET );
Only java. Lang. object can be accepted.
That is, list ret = (list) request. getattribute ("RET ");

One is a variable type and the other is an object type.

 

HTTP protocol

The Request Parameters in the get form are
URL? Back
Like http://xxx.xxx.xxx/aaa.jsp? A = 1

The Request Parameters in post form are in the Request body and are not in the HTTP header.

-------------------------------------------
The following section describes the HTTP protocol for GET requests.

GET/AAA. jsp? A = 1 HTTP/1.1
Accept: image/GIF, image/X-xbitmap, image/JPEG, image/pjpeg, application/X-Shockwave-flash ,*/*
Accept-language: ZH-CN
Accept-encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1; Embedded Web browser from: http://bsalsa.com/;. Net CLR 1.1.4322)
HOST: 127.0.0.1: 8080
Connection: keep-alive

The following section describes the HTTP protocol for post requests.
Post/AAA. jsp HTTP/1.1
Accept: image/GIF, image/X-xbitmap, image/JPEG, image/pjpeg, application/X-Shockwave-flash ,*/*
Referer: http: // 127.0.0.1: 8080/B .html
Accept-language: ZH-CN
Content-Type: Application/X-WWW-form-urlencoded
Accept-encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1; Embedded Web browser from: http://bsalsa.com/;. Net CLR 1.1.4322)
HOST: 127.0.0.1: 8080
Content-Length: 48
Connection: keep-alive
Cache-control: No-Cache
COOKIE: JSESSIONID = 111

A = 1

-------------------------------------
See the difference.
The values of these parameters are stored in the HTTP request section.
The preceding parameters must be obtained using the request. getparameter () method.

========================================================== ====================

While
The value obtained by the request. getattribute () method is stored on the server.
Four built-in objects on the server
Page
Request
Session
Application
The stored data must be obtained using the getattribute () method.

Lead: http://www.javathinker.org/bbs/topic.jsp? DB = 2 & topic = 76

The httpservletrequest class includes both the getattribute () method and the getparameter () method. The two methods have the following differences:

(1) The httpservletrequest class has the setattribute () method instead of the setparameter () method.

(2) When two Web components are linked, the linked component obtains the request parameters through the getparameter () method, for example, assuming welcome. JSP and authenticate. JSP is a link, welcome. JSP has the followingCode:

<A href = "authenticate. jsp? Username = weiqin "> authenticate. jsp </a>

Or:

<Form name = "form1" method = "Post" Action = "authenticate. jsp">
Enter the User name: <input type = "text" name = "username">
<Input type = "Submit" name = "Submit" value = "Submit">
</Form>

In authenticate. jsp, the request parameter username is obtained through the request. getparameter ("username") method:

<% String username = request. getparameter ("username"); %>

(3) When the forwarding relationship is between two Web components, the forwarding target component shares the data within the request range with the conversion source component through the getattribute () method. Assume that the relationship between authenticate. jsp and hello. jsp is forwarding. Authenticate. jsp wants to pass the current username to hello. jsp. How can this data be transmitted? First, call the setattribute () method in authenticate. jsp:

<%
String username = request. getparameter ("username ");
Request. setattribute ("username", username );
%>

<JSP: Forward page = "Hello. jsp"/>

In hello. jsp, use the getattribute () method to obtain the username:

<% String username = (string) request. getattribute ("username"); %>
Hello: <% = username %>

From a deeper perspective, the data transmitted by the request. getparameter () method is transmitted from the Web Client to the Web server, representing the HTTP request data. The request. getparameter () method returns string-type data.

The data transmitted by the request. setattribute () and getattribute () methods only exists in the Web Container and is shared among the Web Components with forwarding relationships. These two methods can be used to set object-type shared data.

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.