Almost all web development languages support the session function, and servlet is no exception. The session function in Servlet/JSP is throughScope(Scope.
There are four scopes:
Page: valid for the current page (for JSP only)
Request: valid in the current request
Session: valid in the current session
Application: valid in all applications
We can see that the most basic unit of web interaction isHTTP Request. Each user's process from entering the website to leaving the website is calledHTTP sessionWhen a server is running, multiple users can access it, that is, multiple HTTP sessions. Then the scope can be understood:
Request: the start time and end time of the HTTP request.
Session: the start time and end time of the HTTP session.
Application: The time from server startup to stop
Request
The processing of an HTTP request may require the cooperation of multiple servlets. Several servlets can transmit information in some way, but this information becomes invalid after the request ends.
Servlet Information sharing is implemented through the httpservletrequest interface:
Void setattribute (string name, object value)
Save the object value with the name in the request scope.
Object getattribute (string name)
Obtain the specified name information from the request scope.
The first parameter of the doget () and dopost () functions is the httpservletrequest object, which can be used to transmit information.
After setting the information, how can I transmit the information to other servlets? This requires the forward method of the requestdispatcher interface to forward requests to other servlets.
Requestdispatcher servletcontext. getrequestdispatcher (string path)
Get dispatcher for forwarding. PATH is the destination servlet for forwarding.
Void requestdispatcher. Forward (servletrequest request, servletresponse response)
Forward requests and response.
Therefore, you only need to setattribute first in the current servlet, forward, and finally getattribute In the servlet to which the forward is sent to implement information transmission.
PHP programmers may not quite understand this section. Because PHP does not have the forwarding concept, a request can only be processed by one PHP file, so PHP does not have the concept of request scope. The servlet is different. requests can be forwarded randomly in the application. Therefore, the request scope is used to transmit information between different servlets. Pay attention to the following two points:
1.Forwarding is not redirection, Forwarding is performed inside the web application. PHP supports redirection but does not forward data.
2.Forwarding is transparent to the browserThat is to say, no matter how the server forwards it, the address bar of the browser still displays the address of the original servlet.
Session
The session scope is easy to understand. When a browser accesses the session for multiple times, information is transmitted between the multiple accesses, that is, the session scope.
The session is implemented through the httpsession interface.
Object httpsession. getattribute (string name)
Obtain information from the session
Void httpsession. setattribute (string name, object value)
Save information to the session
The httpservletrequest. getsession () method can be used to obtain the httpsession object.
Httpsession httpservletrequest. getsessio ()
Obtains the session object of the current request.
The start of the session is easy to judge (the browser determines the start of the session when sending the first HTTP request), but it is difficult to judge the end (because the browser does not notify the server when it is closed "I shut down, the session can end "), so we can only use this method to judge: if the client does not respond within a certain period of time, the session ends. The default value of Tomcat is 120 minutes, but this value can also be set through the setmaxinactiveinterval method of httpsession.
Void setmaxinactiveinterval (INT interval)
Set the painting timeout value.
If you want to end the session, you can use the invalidate method of httpsession when you click "logout:
Vooid invalidate ()
End the current session forcibly.
Application
The application scope refers to the entire period from server startup to server shutdown. The information set in this scope can be used by all applications.
Information Transfer in application scope is implemented through servetcontext.
Object getattribute (string name)
Obtain information from the application.
Void setattribute (string name, object value)
Set Information to the application scope.