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。