Stateless State of the HTTP protocol 1, stateless means that the server responds when the browser sends a request to the server. But when the same browser sends the request again, the server will not know that it was just that browser. 2, simply said, the server "will not save the user state", will not remember whether the client has been visited, so this is a stateless protocol.
JSP state management Two major mechanisms for saving User State 1, Session 2, Cookie What is a cookie? Cookie: A series of textual information that the Web server holds on the client. One of the typical applications: determine if the registered user is already logged in to the site. Typical two: Save user browsing history. The role of Cookies: 1, the tracking of specific objects. 2, save the user's web browsing records and habits. 3, simplified login security risk: Easy to disclose user information.
Create and use Cookie:1 in JSP, create cookie object: Cookie cookie=new Cookie (String key,object value); 2, write Cookie:response.addCookie ( cookies); 3, read cookie:cookie[] cookies=request.getcookies ();
Common methods: 1. Setmaxage (expiry) sets the validity period of the cookie, in seconds getmaxage () to obtain the effective time of the cookie, in seconds 2. SetValue (String value) after the cookie is created, the cookie is assigned a value of GetValue () to get the value of the cookie 3. GetName () Gets the name of the cookie 4. SetValue () and GetValue () are all related to strings, which correspond to the previously mentioned Cookie, which is a text file that the Web server holds on the client.
Solve the problem that the cookie cannot save the Chinese string 1.java.net the Urlencoder class under the package to encode public static encode (string string, string Enco); 2. The Urldecoder class decodes Urldecoder.decode (String string, String Enco), 3. If the value of username and password is set to NULL at the login interface, Then the login box will appear null is best set to "" 4. When looking for cookies, is to find by K value-----------------------------------use Urlencoder (under the java.net package) to solve the problem of saving Chinese strings in a cookie: string username = Urlencoder.encode (Request.getparameter ("username"), "Utf-8");//Encoding to prevent Chinese garbled string password = Urlencoder.encode ( Request.getparameter ("password"), "Utf-8");//code to prevent Chinese garbled
Session vs. Cookie 1. Save location: Session in server-side memory, cookie in client Text 2. Save object: Session Save object Class (there is no limit to save size), Cookie saves string type (Save object size is limited) 3. Right to life: SE Ssion sessions are destroyed at the end of the session, and cookies can be stored for a long time on client 4. Importance: Session security is higher, important information is saved, cookies keep unimportant information
JSP-JSP State Management