1.session.setattribute () and Session.getattribute () are used in pairs, scoped to the entire session, and used when all the pages are using the data.
2.request.setattribute () and Request.getattribute () are paired, with the scope being between the request and the requested page. Request.setattribute () is used only when the next forward of the action needs to be used; Request.getattribute () represents the property that is set from the request scope. The property must be set setattribute before it can be obtained by getattribute, and the object type is set and obtained. In fact, the name and value of object in the form control are stored in a hash table, so the name of object is given here to the hash table to find the value corresponding to it. The arguments for SetAttribute () are string and object.
3.request.getparameter () is the parameter that receives the parameter, and the parameter is the page submission. Include: Parameters for form submission, URL rewriting (that is, ID in xxx?id=1), and so on, so this method does not have a parameter set (no Setparameter ()), and the receive parameter returns not an object, but a string type.
Example:
Session.setattribute ("Kindresult", result); Result is a StringBuffer type Object
Response.sendredirect (".. /manage_kind.jsp ");
In the manage_kind.jsp:
<%
StringBuffer kindresult=new StringBuffer ();
kindresult= (StringBuffer) session.getattribute ("Kindresult");
%>
<%=kindresult%>
##################################################################
I used Request.setattribute () to store information in the servlet.
The syntax is as follows: Request.setattribute ("User", "1234");
Then Response.sendredirect ("/hello.jsp");
But in my hello.jsp request.getattribute ("user");
The return value is NULL, why is the string "1234" not taken?
Notice here that Sendredirect cannot pass the request object. When using Request.setattribute, you cannot make redirect but forward. That is, the request is forwarded instead of redirected.
You can use Getservletcontext (). Getrequestdispatcher ("/hello.jsp"). Forward (Request,response) Go to the hello.jsp page, which is not aware to the client that the Hello.jsp page responds to it.
The request object is the same as the response object, and of course your arguments are passed.
You use Response.sendredirect ("/hello.jsp"); After you go to hello.jsp, the request object is new and your property value is naturally gone. But if you use the session instead of request, it's still possible.
Session.setattribute ("User", "1234");
Session.getattribute ("user");
#####################################################################################
4.request.getparameter () and Request.getattribute () differences
(1) Request.getparameter () is obtained through the implementation of the container to obtain the data via similar post,get, Request.setattribute () and getattribute () is only inside the Web container flow, Only the request processing phase.
(2) The data passed by the Request.getparameter () method is uploaded from the Web client to the Web server side and represents the HTTP request data. The Request.getparameter () method returns data of type String.
The data passed by the Request.setattribute () and GetAttribute () methods will only exist inside the Web container
Another point is that the HttpServletRequest class has a setattribute () method and no Setparameter () method.
Take an example, if two Web pages are linked, that is, if you want to link to 2.jsp from 1.jsp, the link is 2.jsp can get the request parameters through the GetParameter () method.
If there is a 1.jsp in
<form name= "Form1" method= "Post" action= "2.jsp" >
Please enter your name: <input type= "text" name= "username" >
<input type= "submitted" name= "submit" value= "Submission" >
</form>
In 2.jsp, the Request.getparameter ("username") method is used to obtain the request parameter username:
<% String Username=request.getparameter ("username"); %>
However, if the two Web is a forwarding relationship, the forwarding destination Web can use the GetAttribute () method to share data in the request scope with the source web, or an example.
With 1.jsp and 2.jsp
1.jsp want to pass the current user name to 2.jsp, how to pass this data? First call the following setattribute () method in 1.jsp:
<%
String Username=request.getparameter ("username");
Request.setattribute ("username", username);
%>
<jsp:forward page= "2.jsp"/>
The user name is obtained through the GetAttribute () method in 2.jsp:
<% string Username= (String) Request.getattribute ("username"); %>
5.request.getattribute () and Request.setattribute ()
Request.getattribute ("Nameofobj") can get the value of a control in a form on a JSP page. In fact, the name and value of object in the form control are stored in a hash table, so the name of object is given here to the hash table to find the value corresponding to it.
When using Request.setattribute (position, nameofobj) for different page values, only one pass from a.jsp to b.jsp, and then this request loses its scope, and then it will be set up again. Request.setattribute (). The use of Session.setattribute () always retains this value in a process.
P.s:javascript and JSPs cannot pass values to each other because JavaScript runs on the client and the JSP runs on the server side. If you want to allow parameters to be passed between them, you can set up a hidden control in the JSP and use its value in conjunction with the above mentioned usage to pass the desired value.
Links and differences between Request.setattribute (), Session.setattribute () and Request.getparameter () (record)