Spring gets session and request at service level

Source: Internet
Author: User

There are two ways to get request:
  • 1. Add the listener to the Web. xml file and then inject the request via annotations:

    <listener> <listener-Class> org.springframework.web.context.request.requestcontextlistener </listener-class> </LISTENER>         
    @Autowired  private HttpServletRequest request; 
  • 2. Get directly in the service layer general class

    HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    Here's how to get the session:
  • Inject the HttpSession object at the service layer and get directly

    @Autowiredprivate HttpSession session;public void getsession (){ String name = (String) session.getAttribute("name");}

Spring gets session and request at service level

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.