The application scope implements user login and squeezes out the previous login user code

3660 단어
1. Realize the idea
1. application (ServletContext) is a scope saved on the server side. We save two forms of key-value pairs in the application: 1:, 2:
2. Whenever a user logs in (a new session will be generated), first query the sessionId in the application according to the userId:
If the sessionId is not queried, it means that there is no user logged in to this account, then save the two data to the application.
If the sessionId is queried, it means that a user has already logged in, then the following three steps will be performed:
1) First get the session that has been logged in and make it invalid
2) Then delete the original session from the application and save the new session to the application ()
3) Finally, delete the original sessionId from the application and save the new sessionId to application()
Second, the encoding of the handler that implements the login function


@RequestMapping("/login")
 public String login(HttpServletRequest request, HttpServletResponse response, Map map) throws Exception{ 
  String userName = request.getParameter("userName");
  String password = request.getParameter("password");
  HttpSession session = request.getSession();
  ServletContext application = session.getServletContext(); // application
  
  User user = new User(userName, password);
  User currentUser = userService.login(user);
  if (currentUser == null) {
   request.setAttribute("error", " ");
   return "login";
  }
  
  String userId = String.valueOf(currentUser.getId()); // userId
  map.put("currentUser", currentUser); // user session , @SessionAttributes 
  if(application.getAttribute("userId") == null){ // 
   application.setAttribute(userId, session.getId()); // sessionId application
   application.setAttribute(session.getId(), session); // session application
  }else{ // 
   String sessionId = (String) application.getAttribute(userId); // userId sessionId
   HttpSession oldSession = (HttpSession) application.getAttribute(sessionId); // sessionId session
   oldSession.invalidate(); // oldSession 
   application.removeAttribute(oldSession.getId()); // oldSession application 
   application.setAttribute(session.getId(), session); // session application
   application.removeAttribute(userId); // oldSession id application 
   application.setAttribute(userId, session.getId()); // session Id application
  }
  return "main";
 }

3. Summary
1. Please pay attention to why it is specially used to save the sessionId?
Because, when the second user logs in, if we want to invalidate the session of the first user, we must get the sessionId of the first user, so we need to save the sessionId in the form to find the first user through the userId. The sessionId of each user, so as to find the session of the first user and invalidate it
The above is the whole content of this article about the application scope to achieve user login and squeeze out the previous login user code, I hope it will be helpful to everyone. Interested friends can continue to refer to this site: Java thread lock object Lock-synchronization problem code example, share a simple java crawler framework, the usage code example of list in java collection, etc., if you have any questions, you can feel free to Leave a message, I will reply you in time. Thank you friends for your support of this site!

좋은 웹페이지 즐겨찾기