Spring 2.5 Four strategies for accessing session properties

Source: Internet
Author: User
Tags stub

WEB applications typically introduce a session to hold a sequence of action/message status between the server and the client, such as online shopping to maintain user login information until user exits. After user login, there are many actions in the session cycle that need to get user from the session, to verify identity rights, or to do other things. This will involve the program to access the session properties of the problem. In Java, the Servlet specification provides a HttpSession object to meet this requirement. Developers can get HttpSession from the Httpservletrquest object and then get state information from the HttpSession.

Or back to the shopping cart example, suppose that in controller a method (this is called action) we're going to take the user object from the httpsession. If you are based on a servlet, the standard code would be this:

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User user = (User)req.getSession().getAttribute("currentUser");
//
}
Such code is common in traditional servlet programs: The Servlet API is used to generate dependencies on the Servlet API. So if we want to test the action, we have to provide a mock or stub implementation for the HttpServletRequest, HttpServletResponse, and HttpSession classes. Of course there are already many open source Servlet test frameworks that help us mitigate this pain, including Spring's own stub implementations of these classes, but that's omissions trivial. There is no better way to make our controller more POJO, so that our action out of the Servlet API dependencies, more useful for testing and reuse it? Let's take a look at several strategies for accessing the session properties in Spring2.5, and continue to explore the deep meaning behind the solution selection in this blog's later articles.

(i) passing the HttpServletRequest object or HttpSession object via the method parameter

The author's previous article has simply introduced the Spring2.5 annotation makes controller out of the Servlet API restrictions on method parameters, here is not to repeat. Interested students can refer to here. Spring's parameter types that provide automatic binding support for annotationed action parameters include the request/response/httpsession in the Servlet API (including Request, Response the specific subclasses declared in the servlet API. The developer can then allow the container to inject the appropriate object by declaring the Request object or the HttpSession object in the action argument.

The action code is as follows:

@RequestMapping

public void hello(HttpSession session){
User user = (User)session.getAttribute("currentUser");
//
}
Advantages:

1. Directly and simply by using the methods defined in the Servlet API specification to manipulate the properties in these objects, the underlying Request/httpsession object is immediately available in the program.

2. The action needs to access which specific session attributes, which are controlled by themselves, truly accurate to each specific attribute in the session.

Insufficient:

1. The program relies on the Servlet API. Although the Controller class already does not need to inherit from HttpServlet, the Servlet API is still required to complete the compilation run and even test.

2. Exposing the underlying Servlet APIs exposes many of the underlying methods and classes that are not needed, which developers can easily misuse.

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.