The maintenance of session state is a problem that must be faced in developing Web application, there are many ways to solve this problem, such as using Cookies,hidden type form field, or directly adding state information to the URL medium, And the servlet itself provides a httpsession interface to support session state maintenance, where we mainly describe the management of session state based on this interface.
The session was invented to fill the limits of the HTTP protocol. Notice how the HTTP protocol works-the user makes the request, the server responds, and the client-server connection is discrete, non-contiguous. The HTTP protocol does not provide the ability to allow the server to track user requests. After the server finishes responding to a user's request, the server cannot continue to remain connected with the browser. From the server side, each request is independent, so the HTTP protocol is considered a stateless protocol, and when a user switches between multiple pages, the server cannot know his identity. The advent of the session is to compensate for this limitation. With session, you can save information when a user switches between multiple pages. So many things that were never done before are much easier.
Each visitor receives a session separately from the time the visitor arrives from a particular home page to the point of departure.
Java The servlet defines a httpsession interface that implements the function of the session, and the process of using the session in the servlet is as follows:
(1) Using HttpServletRequest's GetSession method to get the current session, if the session is not currently defined, create a new session, you can also use the method GetSession (True)
(2) write the session variable. You can use Method Httpsession.setattribute (Name,value) to store a message in the session. You can also use Httpsession.putvalue (Name,value), but this method is obsolete.
(3) Read the session variable. You can use the method Httpsession.getattribute (name) to read a variable value in the session, and if name is a variable that is not defined, then NULL is returned. It is important to note that the variable type read from GetAttribute is object and must be cast with coercion type, such as:
String uid = (string) session.getattribute ("UID");
You can also use Httpsession.getvalue (name), but this method is obsolete.
(4) Close the session, then use the Session.invalidate () method to close the session when the session is finished. But it's not strict. Because, the servlet engine automatically shuts down seesion after a period of time.
Here is a simple example to illustrate the use of the session
97 Color Sessionexample.java
Import java.io.*;
Import java.util.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import the necessary packages
public class Sessionexample extends HttpServlet
{
public void doget (HttpServletRequest request, httpservletresponse response)
Throws IOException, Servletexception//Implement Doget method
{
Response.setcontenttype ("text/html"); Set HTTP Headers
PrintWriter out = Response.getwriter (); Get Output 97gan
HttpSession session = Request.getsession (true);
Get Session Object
Print HTML markup
Out.println ("
Out.println ("
Out.println ("<meta http-equiv=\" content-type\ "content=\" text/html; charset=gb2312\ ">");
Out.println ("
Out.println ("<body>");
Date created = new Date (Session.getcreationtime ());
Gets the time that the session object was created
Date accessed = new Date (Session.getlastaccessedtime ());
Gets the last time the session object was accessed
Out.println ("ID" + session.getid () + "<br>");
Get the ID of the session and print
Out.println ("Created:" + created+ "<br>");
Print Session creation time
Out.println ("Last accessed:" + accessed+ "<br>");
Print last access time
Session.setattribute ("UID", "12345678");
Add a variable to the session uid=12345678
Session.setattribute ("Name", "Tom");
Add a variable to the session Name=tom
Enumeration E = Session.getattributenames ();
Gets the enumeration object for the variable name in the session
while (E.hasmoreelements ()) {//Traverse each variable
String name = (string) e.nextelement (); First get the name
String value = Session.getattribute (name). toString ();
Get the value from the 97gan from the name
OUT.PRINTLN (name + "=" + value+ "<br>"); Print
}
Out.println ("</body>"); Print HTML markup
Out.println ("
}
}
}
Session in Java