.1 Session
For a period of time, a series of related exchange processes between a single customer and a Web server.
ø4.1.1 Application Scenario: Save user state after user login
Determine the user's unique Sessin.getid ();
ø4.1.2 session creation and use: creating
Httpsession session= request.getsession (Boolean value);
Httpsession session= request.getsession ();
In the first method, when the Boolean value is true, if there is a session associated with the current request, the session is returned, otherwise a new session is created and the session is returned. When the Boolean value is Flase, if there is a session return, NULL is returned, and the session is no longer created.
The second method is equivalent to the case where the Boolean parameter value of the first method is true.
Use
Assignment Syntax : public void SetAttribute (String name, Object value);
Usage: Session.setattribute ("name", "Zhang Man");
value Syntax : public Object getattribute (String name);
Usage: String username = (string) session.getattribute ("name");
The session is saved on the server side, and if there is no cleanup mechanism, it can cause performance problems or a server crash.
Clear session:
1) The program actively clears session data
Set session invalidation: session.invalidate (); ///Logout
To remove an attribute from a session:
Syntax: public void RemoveAttribute (String name);
Usage: Session.removeattribute ("name");
2) The server actively cleans up the session without re-issuing the request for a long time
To set the session Expiration time:
Method one: public void setmaxinactiveinterval (int interval); (Unit: seconds)
The session expiration time can be set directly after assigning a value
Method Two: Configure the Web. xml file
Add to
<session-config>
<session-timeout>30<session-timeout>
</session-config> (unit: minutes note in Wep-app)
Three ways to end a session:
- Close browser
- Two access times are greater than inactive time
- Call HttpSession's Invalidate () method
The life cycle of the ø4.1.3 session
HttpSession technology is not within the scope of the HTTP protocol and is a service provided by the Web container for the program.
4.2 cookieø4.2.3 Application Scenario
Close the browser, end the session, restart, after the user name has been filled. Login to the mailbox or other services, the page directly filled out the user name.
ø4.2.4 How to use
public void Addcookie (Cookie cookie)
The cookie stores the information in the form of a file, which is stored in the client and can be set again by IE.
Add data: public void Addcookie (Cookie cookie)
Get data: Public cookie[] GetCookies ()
Set validity period: public void setmaxage (int expiry)
Users can disable: cookies
Added: Cookie cookie = new Cookie ("Variable name", value);
Cookie.setmaxage (60*60); Set the effective time, the default is wonderful
Response.addcookie (cookie);
return: cookie[] Cookie = Request.getcookes ();
For (...) {
if (Cookie.getname (). Equals ("variable name")) {
User=cookies[i].getvalue;
}
}
Disable cookies
4.3 Application
4.3.1 Usage Scenarios: Count the number of page visits, while online
4.3.2 Implementation mode: public void SetAttribute (String name,object Object)
Public Object getattribute (String name)
<%
Object count = Application.getattribute ("Count");
if (count ==null) {
Application.setattribute ("Count", New Integer (1));
}else{
Integer I = (integer) count;
Application.setattribute ("Count", I.value () +1);
}
%>
The data stored in request is made available in one request.
The data stored in the session can be used within the duration of a conversation.
The data stored in application is available throughout the Web project.
jsp-04-Implementing Data Preservation