Based on Request.getattribute and request.getparameter of the difference between the detailed _java

Source: Internet
Author: User

The HttpServletRequest class has both the getattribute() method and the Getparameter() method, which have the following differences:
1, HttpServletRequest class has the setattribute() method, but does not have the setparameter() method;
2, when the link between two Web Components, the linked component through the getparameter() method to obtain request parameters;

For example, suppose there is a link between welcome.jsp and authenticate.jsp, and welcome.jsp has the following code:

Copy Code code as follows:

<a href= "authenticate.jsp?username=qianyunlai.com" >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 the forwarding source component.
Assume a forwarding relationship between authenticate.jsp and hello.jsp. Authenticate.jsp want to pass the current user name to hello.jsp, how to pass this data? First call the SetAttribute () method in authenticate.jsp:

Copy Code code as follows:

<%
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:
Copy Code code as follows:

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

4, the Request.getattribute returned is Object, Request.getparameter returned to the String。

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.