Transfer http://kanghao126.javaeye.com/blog/99104
The HttpServletRequest class includes both the getAttribute () method and the getParameter () method. The two methods have the following differences:
(1) The HttpServletRequest class has the setAttribute () method instead of the setParameter () method.
(2) When two Web components are linked, the linked component obtains the request parameters through the getParameter () method, for example, assuming welcome. jsp and authenticate. jsp is a link, welcome. jsp has the following code:
<A href = "authenticate. jsp? Username = weiqin "> authenticate. jsp </a>
Or:
- <Form name = "form1" method = "post" action = "authenticate. jsp">
- Enter the User name: <input type = "text" name = "username"> <input type = "submit" name = "Submit" value = "submit"> form>
- In authenticate. jsp, obtain the request parameter username through the request. getParameter ("username") method: <% String username = request. getParameter ("username"); %>
Html code
In authenticate. jsp, the request parameter username is obtained through the request. getParameter ("username") method:
(3) When the forwarding relationship is between two Web components, the forwarding target component shares the data within the request range with the conversion source component through the getAttribute () method. Assume that the relationship between authenticate. jsp and hello. jsp is forwarding. Authenticate. jsp wants to pass the current username to hello. jsp. How can this data be transmitted? First, call the setAttribute () method in authenticate. jsp:
<Jsp: forward page = "hello. jsp"> </jsp: forward> in hello. jsp, use the getAttribute () method to obtain the username: Hello:
(4) at a deeper level, the data transmitted by the request. getParameter () method will be transmitted from the Web Client to the Web server, representing the HTTP request data. The request. getParameter () method returns String-type data.
The data transmitted by the request. setAttribute () and getAttribute () methods only exists in the Web Container and is shared among the Web Components with forwarding relationships. These two methods can be used to set Object-type shared data.
GetParameter is of the String type. Or 123 in http://a.jsp id = 123, or a form submits data in the past.
GetAttribute can be an object.
GetParameter () is used to obtain the parameter value passed by POST/GET;
GetAttribute () is used to obtain the data value in the object container;
GetParameter: used for client redirection, that is, when the link is clicked or the button is submitted, the value is used, that is, the data is received when the form or url redirection is used.
GetAttribute: used for server-side redirection, that is, the forward function is used in the sevlet, or the mapping. findForward function is used in struts. GetAttribute can only receive Values Transmitted by the program using setAttribute.
In addition, you can use setAttribute and getAttribute to send the receiving object, while getParameter can only transmit strings.
SetAttribute indicates that the application server places this object in the memory of the corresponding page. When your Page Server redirects to another page, the Application Server copies the memory to the memory corresponding to another page. In this way, getAttribute can get the value you set. Of course, this method can be used to transmit objects. The same is true for sessions, but the lifecycle of objects in the memory is different.
GetParameter is only used by the application server to analyze the text of the request page you sent, and obtain the value you set when you set it to form or url redirection.