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.