Home  >  Article  >  Java  >  How to implement the session state of Session in JAVA

How to implement the session state of Session in JAVA

WBOY
WBOYforward
2023-05-11 13:10:06694browse

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("");
out.println("") ;
out.println("");
out.println("");
Date created = new Date(session.getCreationTime());
//Get the time when the session object was created
Date accessed = new Date(session.getLastAccessedTime());
//Get the time when the session object was last accessed
out.println("ID " session. getId() "
");
//Get the id of the session and print it
out.println("Created: " created "
");
//Print session creation time
out.println("Last Accessed: " accessed "
");
//Print the last access time
session.setAttribute("UID","12345678");
//Add variable UID=12345678 in session
session.setAttribute("Name","Tom");
//Add variable Name=Tom in session
Enumeration e = session.getAttributeNames ();
//Get the enumeration object of 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 97gan by name
out.println(name " = " value "}
out.println(""); //Print HTML tag
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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete