Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The introduction and usage of Session in Java

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In view of the introduction and usage of Session in Java, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

1. What is session:

The server creates a session object for each user (browser)

A session monopolizes a browser. As long as the browser is not closed, the session will always exist.

Session, called session control, stores the attributes and configuration information needed by the user's session, so that when the user jumps to the web page of the application, the user information stored in the session object is not thrown, but is maintained throughout the user session.

Classic application scenarios of 2.session:

After logging in, a user has been logged in when he visits other pages of the site.

Save shopping cart information, etc.

Methods commonly used in 3.session

GetId (): get the unique identity of the session

GetServletContext (): represents the entire web service

GetAttribute (String): get the node of session. For example, if you configure the relevant session information through setAttribute (String,Object) in one of the Servlet, and then obtain the session information in this Servlet through another Servlet, you need to use the getAttribute (String) method.

SetAttribute (String,Object): setting up the session node

RemoveAttribute (String): one out of a session node

IsNew (): determines whether a session is a new session

Invalidate (): log out of session

4. Give an example

Example 1: create a session and get the ID of session to determine whether the session is a new session

Step 1: create a new maven project, using the webapp template

Step 2: create a com.xiaoma.servlet.SessionDemo01 class under the Java folder and write the servlet class file

Package com.xiaoma.servlet;import com.xiaoma.pojo.Person;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionDemo01 extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {/ / resolve Chinese garbled resp.setHeader ("content-type", "text/html") Charset=UTF-8 "); / / get session HttpSession session = req.getSession (); / / store information session.setAttribute (" name "," pony ") in Session through setAttribute method; / / obtain session-related information, including id, whether it is a new session, etc. String sessionId = session.getId () If (session.isNew ()) {resp.getWriter () .write ("Session created successfully, ID is:" + sessionId);} else {resp.getWriter () .write ("Session already exists, ID is:" + sessionId);}} @ Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet (req, resp);}}

Step 3: configure the XML file

SessionDemo01 com.xiaoma.servlet.SessionDemo01 SessionDemo01 / s1

Example 2: reading session information from one servlet to another servlet

Step 1: create a com.xiaoma.servlet.SessionDemo01 class under the Java folder and write the servlet class file

Package com.xiaoma.servlet; import com.xiaoma.pojo.Person; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionDemo01 extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {/ / resolve Chinese garbled resp.setHeader ("content-type", "text/html") Charset=UTF-8 "); / / get session HttpSession session = req.getSession (); / / store information session.setAttribute (" name "," pony ") in Session through setAttribute method; / / obtain session-related information, including id, whether it is a new session, etc. String sessionId = session.getId () If (session.isNew ()) {resp.getWriter () .write ("Session created successfully, ID is:" + sessionId);} else {resp.getWriter () .write ("Session already exists, ID is:" + sessionId);}} @ Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet (req, resp);}}

Step 2: create a com.xiaoma.servlet.SessionDemo02 class under the Java folder, write another servlet class to read, and read the session information in SessionDemo01 through SessionDemo02

Package com.xiaoma.servlet; import com.xiaoma.pojo.Person; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionDemo02 extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {/ / resolve Chinese garbled resp.setHeader ("content-type", "text/html") Charset=UTF-8 "); / / get session HttpSession session = req.getSession (); String name= (String) session.getAttribute (" name "); resp.getWriter (). Write (name);} @ Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost (req, resp);}}

Step 3: configure the xml file

SessionDemo02 com.xiaoma.servlet.SessionDemo02 SessionDemo02 / s2

Object information can also be stored in the example 3:session

Step 1: create a com.xiaoma.pojo.Person class under the Java folder and write our entity class

Package com.xiaoma.pojo; public class Person {private String name; private int age; public Person () {} public Person (String name, int age) {this.name = name; this.age = age;} public String getName () {return name;} public void setName (String name) {this.name = name } public int getAge () {return age;} public void setAge (int age) {this.age = age;} @ Override public String toString () {return "Person {" + "name='" + name +'\'+ ", age=" + age +'};}}

Step 2: create a com.xiaoma.servlet.SessionDemo01 class under the Java folder and write the servlet class file

Package com.xiaoma.servlet; import com.xiaoma.pojo.Person; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionDemo01 extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {/ / resolve Chinese garbled resp.setHeader ("content-type", "text/html") Charset=UTF-8 "); / / get session HttpSession session = req.getSession (); / / store information session.setAttribute (" name ", new Person (" pony ", 18)) in Session through setAttribute method; / / obtain session-related information, including id, whether it is a new session, etc. String sessionId = session.getId () If (session.isNew ()) {resp.getWriter () .write ("Session created successfully, ID is:" + sessionId);} else {resp.getWriter () .write ("Session already exists, ID is:" + sessionId);}} @ Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet (req, resp);}}

Step 3: create a com.xiaoma.servlet.SessionDemo02 class under the Java folder and write another servlet class file to read the Session object information in SessionDemo01

Package com.xiaoma.servlet; import com.xiaoma.pojo.Person; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;import java.io.IOException Public class SessionDemo02 extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {/ / get the Person object in session / / resolve Chinese garbled resp.setHeader ("content-type", "text/html;charset=UTF-8"); / / get session HttpSession session = req.getSession (); Person person= (Person) session.getAttribute ("name") Resp.getWriter () .write (person.toString ()); @ Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost (req, resp);}}

Step 4: write the xml file, same as above

Example 4: log out of session (there are two ways to log out of session, one is through the removeAttribute (String) method, the other is through the xml configuration file)

First, through the removeAttribute (String) method, which is to make session expire manually

Package com.xiaoma.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionDemo03 extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {/ / get session HttpSession session = req.getSession () / / manually log out of session session.removeAttribute ("name"); session.invalidate ();} @ Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost (req, resp);}}

Second, through the xml file, which is when the session expires automatically

1 the difference between 5.session and cookie

(1) cookie, right? write the user's data to the browser and save it by the browser (multiple can be saved)

(2) session is to write the user's data to the user's exclusive session, and the server saves (saves important information to avoid wasting server resources)

(3) session is created by the server

This is the answer to the question about the introduction and usage of Session in Java. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report