The difference between "getattribute" and "GetParameter" in Servlet HttpRequest

Source: Internet
Author: User

1. Different values are obtained
    • GetAttribute represents a property that is set from the request scope and must be set through setattribute to be obtained through getattribute. Both the set and the obtained values are of type object.
    • GetParameter is the parameter that receives the page submission, mainly has the form submits the parameter, the URL overrides passes the parameter (the ID value in the http://item.jsp?id=1) and so on, therefore does not need through the Setparameter to set the parameter, And the return type of the parameter is a string type.

2. Data transfer paths are different
    • The Request.getparameter () method passes data from the Web client to the Web service side, which represents the HTTP request data used for form or URL redirection.

Example:

For example, the conversion between two Web pages, exactly two Web pages when the link relationship, from login.jsp link to menu.jsp, menu.jsp can pass the getparameter() method to obtain the request parameters.

LOGIN.JSP:

1 <%  2// get parameter 3 String username=request.getparameter ("username" );   4 // Setting Properties 5 Request.setattribute ("username", username);   6 %>  7   8 <!--page Jump--! >9 <jsp:forward page= " Menu.jsp "/>
1 <form name= "Form1" method= "Post" action= "menu.jsp" >  2 Please enter user name: <input type= "text" name= "username" >< C1/>3 <input type= "Submit" name= "Submit" value= "Submission" >  4 </form>  

The request.getparameter ("username") method can be used to obtain the requested parameter username in menu.jsp:

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

    • The data passed by Request.getattribute () exists only within the Web container, within the request scope, which is the sharing between Web components when forwarding.

Example:

Login.jsp want to pass the current user name to menu.jsp, first call the SetAttribute() method in login.jsp:

1 <%  2// get parameter 3 String username=request.getparameter ("username");  4// Set Properties 5 Request.setattribute ("username", username);  6%>  7   8 <!--page Jump--! >9 <jsp:forward page= "menu.jsp"/> 

Get the user name in menu.jsp by the getattribute() method:

The difference between "getattribute" and "GetParameter" in Servlet HttpRequest

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.