Request attribute request. getAttribute (), request. getattribute

Source: Internet
Author: User

Request attribute request. getAttribute (), request. getattribute

1. Difference between request. getParameter () and request. getAttribute ()

(1) request. getParameter () is achieved through the implementation of the container to obtain data transmitted through methods such as post and get, request. setAttribute () and getAttribute () are transferred only within the web Container, but only in the request processing phase.

(2) 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.

Another point is that the HttpServletRequest class has the setAttribute () method instead of the setParameter () method.

Let's take an example.

J1.jsp contains

[Html]View plain copy
  1. <! -- Link between j1 and j2 -->
  2. <Form method = "post" action = "j3.jsp">
  3. Username: <input type = "text" name = "username">
  4. <Input type = "submit" name = "submit" value = "submit">
  5. </Form>

In j2.jsp, request. getParameter ("username") is used to obtain the request parameter username:

[Html]View plain copy
  • <%
  • String s = request. getParameter ("username ");
  • %>
  • Name: <% = s %>

  • Or j1.jsp has

    [Html]View plain copy
  • <A href = "2.jsp? Username = accp "> 2.jsp</a>
  • In j2.jsp, request. getParameter ("username") is used to obtain the request parameter username.

    However, ifLet's take an example of the data within the range.

    J1.jsp j2.jsp and j3.jsp,

    J1.jsp month j3.jsp is the link, j3.jsp and j2.jsp are the forwarding relationship

    When j1.jsp is linked to j3.jsp, j3.jsp can obtain request parameters through the getParameter () method.

    How does j3.jsp transmit the user name obtained from j1.jsp to j2.jsp? Call the following setAttribute () method in j3.jsp:

    J1.jsp

     

    [Html]View plain copy
  • <Form method = "post" action = "j3.jsp">
  • Username: <input type = "text" name = "username">
  • <Input type = "submit" name = "submit" value = "submit">
  • Lt;/form>
  • J3.jsp

    [Html]View plain copy
  • <%
  • String username = request. getParameter ("username ");
  • Request. setAttribute ("username", username );
  • %>
  • <Jsp: forward page = "j2.jsp"/>
  • <! -- J1 to j3, j3 to j2 is a request object, request. setAttribute (), set an attribute for the request object, and then get it on another page -->

  • J2.jsp

     

    [Html]View plain copy
  • <% String username = (String) request. getAttribute ("username"); %>
  • J2 page, username: <% = username %>

  • 2. Further consideration

    The data transmitted by the request. getParameter () method is uploaded from the Web Client to the Web server. 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.
    GetParameter is of the String type. Or 123 in http://a.jspid = 123, or a form submits data in the past.

    GetAttribute can be an object.
    GetParameter () is used to obtain the parameter value passed by POST/GET;
    GetAttribute () is used to obtain the data value in the object container;
    GetParameter: used for client redirection, that is, when the link is clicked or the button is submitted, the value is used, that is, the data is received when the form or url redirection is used.

    GetAttribute: used for server-side redirection, that is, the forward function is used in the sevlet, or the mapping. findForward function is used in struts. GetAttribute can only receive Values Transmitted by the program using setAttribute.

    SetAttribute indicates that the application server places this object in the memory of the corresponding page. When your Page Server redirects to another page, the Application Server copies the memory to the memory corresponding to another page. In this way, getAttribute can get the value you set. Of course, this method can be used to transmit objects. The same is true for sessions, but the lifecycle of objects in the memory is different.

    GetParameter is only used by the application server to analyze the text of the request page you sent, and obtain the value you set when you set it to form or url redirection.

    In addition, JavaScript and JSP cannot pass values to each other because JavaScript runs on the client and JSP runs on the server. If you want them to pass parameters to each other, you can set a hidden control in JSP and use its value in combination with the above usage to pass the required parameters.

    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.