简体   繁体   中英

is there any java/spring way to store session information for the current logged user using the HttpServletRequest?

I am wondering if I can set attributes on the HttpServletRequest object.

What I want to do is to store some information for the current logged user that I can later get back (on the same session).

I am using spring mvc.

So far I tried this

@RequestMapping(value = "/url1", method = RequestMethod.GET)
public void test1(final HttpServletRequest req, final ModelMap model) {
    List<String> myList = (List<String>)req.getAttribute("myList");
}

@RequestMapping(value = "/url2", method = RequestMethod.GET)
public void test2(final HttpServletRequest req, final ModelMap model) {
    String message = "hello world";
    List<String> messages = new ArrayList<String>();
    messages.add(messages);
    req.setAttribute("myList", messages);
}

So far, when I make the req.getAttribute I get a null... Any idea?

To setAttribute in session should be used like this:

 request.getSession().setAttribute("myList", messages);

And you can get it like this :

 request.getSession().getAttribute("myList");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM