Home  >  Article  >  Java  >  How does JAVA operate Session variables in JSP through Servlet?

How does JAVA operate Session variables in JSP through Servlet?

WBOY
WBOYforward
2023-05-02 17:34:131110browse

Use Servlet to control the session

Maintaining session state is a problem that must be faced when developing Web applications. There are many ways to solve this problem. For example, using cookies, hidden type form fields, or directly adding status information to the URL, etc., and the Servlet itself provides an HttpSession interface to support the maintenance of session state. Here we mainly introduce the management of session state based on this interface.

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. This connection between the client and the server is discrete and discontinuous. 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 it is not currently defined session, create a new session, and you can also use the method getSession(true)

 (2) Write the session variable. 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, such as:
  String uid = (String) session.getAttribute("uid");
 You can also use HttpSession.getValue (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
// 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
Get the output Writer
   HttpSession session = request.getSession(true);
                           
##    ////Print HTML tags
   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 the session creation time
  out.println("Last Accessed: " accessed " ");
     //Print the last accessed time

session.setattribute ("uID", "12345678");
// Add variables in session uid = 12345678
session.setttribute ("name", "tom");
// Add the variable Name=Tom


in the session. Enumeration e = session.getAttributeNames();
) { //Traverse each variable
String //Get the name first
String value = session.getAttribute(name).toString();
//Get the value from the session by name
out .println(name " = " value " "); //Print
   }
  out.println(""); //Print HTML markup
  out.println("");
   }
  }
 }

The above is the detailed content of How does JAVA operate Session variables in JSP through Servlet?. 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