[TOC]
# Session
Session在用戶登錄,一些特殊場合在頁面間傳遞數據的時候會經常用到
## 步驟 1 : 先運行,看到效果,再學習
先將完整的項目(向老師要相關資料),配置運行起來,確認可用之后,再學習做了哪些步驟以達到這樣的效果。
## 步驟 2 : 模仿和排錯
在確保可運行項目能夠正確無誤地運行之后,再嚴格照著教程的步驟,對代碼模仿一遍。
模仿過程難免代碼有出入,導致無法得到期望的運行結果,此時此刻通過比較**正確答案** ( 可運行項目 ) 和自己的代碼,來定位問題所在。
采用這種方式,**學習有效果,排錯有效率**,可以較為明顯地提升學習速度,跨過學習路上的各個檻。
## 步驟 3 : 效果
訪問路徑/check,可以不停刷新session中記錄的訪問次數

## 步驟 4 : 修改IndexController
映射 /check 到方法check()
為方法check()提供參數HttpSession session,這樣就可以在方法體中使用session了
接下來的邏輯就是每次訪問為session中的count+1.
最后跳轉到check.jsp頁面
~~~
package com.dodoke.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController {
@RequestMapping("/index")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("index");
mav.addObject("message", "Hello SpringMVC");
return mav;
}
@RequestMapping("/jump")
public ModelAndView jump() {
ModelAndView mav = new ModelAndView("redirect:index");
return mav;
}
@RequestMapping("/check")
public ModelAndView check(HttpSession session) {
Integer i = (Integer) session.getAttribute("count");
if (null == i) {
i = 0;
}
i++;
session.setAttribute("count", i);
ModelAndView mav = new ModelAndView("check");
return mav;
}
}
~~~
## 步驟 5 : check.jsp
在WEB-INF/page中新建check.jsp頁面
~~~
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
session中記錄的訪問次數:${count }
</body>
</html>
~~~
## 步驟 6 : 測試
訪問頁面
`http://localhost:8080/springmvc/check`
每次訪問,次數都+1

## 步驟 7 : 練習
配置一個路徑 /clear
訪問該路徑后,清空session中的count. 并且客戶端跳轉到/check
~~~
package com.dodoke.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController {
@RequestMapping("/index")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("index");
mav.addObject("message", "Hello SpringMVC");
return mav;
}
@RequestMapping("/jump")
public ModelAndView jump() {
ModelAndView mav = new ModelAndView("redirect:index");
return mav;
}
@RequestMapping("/check")
public ModelAndView check(HttpSession session) {
Integer i = (Integer) session.getAttribute("count");
if (null == i) {
i = 0;
}
i++;
session.setAttribute("count", i);
ModelAndView mav = new ModelAndView("check");
return mav;
}
@RequestMapping("/clear")
public ModelAndView clear(HttpSession session) {
Integer i = (Integer) session.getAttribute("count");
if (null == i) {
ModelAndView mav = new ModelAndView("check");
return mav;
}
session.removeAttribute("count");
ModelAndView mav = new ModelAndView("check");
return mav;
}
}
~~~
## 常見問題
1. 為什么HttpSession可以直接傳到check方法里面
> 對于一個J2EE應用,HttpSession是一直存在的,Spring MVC框架把當前的session對象取出來,就傳進去了