Several methods for obtaining sessions in Spring MVC, mvcsession

Source: Internet
Author: User

Several methods for obtaining sessions in Spring MVC, mvcsession

Using session in Spring MVC is a common operation. However, when you search for a session on the Internet, you can see that there are a variety of methods to obtain the session. Recently, I ended it myself, record the method for obtaining the session so that everyone can learn and make progress together.

First, pass HttpSession as the method parameter of Spring MVC and obtain it directly.

Directly input parameters in the Spring MVC method:

public void getSessionAction(HttpSession session){}

This method is not recommended to many people when I search for it online. However, it is still usable in simple tests. The reason for not recommending this method is not described in many articles.

Type 2: Use HttpServletRequest as the method parameter of Spring MVC to indirectly obtain

First, get the request, and then indirectly get the session through the request. The Code is as follows:

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

This method is a common one, but some people may think this is quite troublesome, because each time you use a session, you need to input a parameter (how laZy are you ), so there is a third method.

Method 3: Get it through @ Autowired HttpServletRequest request

This method is the same when we inject the service class. At first glance, I refused this method because we all know that, by default, servlet processes multi-user requests in a single-sample and multi-thread mode. Is it unsafe to write a global variable directly? But here we use spring annotations to ensure thread security. But to be honest, I still don't feel relieved, so I checked some information on the Internet and finally looked at the source code of spring (I will discuss it in detail in another article ), finally, it is completely feasible to do so.

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

By using the RequestContextHolder class, we can directly obtain the request in this request without passing in HttpServletRequest. In general, we will encapsulate it and use it as a tool:

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

Pay attention to the forced conversion to ServletRequestAttributes. The specific cause can be found in the source code (a bit nonsense). The general principle is that two ThreadLocal in the RequestContextHolder class stores the request under the current thread, every time we call getRequestAttributes (), we get the current request. The request is not passed in when the springmvc method is called. Where can we get this request? You can see the source code.processRequest(HttpServletRequest request, HttpServletResponse response)In this method, every time we use doget () or dopost (), we will use this method to upload our request and response.

Category 5: @ SessionAttributes

Specifically, this method does not directly obtain the session, but we can put the value we want into the session.

 

In fact, summary is a good habit. In the process of summing up the method for obtaining sessions, I saw many related blog posts and learned some unexpected things. So I want to share them and discuss them together, please advise if you have any more information or errors.

 

 

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.