The difference between request-getparameter () and getattribute ()

Source: Internet
Author: User
Tags form post

The HttpServletRequest class has GetParameter (), getattribute (), setattribute (), and no Setparameter () method

GetParameter () is used to post or get the form, to obtain the data in the form, or the parameters on the URL.

When the Web component is linked (I think this refers to web redirection), the linked Web component obtains the parameter by GetParameter () method

We have included the following code in the b.jsp:

<!--b.jsp-->
<a href= "A.jsp?username=admin" >a.jsp</a>
or form Post/get Submission method
<!--b.jsp-->
<form ame= "form" method= "POST" action= "a.jsp" > Username: <input type= "text" Name= " Username "/> <input type=" Submission "name=" submit "value=" submitted "/></form>
The username parameter in the b.jsp is submitted to the a.jsp, so we can get getparameter by username in the a.jsp

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

The data passed by the Request.getparameter () method is uploaded from the Web client to the Web server side, representing the HTTP request data. The Request.getparameter () method returns the data of type string.

When a forwarding relationship is between two Web components, the forwarding target component shares the data in the request scope through the GetAttribute () method and forwarding the source component. Assume a forwarding relationship between a.jsp and b.jsp

A.jsp wants to pass the current user name to b.jsp and how to pass the data. First call the SetAttribute () method in a.jsp:

<%
String username=request.getparameter ("username");
Request.setattribute ("username", username);
%>
<!--forwarded to b.jsp-->
<jsp:forward page= "b.jsp"/>
The user name is obtained by the GetAttribute () method in b.jsp

<% string Username= (String) Request.getattribute ("username"); %>
Username: <%= username%>
The data passed by the Request.setattribute () and GetAttribute () methods only exist within the Web container and are shared among Web components that have forwarding relationships. These two methods can set the shared data of type object.

Summarized:
Request.getparameter () acquisition is achieved through the implementation of the container to obtain through similar post,get and other means of data passed in.
Request.setattribute () and getattribute () only flow within the Web container, only the request processing phase.
GetAttribute is the return object, GetParameter returns a string
Overall:
Request.getattribute () method returns an object that exists within the Reques,sessiont range
The Request.getparameter () method is to obtain data submitted by HTTP.



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.