Almost all Web development languages support the session feature, and the servlet is no exception. The session function in servlet/jsp is achieved through the concept of scope (scope).
The scope is divided into four kinds, respectively:
Page |
Valid on current page (in JSP only) |
Request |
Valid in the current request |
Session |
Valid in current session |
Application |
Valid in all applications |
Don't you see? Page because it is used only in JSPs, this only covers the other three scopes. The first thing to declare is that "scope" is "the range of information sharing", that is, how much information can be effective.
Words Wu Song Day came to the King Yang Hillock, see a banner waving, the flag of five characters "three bowls but hillock". Wu Song cried: "Store, take three bowls of wine, and then cut two kilograms of cooked beef!" "Bartender answered:" Three bowls of good wine, two kg of cooked beef ~ ~ "Inside the chef quickly when when when cut beef, bartender poured three bowls of wine, bartender end came.
Wu Song plump thump even dry three bowls, called a "good wine!" Store, three more bowls! "Small two busy and poured three bowls of good wine, Wu song a drink and do." In this way, Wu Song drank 18 big shots altogether. Pay the bill just want to go, bartender way: "Sir, this front mountain has the bug, Sir just drank 18 bowls of wine I am afraid to have no hillock, as a temporary stay in the store overnight, tomorrow and Orion together will not be good?" ”
After that, Wu Song will leave you reader to remember. In this section of Wu Song dozen Tigers, do you see any familiar things?
• Wu Song: browser.
• Pub: Server.
• Store small Two, chef: Servlet or JSP.
• Three bowls of good wine! : The browser sends an HTTP request to the server.
• Store small Second wine: server response.
• Wu Song from the store to the left: an HTTP session (that is, sessions).
We can see that the most basic unit of web interaction is the HTTP request (' Wu song La Carte '). Each user from entering the site to the process of leaving the site is called an HTTP session ("Wu song into the shop to shop"), a server running process will be more than one user access, is a number of HTTP sessions ("The pub certainly can not only receive Wu song a Guest"). Then the scope can be understood as:
Request |
The HTTP request begins to the end of the period |
Session |
HTTP session start to end period |
Application |
Server startup to stop this period of time |
Request
The processing of an HTTP request may require multiple servlet collaborations ("Wu Song ordered the kitchen to cook"), several servlet can pass information in some way ("Bartender notifies the kitchen"), but this information is invalid after the request has been completed (" The kitchen does not need to be in charge of this dish after the dish is finished. "
Information sharing between servlet is achieved through two methods of HttpServletRequest interfaces:
void SetAttribute (String name, Object value)
Saves the object value as name to the request scope.
Object getattribute (String name)
Gets the information of the specified name from the request scope.
The first parameter of the Doget (), DoPost () function is the HttpServletRequest object, which can be used to convey information using the setattribute of the object.
So how do you pass information to the other servlet after you set up the information? This requires the use of the forward method of the RequestDispatcher interface to forward the request to the other servlet.
RequestDispatcher servletcontext.getrequestdispatcher (String path)
Get dispatcher for forwarding. Path is the destination servlet for forwarding.
void Requestdispatcher.forward (ServletRequest request, servletresponse response)
Forwards the request and response.
Therefore, as long as in the current servlet first setattribute, then forward, and finally in forward to the servlet getattribute can achieve information delivery.
PHP programmers may not understand this paragraph, because there is no forwarding concept in PHP, a request can only be handled by a PHP file, so there is no request scope in PHP concept. Unlike a servlet, a request can be forwarded arbitrarily in an application, so it passes information between different servlet using the request scope. Two points to note:
1. Forwarding is not redirected, and forwarding is done within the Web application. PHP supports redirection but not forwarding.
2. Forwarding is transparent to the browser, that is, regardless of how forwarding is on the server, the browser address bar still shows the address of the original servlet.
Session
Session scope is relatively easy to understand, the same browser access multiple times, in this multiple access to pass information, is the session scope. Every time he orders, Mr. Wu has to keep an account of the accounts, and so Wu song to check out before leaving. This account is always valid during Wu Song's meal, which is in the session scope.
The session is implemented through the HttpSession interface.
Object Httpsession.getattribute (String name)
Getting information from the session
void Httpsession.setattribute (String name, Object value)
Save information to session
The HttpSession object can be obtained by means of the httpservletrequest.getsession () method.
HttpSession httpservletrequest.getsession ()
Gets the object of the session where the current request is located.
The beginning of the session is easy to judge (the browser makes the first HTTP request to start the conversation), but the end is difficult to judge (because the browser shuts down without notifying the server "I closed, the session can end"), so can only be judged by this method: if a certain amount of time the client does The end of the session is considered. The default value for Tomcat is 120 minutes, but this value can also be set through the HttpSession Setmaxinactiveinterval () method.
void setmaxinactiveinterval (int interval)
Sets the time-out value for the session.
If you want to take the initiative to end the session, you can use the HttpSession invalidate () method if the user clicks Logout:
void Invalidate ()
Forces the current session to end.
Application
The application scope is the entire period of time that the server boots to shutdown, and the information set in this scope can be used by all applications. (The restaurant closes after closing and uses all the information that is between opening and closing.) )
Do you remember the servetcontext mentioned in the previous section? The information transfer on the application scope is realized through Servetcontext.
Object getattribute (String name)
Get information from the application.
void SetAttribute (String name, Object value)
Sets the information to the application scope.
Summarize
As you can see, each scope, in addition to implementing interfaces and different meanings, uses the same methods and functions, all through the GetAttribute and SetAttribute methods for information delivery.
Scope |
Significance |
Implementing interfaces |
Request |
Within the HTTP request |
HttpServletRequest |
Session |
Within an HTTP session |
HttpSession |
Application |
Within the server life cycle |
ServletContext |
Sample Programs
Sample Programs
The example program in this section is an emulator that a user logs on to. More documents.
login.html Login Form
Dologin.java The servlet that handles login actions
Loginsuccess.java the servlet used to display login success information
Sessiontest.java logged in handler
Dologout.java a handler for logoff
To demonstrate the use of each scope of the request, application, and session, the data is passed between the servlet and the data is delivered as follows:
Data generation |
Data acceptance |
Data content |
Scope |
Dologin |
Loginsuccess |
Logon time |
Request |
Dologin |
Sessiontest |
Login user Name |
Session |
Dologin |
Sessiontest |
System logon Times |
Application |
To access Http://localhost:8080/LearnJSP/sessiontest, enter the username Charlee and password 123456 when you log in.
Sample Downloads: Session-test_jb51net.zip