Two mechanisms to save the state of a user (Cookie Session)
1.Cookie (Web server saves text messages on the client)
1.1 Effects:
1. Tracking for a specific object
2. Save user's web browsing history and habits
3. Simplified login operation
1.2 Disadvantages:
There is a security risk, easy to disclose user information
1.3 Creating/Using Cookies
1. Create a Cookie Object
Cookie cookie = new cookie (String key,object obj);
2. Write Cookie Object
Response.addcookie (cookie);
3. Read cookies
cookie[] cookies = request.getcookies ();
1.4 Common methods:
Method name |
Describe |
void setmaxage (int expiry) |
Set the lifetime of the cookie, in seconds |
void SetValue (String value) |
After creating a cookie, assign a value to the cookie |
String GetName () |
Get the name of the cookie |
String GetValue () |
Get the value of a cookie |
int Getmaxage () |
Gets the effective time of the cookie, in seconds |
2.Session and Cookie Differences
|
Session |
Cookies |
Describe |
Saving user information on the server |
Saving user information on the client |
Object type is saved in session |
When a cookie is saved, string type |
Destroys data stored by the end of the session |
Cookies can be stored for a long time on the client |
Save Important information |
Save non-critical user information |
JSP (Java Server Page) JSP state management