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

Source: Internet
Author: User

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 following code:

<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.