Session was invented to fill the limitations of the HTTP protocol. Please note how the HTTP protocol works - the user makes a request and the server responds. The connection between the client and the server is discrete and non-continuous. The HTTP protocol does not provide functionality that allows the server to track user requests. After the server completes responding to the user's request, the server cannot continue to maintain a connection with the browser. From the server side, each request is independent, so the HTTP protocol is considered a stateless protocol. When a user switches between multiple homepages, the server cannot know his identity. The emergence of Session is to make up for this limitation. Using Session, you can save a user's information when he switches between multiple homepages. This makes many things that were impossible to do before become much easier.
During the period from when a visitor arrives at a specific homepage to when he leaves, each visitor will receive a separate Session.
Java Servlet defines an HttpSession interface, which implements the function of Session. The process of using Session in Servlet is as follows:
(1) Use the getSession method of HttpServletRequest to get the currently existing session. If the session is not currently defined, then To create a new session, you can also use the method getSession(true)
(2) to write session variables. You can use the method HttpSession.setAttribute(name, value) to store a piece of information in the Session. You can also use HttpSession.putValue(name, value), but this method is obsolete.
(3) Read Session variable. You can use the method HttpSession.getAttribute(name) to read the value of a variable in the Session. If name is an undefined variable, null is returned. It should be noted that the variable type read from getAttribute is Object, and forced type conversion must be used, for example:
String uid = (String) session.getAttribute("uid");
HttpSession.getValue can also be used (name), but this method is also obsolete.
(4) Close the session. After using the session, you can use the session.invalidate() method to close the session. But this is not strictly required. Because the Servlet engine automatically closes seesion after a period of time.
The following is a simple example to illustrate the use of session
//97色色SessionExample.java
import java.io.*;
import java.util.*;
import javax.servlet .*;
import javax.servlet.http.*;
//Import necessary software 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 header
PrintWriter out = response.getWriter(); //Get Output 97gan
HttpSession session = request.getSession(true);
//Get session object
//Print HTML tag
out.println("");
out .println("
The above is the detailed content of How to implement the session state of Session in JAVA. For more information, please follow other related articles on the PHP Chinese website!