[TOC]
# 1. 会话及会话技术
## 问题01:客户端存储和服务器存储的区别?
![](https://img.kancloud.cn/70/09/70096e9ea7625fd8596aa37b8fd12f7b_663x311.png)
![](https://img.kancloud.cn/08/2b/082b8e751ec60192b5af1ebdb08ecdff_622x323.png)
## 问题02:如何写入和读取cookie?【记录历史访问者】
![](https://img.kancloud.cn/d2/6a/d26ad86d8bea60f81ab89588979ff636_497x274.gif)
### 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:如何进行信息的增删改查?
![](https://img.kancloud.cn/7b/ce/7bce58499a13a688e4a92f208ff92b9c_467x254.png)
> **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如何获得其他存储对象?
![](https://img.kancloud.cn/ee/fc/eefc020486873f863a9dc4f6e04821b0_755x168.png)
> **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快速过期
![](https://img.kancloud.cn/83/3c/833c67cc3c1ce38cd6ec9354279cf1a9_1056x291.png)
```
session.setMaxInactiveInterval(1);
```
* [ ] 服务器统一设置过期时间
```
<session-config>
<session-timeout>min</session-timeout>
</session-config>
```
4. 使session立即失效
![](https://img.kancloud.cn/f4/b3/f4b351aff3adc43d844702eaafcc521c_901x269.png)
```
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} ```
![](https://img.kancloud.cn/7e/c5/7ec5fe5cb6691bd8117dfd65c471e7ba_568x242.png)
## 问题11:是否每个JSP都有Exception对象?
* [ ] 并不是
* [ ] 只有设置isErrorPage="true"才有内置对象exception
![](https://img.kancloud.cn/c8/e4/c8e48e22f6dbf022e84782830d4e337b_837x300.png)
## 问题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-安全信息系统