SpringMVC下设置和取得session

建立sessioon

    /**
 * 登录
 * @param c_username
 * @param c_password
 * @return
 */
@RequestMapping("/login")
public String login(
        @RequestParam(value = "username",required = true)String c_username,
        @RequestParam(value = "password",required = true)String c_password,
        HttpSession session){
    Users users=new Users();
    users.setUsername(c_username);
    users.setPsw(c_password);
    if(loginService.login(users)){
        //登录成功
        session.setAttribute("user",users);
        System.out.println("登录成功");
    }
    else {
        System.out.println("登录失败");
        return "forward:/login/index";
    }
    return "forward:/index/show";

}

取得session

@RequestMapping("/show")
public void cshow(HttpSession session){
    Users u=(Users) session.getAttribute("user");
    System.out.println(u.getUsername());
}