[TOC]
# 1. 會話及會話技術
## 問題01:客戶端存儲和服務器存儲的區別?


## 問題02:如何寫入和讀取cookie?【記錄歷史訪問者】

### cookie的寫入
```
Cookie cookie = new Cookie("name","value");
response.addCookie(cookie);
```
> **void javax.servlet.http.HttpServletResponse.addCookie(Cookie cookie)**
> Adds the specified cookie to the response. This method can be called multiple times to set more than
one cookie.
### cookie的讀取
```
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(Cookie cookie:cookies){
String name = cookie.getName();
String value = cookie.getValue();
}
}
```
> **Cookie[] javax.servlet.http.HttpServletRequest.getCookies()**
> Returns **an array** containing all of the Cookie **objects** the client sent with this request. This method
**returns null if no cookies were sent**.
> **String javax.servlet.http.Cookie.getName()**
> Returns the name of the cookie. The name **cannot be changed after creation**.
> **String javax.servlet.http.Cookie.getValue()**
> Returns the value of the cookie.
> **void javax.servlet.http.Cookie.setValue(String newValue)**
> Assigns **a new value** to a cookie after the cookie is created. With Version 0 cookies, values **should not contain** white space, brackets, parentheses, equals signs, commas, double quotes, slashes, question marks, at signs, colons, and semicolons. Empty values may not behave the same way on all browsers.
> **(不支持特殊字符及中文)**
## 問題03:服務器存儲對象有哪些?page、request、session、application
|對象名稱|所屬類型|范圍|
| -- | -- | -- |
|page| java.lang.Object | page|
|request | javax.servlet.http.HttpServletRequest | request |
|session | javax.servlet.http.HttpSession |session |
| application | javax.servlet.ServletContext | application |
> 為什么需要域對象?
* [ ] MVC各個模塊之間需要進行相互通信,特別是JSP與Servlet、JSP與JSP、Servlet與Servlet之間
## 問題04:為什么需要這么多存儲對象?
* [ ] 適用于不同場景
* [ ] 因為request作用域太小
* [ ] 一般我們使用session,因為application作用域太大
## 問題05:這些存儲對象有什么區別?
|對象名稱|特點|應用場景|
| -- | -- | -- |
| page| 只在當前頁面有效 | 除當前頁面需要立即顯示數據,一般不使用 |
| request | 只在當前請求有效,即相同request | 除請求轉發時,一般不使用 |
| session | 一次會話有效,即沒有與服務器斷開連接 | 最常使用,非所有用戶共享數據結皆能存儲 |
| application | 整個服務器運行期間有效,所有用戶共享 | 一般用于保存服務器配置信息,通常不使用 |
## 問題06:如何進行信息的增刪改查?

> **void javax.servlet.ServletRequest.setAttribute(String name, Object o)**
> **Stores an attribute** in this request. **Attributes are reset** between requests. This method is most often used in conjunction with RequestDispatcher. Attribute **names** should follow **the same conventions as package names**. If the object passed in is **null**, the effect is the same as calling **removeAttribute**.
---
> **void javax.servlet.ServletRequest.removeAttribute(String name)**
> **Removes** an attribute from this request.
---
> **Object javax.servlet.ServletRequest.getAttribute(String name)**
> Returns the value of the named attribute **as an Object**, or **null** if **no** attribute of the given name **exists**.
> 注意:getAttribute()因為得到的值為null或object,因此可能引發**空指針異常**或者**類型轉換異常**
## 問題07:通過request如何獲得其他存儲對象?

> **HttpSession javax.servlet.http.HttpServletRequest.getSession(boolean create)**
> Returns **the current HttpSession** associated with this request or, if there is **no current session** and **create is true**, returns **a new session**. If **create is false** and the request has no valid HttpSession, this method returns **null**.
> **HttpSession javax.servlet.http.HttpServletRequest.getSession()**
> Returns the current session associated with this request, or if the request does **not have a session, creates one**.
## 問題08:為什么不直接使用session和application,而是需要從request中得到?
* [ ] 因為在Servlet中沒有內置對象,在Service方法(運行方法)中只有request參數作為的對象
## 問題09:如何銷毀session或使session中的某一個特殊標記失效?
1. 刪除session中的屬性
```
session.removeAttribute("key");
```
2. 使用空串覆蓋session中的屬性
```
session.setAttribute("key","");
```
3. 設置session快速過期

```
session.setMaxInactiveInterval?(1);
```
* [ ] 服務器統一設置過期時間
```
<session-config>
<session-timeout>min</session-timeout>
</session-config>
```
4. 使session立即失效

```
session.invalidate();
```
# 2. 其他內置對象page/pageContext/config/exception
|對象名稱|所屬類型|范圍|
| -- | -- | -- |
| page | javax.servlet.jsp.HttpJspPage | page |
| pageContext |javax.servlet.jsp.PageContext | page|
| config |javax.servlet.ServletConfig | page|
| exception|java.lang.Throwable | page|
## 問題10:pageContext有什么作用?
* [ ] 獲得其他內置對象,特別是request
* [ ] ``` ${pageContext.request.ContextPath} ```

## 問題11:是否每個JSP都有Exception對象?
* [ ] 并不是
* [ ] 只有設置isErrorPage="true"才有內置對象exception

## 問題12:九個內置對象分別屬于什么對象?即內置對象分類。
1. 錯誤對象:exception
2. servlet對象:page、config
3. 輸入輸出對象:request、response、out
4. 通信對象:session、application、pageContext
- 1課程概述
- 2環境配置
- 3MVC
- 3.1View
- 3.1.1前端基礎
- 3.1.2JSP語法
- 3.1.3JSP內置對象1
- 3.1.4JSP內置對象2
- 3.2Bean
- 3.3Controller
- 3.3.1Servlet
- 3.3.2Filter
- 3.3.3Listener
- 3.4EL&JSTL
- 4三層架構
- 4.1數據庫操作
- 4.1.1JDBC
- 4.1.2JDBC優化
- 4.2三層架構設計
- 4.3程序優化
- 4.3.1數據庫連接優化
- 4.3.2數據庫操作優化
- 4.4安全專題
- 4.4.1Ajax異步查詢
- 4.4.2CAPTCHA
- 4.4.3MD5&SHA
- 4.4.4Cookie
- 4.4.5分頁顯示
- 4.4.6文件上傳
- 4.4.7發送郵件
- 5企業級框架
- 5.0Maven
- 5.1MyBatis
- 5.2Spring
- 5.3SpringMVC
- 6實踐項目
- 6.1實驗1-用戶登錄(MVC)
- 6.2實驗2-訪問統計(Servlet高級)
- 6.3實驗3-三層架構
- 6.4實驗4-安全信息系統