Several methods to obtain session in Spring MVC (Summary)

  • 2020-10-07 18:39:16
  • OfStack

Using session in Spring MVC is a common operation, but you can find the way to get session by searching on the Internet under 1, 5 and 8 doors. Recently, I finished 1 by myself, and recorded the way to get session, so that we can learn and make progress together.

The first is to pass HttpSession as a method parameter of Spring MVC and obtain it directly.

Directly pass the parameters in the method of Spring MVC:


public void getSessionAction(HttpSession session){
}

This method is not recommended by many people when I search on the Internet. However, it can be used in simple tests. The specific reasons for this method are not explained in many articles.

Second, HttpServletRequest is taken as the method parameter of Spring MVC and obtained indirectly

First, request is obtained, and then session is obtained indirectly through request. The code is as follows:


public void getSessionAction(HttpServletRequest request){
 HttpSession session = request.getSession();
}

This method is one of the most common, but some people find it troublesome because session requires passing in one parameter each time you use it (how lazy are you?), which is why there is a third method

Type 3: Via @Autowired HttpServletRequest request

This approach is similar to what we did when we injected the service class, which I rejected at first glance, because we all know that servlet USES singleton multithreading by default to handle multi-user requests. Wouldn't it be unsafe to just write it as a global variable? But here · spring annotated, so basically ensure thread safety. But to be honest, I was still worried, so I looked up some information online, and finally looked at the source code of spring (Which I will discuss in detail in another article), and finally found that it was completely feasible to do so.

Type 4: Use the RequestContextHolder class to get request and get session indirectly

By using the class RequestContextHolder, we can get request in this request without passing in HttpServletRequest. 1. In general use, we will encapsulate it and make it into a tool method for use:


((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

Strong attention to turn into ServletRequestAttributes, specific reasons can be found in the source (a little useless talk), general principle is that RequestContextHolder class has two ThreadLocal save request under the current thread, every time we call getRequestAttributes () when it comes to access to the current request, no incoming call springmvc method request, then this request is where did you get? Look at the source code can know, is in 1 processRequest(HttpServletRequest request, HttpServletResponse response) this method, each pick doget (), dopost () will use this method, our request, response to pass in.

Number 5: Use @SessionAttributes

This is not exactly a direct fetch to session, but we can put the values we want into session in this way.

In fact, the summary is a good habit, in the process of summarizing the method to obtain session, I saw more relevant blog posts, learned something unexpected, so I want to take it out, we have a discussion.


Related articles: