In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to realize the session state of Session in JAVA". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the session state of Session in JAVA".
Session was invented to fill the limitations of the HTTP protocol. Notice how the HTTP protocol works-the user makes a request and the server responds, and the connection between the client and the server 97 color side is discrete and discontinuous. The HTTP protocol does not provide functionality that allows the server to track user requests. After the server has finished responding to the user's request, the server cannot continue to connect to the browser. From the server side, each request is independent, so the HTTP protocol is considered to be stateless. When a user switches between multiple home pages, the server cannot know his identity. The emergence of Session is to make up for this limitation. With Session, you can save a user's information when he or she switches between multiple homepages. This makes it much easier to do a lot of things that can't be done before.
During the period between arriving at a particular home page and leaving, each visitor gets a separate Session.
Java Servlet defines a HttpSession interface to implement the function of Session. The process of using Session in Servlet is as follows:
(1) use the getSession method of HttpServletRequest to get the existing session. If no session is currently defined, create a new session. You can also use the method getSession (true).
(2) write session variable. You can use the method HttpSession.setAttribute (name,value) to store a piece of information in Session. You can also use HttpSession.putValue (name,value), but this method is out of date.
(3) read Session variable. You can use the method HttpSession.getAttribute (name) to read the value of a variable in Session, and if name is an undefined variable, null is returned. It should be noted that the variable type read from getAttribute is Object, and you must use a cast, such as:
String uid = (String) session.getAttribute ("uid")
You can also use HttpSession.getValue (name), but this method is also out of date.
(4) close session. After running out of session, you can use the session.invalidate () method to close session. But this is not strictly required. Because the Servlet engine automatically shuts down seesion after a period of time.
Here is a simple example to illustrate the use of session
/ / 97 Color 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 headers
PrintWriter out = response.getWriter (); / / get the output 97gan
HttpSession session = request.getSession (true)
/ / get the session object
/ / 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 creation time of session
Out.println ("Last Accessed:" + accessed+ "
")
/ / print the last visit time
Session.setAttribute ("UID", "12345678")
/ / add the variable UID=12345678 to session
Session.setAttribute ("Name", "Tom")
/ / add the variable Name=Tom to session
Enumeration e = session.getAttributeNames ()
/ / get the enumeration object of variable names in session
While (e.hasMoreElements ()) {/ / traverses each variable
String name = (String) e.nextElement (); / / get the name first
String value = session.getAttribute (name). ToString ()
/ / get the value from 97gan by name
Out.println (name + "=" + value+ "
}
Out.println (""); / / print the HTML tag
Out.println ("")
}
}
}
Thank you for reading, the above is the content of "how to realize the session state of Session in JAVA". After the study of this article, I believe you have a deeper understanding of how to realize the session state of Session in JAVA, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.