How does spring MVC get the session value to the foreground, mvcsession

Source: Internet
Author: User

How does spring MVC get the session value to the foreground, mvcsession

Session Introduction

In WEB development, the server can create a session object (session Object) for each user's browser. Note: a browser exclusively occupies a session object (by default ). Therefore, when you need to save user data, the server program can write user data to the session exclusively occupied by the user's browser. When you use the browser to access other programs, other programs can extract the user data from the user's session to serve the user.

 

 

Obtain the session in the background:

@ RequestMapping ("/usrlogin ")
Public ModelAndView usrlogin (@ RequestParam String usrid,
@ RequestParam String passwd ){
HttpSession session = getSession ();
User user = new User ();
User = userMapper. getUserByUsridAndPasswd (usrid, passwd );
If (null! = User ){
Session. setAttribute ("user", user. getUser_id ());
// User user1 = (User) session. getAttribute ("user ");
// System. out. println (user1.getUser _ name ());

Return new ModelAndView ("main ");
} Else {
Return new ModelAndView ("error ");
}
}


Public static HttpSession getSession (){
HttpSession session = null;
Try {
Session = getRequest (). getSession ();
} Catch (Exception e ){}
Return session;
}

Public static HttpServletRequest getRequest (){
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder. getRequestAttributes ();
Return attrs. getRequest ();
}

 

Foreground jsp values:

 <%        HttpSession s = request.getSession();       %><span><%=s.getAttribute("user")%></span>



 

Related Article

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.