Managing user sessions with ThreadLocal

Source: Internet
Author: User
Tags sessions

In the new project, between the screen and the function data transmission, uses the threadlocal, first turns to one, and so on has the time to reorganize again


Many projects need to use the information of the current logged-on user in the code, but it is not convenient to pass the session object that holds the user information,

In this case, you can consider using ThreadLocal.

Threadlocal is a variable attached to the local thread, and as I understand it, a thread is used every time a request is made to the server, and threadlocal is used only for this thread during the use of this thread.

Talk about how to manage user sessions specifically.

SSH framework now used more, sometimes in the DAO layer need to use the current user information, we first define the user in the HttpSession class, can be your user model, can also be nested model of the packaging class, I am called usersession.

In the login method, the user class object defined above is put into httpsession, namely: View plain Session.setattribute ("usersession", usersession);

Write a filter to verify the user, And in the Web.xml configuration, the purpose is to determine whether the user has logged in and the user has no access to the link to the permissions, and so on, each request through the filter if the HttpSession usersession object exists, reset the:  view Plain Public void dofilter (servletrequest req, servletresponse res, filterchain  filterchain)  throws IOException, ServletException {        HttpServletRequest request =  (httpservletrequest) req;           UserSession userSession =  (usersession) request.getsession (). getattribute (" Usersession ");          if  (usersession == null)  {            //Jump Login page             ...       } else {            request.getsession () setattribute ("Usersession",  usersession);   & nbsp;        filterchain.dofilter (req, res);        }  }  

Then create a class that holds the static variables for ThreadLocal: View Plain public class Systemsession {private static threadlocal<usersession& Gt          Local = new threadlocal<usersession> ();       public static void Setusersession (Usersession sessions) {Local.set (session);       public static Usersession Getusersession () {return local.get (); }   }

Create a listener and configure it in Web.xml to listen for httpsession property changes:

  View Plain public class myhttpsessionattributelistener implements  httpsessionattributelistener {       public void attributeadded ( httpsessionbindingevent event)  {           if  ("Usersession". Equals (Event.getname ())  {                systemsession.setusersession ((usersession) Event.getvalue ());            }       }           public void attributereplaced (httpsessionbindingevent event)  {           if  ("Usersession". Equals (Event.getname ())  {                systemsession.setusersession ( usersession) Event.getvalue ());            }       }  }   

When the user usersession the object into the httpsession and triggers the attributeadded method in the listener, the Usersession object is also placed in the threadlocal.  After logging on each request will reset the Usersession object, triggering the listener's Attributereplaced method, Usersession object will also be placed in Threadlocal, in the program with the following statement can get Usersession object: View plain systemsession.getusersession ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.