Request.getattribute () and request.getparameter () difference

Source: Internet
Author: User

The HttpServletRequest class has both the GetAttribute () method and the GetParameter () method, which has the following differences:

(1) The HttpServletRequest class has a setattribute () method, but no Setparameter () method

(2) When a link is between two Web components, the linked component obtains request parameters through the GetParameter () method, for example, assuming the link between welcome.jsp and authenticate.jsp. WELCOME.JSP has the following code:

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

Or:

<form name= "Form1" method= "Post" action= "authenticate.jsp" >
Please enter user name: <input type= "text" name= "username" >
<input type= "Submit" name= "Submission" value= "submitted" >
</form>

The Request.getparameter ("username") method is used in authenticate.jsp to obtain the request parameter username:

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

(3) 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 authenticate.jsp and hello.jsp. Authenticate.jsp wants to pass the current user name to hello.jsp and how to pass the data. 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, the user name is obtained through the GetAttribute () method:

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


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

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.

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.