-- Learning not just technology, but dreams!
Home HTML JAVASCRIPT CSS VUE REACT PYTHON3 JAVA C C++ C# AI GO SQL LINUX VS CODE BOOTSTRAP GIT Local Bookmarks
JSP Tutorial
JSP Tutorial
JSP Introduction
JSP Development Environment Setup
Eclipse JSP/Servlet
JSP Structure
JSP Life Cycle
JSP Syntax
JSP Directives
JSP Action Elements
JSP Implicit Objects
JSP Client Request
JSP Server Response
JSP HTTP Status Codes
JSP Form Processing
JSP Filters
JSP Cookie Handling
JSP Session
JSP File Upload
JSP Date Processing
JSP Page Redirection
JSP Hit Counter
JSP Auto Refresh
JSP Sending Emails
JSP Advanced Tutorial
JSP Standard Tag Library (JSTL)
JSP Database Connection
JSP XML Data Processing
JSP JavaBean
JSP Custom Tags
JSP Expression Language
JSP Exception Handling
JSP Debugging
JSP Internationalization
JSP Cookie Handling
JSP File Upload
Deep Dive
Scripts
Scripting Languages
Web Design & Development
Computer Science
Programming
Software
Web Service
Development Tools
Web Services
Programming Languages
JSP Session
HTTP is a stateless protocol, which means that each time a client retrieves a webpage, a separate server connection must be opened, so the server does not record any information from previous client requests.
There are three methods to maintain the session between the client and the server:
Cookies
The web server can specify a unique session ID as a cookie to represent each client, used to identify the client's subsequent requests.
This may not be an effective method, because many times browsers do not necessarily support cookies, so we do not recommend using this method to maintain sessions.
Hidden Form Fields
A web server can send a hidden HTML form field and a unique session ID, like this:
This entry means that when the form is submitted, the specified name and value will be automatically included in the GET or POST data. Whenever the browser sends a request, the value of session_id can be used to keep track of different browsers.
This method may be an effective way, but clicking a hyperlink in an
tag does not generate a form submission event, so hidden form fields also do not support general session tracking.
URL Rewriting
You can add some extra data to each URL to distinguish sessions, and the server can associate session identifiers based on this data.
For example, http://w3cschool.cc/file.htm;sessionid=12345, the session identifier is sessionid=12345, and the server can use this data to identify the client.
In comparison, URL rewriting is a better method, as it works even if the browser does not support cookies, but the drawback is that you must dynamically specify the session ID for each URL, even for a simple HTML page.
Session Object
In addition to the above methods, JSP uses the HttpSession interface provided by servlets to identify a user and store all access information for that user.
By default, JSP allows session tracking, and a new HttpSession object will be automatically instantiated for a new client. To disable session tracking, you need to explicitly turn it off by setting the session attribute value in the page directive to false, like this:
The JSP engine exposes the implicit session object to developers. With the session object, developers can conveniently store or retrieve data.
The following table lists some important methods of the session object:
S.N. Method & Description
1 public Object getAttribute(String name)
Returns the object bound to the specified name in the session object, or null if it does not exist.
2 public Enumeration getAttributeNames()
Returns all object names in the session object.
3 public long getCreationTime()
Returns the time when the session object was created, in milliseconds since midnight, January 1, 1970.
4 public String getId()
Returns the ID of the session object.
5 public long getLastAccessedTime()
Returns the last time the client accessed, in milliseconds since midnight, January 1, 1970.
6 public int getMaxInactiveInterval()
Returns the maximum time interval, in seconds, that the servlet container will keep the session open.
7 public void invalidate()
Invalidates the session and unbinds any objects bound to it.
8 public boolean isNew()
Returns whether this is a new client, or whether the client has refused to join the session.
9 public void removeAttribute(String name)
Removes the object with the specified name from the session.
10 public void setAttribute(String name, Object value)
Creates an object with the specified name and value and binds it to the session.
11 public void setMaxInactiveInterval(int interval)
Specifies the time, in seconds, that the servlet container will keep the session valid.
JSP Session Application
This example describes how to use the HttpSession object to get the creation time and last access time. We will associate a new session object with the request object if it does not already exist.
Session Tracking
Session Tracking
| Session Information |
Value |
| id |
|
| Creation Time |
|
| Last Access Time |
|
| User ID |
|
| Visit Count |
|
Try accessing http://localhost:8080/testjsp/main.jsp, and when you run it for the first time, you will get the following result:
When you visit again, you will get the following result:
Deleting Session Data
After processing a user's session data, you have the following options:
Remove a specific attribute:
Call the public void removeAttribute(String name) method to remove the specified attribute.
Delete the entire session:
Call the public void invalidate() method to invalidate the entire session.
Set session validity period:
Call the public void setMaxInactiveInterval(int interval) method to set the session timeout.
Log out the user:
Servers supporting servlet 2.4 can call the logout() method to log out the user and invalidate all related sessions.
Configure the web.xml file:
If you are using Tomcat, you can configure the web.xml file like this:
15
The timeout is in minutes, and the default timeout in Tomcat is 30 minutes.
The getMaxInactiveInterval() method in Servlet returns the timeout in seconds. If configured as 15 minutes in web.xml, the getMaxInactiveInterval() method will return 900.
JSP Cookie Handling
JSP File Upload
ByteArk Coding Plan
Supports mainstream large models like Doubao, GLM, DeepSeek, Kimi, MiniMax, officially supplied, stable and reliable.
Configuration Guide
¥9.9
/ Month
Activate Now
iFlytek Star Coding Plan
Includes free model call quota, DeepSeek, GLM, Kimi, MiniMax, one-stop experience and deployment platform.
Configuration Guide
¥3.9
/ Month
Activate Now
Click to Share Notes
Category Navigation
Python / Data Science
AI / Intelligent Development
Front-end Development
Back-end Development
Database
Mobile Development
DevOps / Engineering
Programming Languages
Computer Fundamentals
XML / Web Service
.NET
Website Building
Advertisement
JSP Tutorial
JSP Tutorial
JSP Introduction
JSP Development Environment Setup
Eclipse JSP/Servlet
JSP Structure
JSP Life Cycle
JSP Syntax
JSP Directives
JSP Action Elements
JSP Implicit Objects
JSP Client Request
JSP Server Response
JSP HTTP Status Codes
JSP Form Processing
JSP Filters
JSP Cookie Handling
JSP Session
JSP File Upload
JSP Date Processing
JSP Page Redirection
JSP Hit Counter
JSP Auto Refresh
JSP Sending Emails
JSP Advanced Tutorial
JSP Standard Tag Library (JSTL)
JSP Database Connection
JSP XML Data Processing
JSP JavaBean
JSP Custom Tags
JSP Expression Language
JSP Exception Handling
JSP Debugging
JSP Internationalization
Online Examples
·HTML Examples
·CSS Examples
·JavaScript Examples
·Ajax Examples
·jQuery Examples
·XML Examples
·Java Examples
Character Sets & Tools
· HTML Character Set Settings
· HTML ASCII Character Set
· JS Obfuscation/Encryption
· PNG/JPEG Image Compression
· HTML Color Picker
· JSON Formatting Tool
· Random Number Generator
Latest Updates
· Large Model Multimodal (M...
· Codex Advanced Configuration
· AI Agent Terminology
· ZCode Getting Started Tutorial
· Loop Engineerin...
· Claude Code Us...
· Claude Code Us...
Site Information
· Feedback
· Disclaimer
· About Us
· Article Archive
Follow WeChat
My Favorites
Mark Article
Browsing History
Clear All
No Records