Use JSP session objects to maintain the logon status.
Http itself is a stateless protocol, that is, there is no connection between multiple requests continuously sent by the client, and the next request does not care about the status of the last request.
In practice, the server is expected to remember the client request status, for example, in the Online Shopping System, the server should be able to identify and track the entire process of selecting and purchasing products for each user logged on to the system. Therefore, the web server must use a mechanism to uniquely identify a user and record the user's status. This requires session tracking technology.
Java Web uses sessions to track sessions and manage their statuses.
The Session object is a jsp built-in object. It is automatically created when the first jsp page is loaded to complete Session management.
A session starts when a client opens a browser and connects to the server, and ends when the client closes the browser and leaves the server. When a customer accesses a server, it may connect several pages of the server and refresh a page repeatedly. The server should know in some way that this is the same customer, this requires the session object.
When one of the following four situations occurs, the data in the session object is cleared:
The user closes the browser program currently in use.
Disable the Web server.
The user has not submitted a request to the server for more than 30 minutes.
Run the program to end the session.
1. Create a session variable
In JSP, no special program code is required to create a user session. When the program uses a session object, the session is automatically created, the following statement adds variable data to the session:
Session. setAttribute ("variable name", variable content)
The variable content can be a string or other object type. Let's take a look at how to use this method to set variable data in the session:
<% Session. setAttribute ("id", "Number"); // sets the string session. setAttribute ("expire", new Date (86400*10); // set the Date session. setAttribute ("level", new Integer (3); // set Integer %>
2. Return the variables in the session.
After variable data is set in the session, you can use getAttribute to read the content on other webpages. The data type returned by this method is Object. The syntax is as follows:
Session. getAttribute ("variable name ")
3. Return the variable names in all sessions.
The getAttributeNames () method can retrieve the names of all variables in the session. The result is an instance of the enumeration class. Syntax:
Session. getAttributeNames ()
4. Clear the variables in the session.
The removeAttribute () method can clear the variable data in the session. The syntax is as follows:
Session. removeAttribute ("variable name ")
5. End the session
For a session that has been created, you can end it by using the invalidate () method. The syntax is as follows:
Session. invalidate ()
Other methods may be used:
Write an instance: Use session to record the logon status of the customer:
Index. jsp logon interface:
<% @ Page contentType = "text/html; charset = UTF-8 "language =" java "%>
Process_login.jsp processes logon data. If the password entered is 123, the logon is successful:
Session. getAttribute () will tell the page1.jsp file whether the user has successfully logged on.
<%@ page contentType="text/html;charset=UTF-8" language="java" %><% String username=request.getParameter("username"); String password=request.getParameter("password"); if (password.equals("123")){ session.setAttribute("username",username); } response.sendRedirect("page1.jsp");%>
Logout. jsp logout
<%@ page contentType="text/html;charset=UTF-8" language="java" %><% session.invalidate(); response.sendRedirect("index.jsp");%>
Page1.jsp, page2.jsp, and page3.jsp display different pages for verifying logon status records (Here we only use page1.jsp as an example ):
Note that the judgment logic here is a very interesting way of writing. It fully integrates jsp code and html code. However, I think it is still messy to write this code. I would rather use only one jsp code segment, use out. println () is used to display different contents in html.
Here, we mainly rely on checking whether session. username is null to determine whether the user has logged on and passed related parameter information.
<% @ Page contentType = "text/html; charset = UTF-8 "language =" java "%>
As you can see, no matter which page you jump to after logon, the user's logon status is not lost.
Once logout logs out, the session. invalidate () method is called and the session is destroyed, so the user's logon information cannot be tracked.
At the same time, if I access the same page using another browser, log on with username as "user 2, the user's login information will be recorded all the time (but it won't work if it's the same browser)
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.