[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對象取出來,就傳進去了
- 數據庫
- 數據庫介紹
- MySQL的安裝
- SQL
- 表基本操作
- 修改數據語句
- 數據檢索操作
- 多表數據操作
- 練習題
- JAVA
- JAVA 介紹
- JAVA 運行原理
- JDK 配置
- 類和對象
- 數據類型
- 變量
- 直接量
- 運算符
- 流程控制
- 數組結構
- 面向對象
- 隱藏和封裝
- 深入構造器
- 類的繼承
- 多態
- 包裝類
- final 修飾符
- 抽象類
- 接口
- 集合框架
- 常用類學習
- 設計模式-單例模式
- 異常處理
- JDBC
- JSP&Servlet
- Web應用
- Tomcat
- JSP
- Scriptlet
- Page 指令
- 包含指令
- 跳轉指令
- 用戶注冊實例
- JSP練習
- 內置對象
- Servlet
- 過濾器
- Web分層思想
- EL表達式
- JSTL
- 分頁實現
- AJAX&JSON
- 開發步驟
- 路徑問題
- Log4j
- Mybatis框架
- 框架介紹
- Mybatis簡單實現
- 表基本操作
- 優化配置文件
- 表字段名與實體類屬性名不同的解決方案
- 一對一關聯
- 一對多關聯
- Spring框架
- IOC/DI
- 注入對象
- 注解方式 IOC/DI
- AOP
- 注解方式AOP
- 注解方式測試
- Spring MVC框架
- Hello SpringMVC
- 視圖定位
- 注解方式
- 接受表單數據
- 客戶端跳轉
- Session
- 中文問題
- 上傳文件
- SSM整合
- 整合步驟
- 分頁
- PageHelper
- 連接池
- CRUD
- 事務管理
- JSON
- Maven
- 介紹
- 下載與配置MAVEN
- MAVEN倉庫
- ECLIPSE中的MAVEN設置
- ECLIPSE下創建MAVEN風格的JAVA項目
- 添加JAR包
- 創建MAVEN風格的JAVA WEB項目
- 創建SSM項目
- 使用ECLIPSE導入一個MAVEN風格的SSM項目
- 教學管理
- 學員名錄
- 周測統計
- 20180608
- 20180706
- 20180721
- 課堂作業
- 練習