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!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
