httpsession的基本使用

        /**
* 注销当前用户并返回首页.
*
* @param request
* @return
*/
@RequestMapping(value = "/logout")
public String logout(HttpServletRequest request) {

HttpSession session = request.getSession(false);
if (session != null) {
session.invalidate();
session = null;
}

return "redirect:/index";
}

         /**
* 请求登录的处理器.
*
* @param model
* @return
* @throws Exception
*/
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(
@RequestParam(value = "username") String username,
@RequestParam(value = "password") String password, HttpSession session) {

// 存储当前登录的用户信息
session.setAttribute("username", username);

// 登录成功
return "redirect:/??";
}

/**
* 进入首页, 也即登录页面.
*
* @return
*/
@RequestMapping(value = "/index")
public String index(HttpSession session) {

if (session.getAttribute("username") != null) {
// 如果用户已登录
return "redirect:/??";
}
return "/index";
}

你可能感兴趣的:(session,Httpsession)