The GetAttribute () and GetParameter () methods of the Request object

Source: Internet
Author: User

the HttpServletRequest class has both the getattribute () method and the getparameter () method, which differ in the following ways:

(1) The HttpServletRequest class has a SetAttribute () method and no setparameter () method

(2) when the two Web components are link relationships, the linked component obtains the request parameters through the GetParameter () method, such as assuming that welcome.jsp and authenticate.jsp between links ,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= "submitted" name= "submit" value= "Submission" >  </form>  

The request parameters are obtained through the Request.getparameter ("username") method in authenticate.jsp username

<input type= "Submit" name= "Submit" value= "Submission" >  ;/form>  

(3web between components is a forwarding relationship, The forwarding target component shares lang= getattribute () request "HI" method authenticate.jsphello.jsp relationship. authenticate.jsp wish to hello.jspsetattribute () in authenticate.jsp method:

<%  String username=request.getparameter ("username");  Request.setattribute ("username", username);  %>  <jsp:forward page= "hello.jsp"/>  in hello.jsp by GetAttribute () method get user name:  <% String Username= (String) Request.getattribute ("username"); %>  Hello: <%=username%>  

From a deeper level of consideration, 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 data of type String.

The data passed by the Request.setattribute () and GetAttribute () methods only exist inside the Web container and are shared between Web Components that have a forwarding shutdown . These two methods can set shared data of type Object
Request.getparameter () is obtained through the implementation of the container to obtain the data passed through like post,get, etc.

Request.setattribute () and getattribute () only flow inside the Web container, only the request processing phase.

Overall: The Request.getattribute () method returns the object that exists in the request scope, and the Request.getparameter () method is to get the data submitted by HTTP. GetAttribute is the return object , GetParameter returns the string.

The GetAttribute () and GetParameter () methods of the Request 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.