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 introduces "how JAVA operates Session variables in JSP through Servlet". In daily operation, I believe many people have doubts about how JAVA operates Session variables in JSP through Servlet. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how JAVA manipulates Session variables in JSP through Servlet". Next, please follow the editor to study!
Control the session with Servlet
The maintenance of session state is a problem that must be faced in the development of Web applications. There are many ways to solve this problem, such as using form fields of Cookies,hidden type, or directly adding state information to URL, and Servlet itself provides a HttpSession interface to support session state maintenance. Here we mainly introduce session state management based on this interface.
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 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
/ / 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 Writer
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 / / get the name first
String value = session.getAttribute (name). ToString ()
/ / get the value from session by name
Out.println (name + "=" + value+ ""); / / print
}
Out.println (""); / / print the HTML tag
Out.println ("")
}
}
}
At this point, the study on "how JAVA manipulates Session variables in JSP through Servlet" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.