资源说明:目录
前言 ............................... 3
其他资料 ............................................................................................................................................................3
谁应该读此规范 ................................................................................................................................................3
API 规范 ............................................................................................................................................................3
其他的 Java 平台规范 .......................................................................................................................................3
其他重要参考资料 ............................................................................................................................................4
提供反馈 ............................................................................................................................................................4
专家组成员 ........................................................................................................................................................5
答谢 ....................................................................................................................................................................5
目录 ............................... 6
概览 .............................. 13
1.1 什么是 Servlet? ..................................................................................................................................13
1.2 什么是 Servlet 容器?...............................................................................................................................13
1.3 例子 ...........................................................................................................................................................13
1.4 Servlet 与其他技术的比较 ........................................................................................................................14
1.5 与 Java 平台企业版的关系 ......................................................................................................................14
1.6 与 Java Servlet 规范 2.5 版本间的兼容性 ..............................................................................................14
1.6.1 监听器(Listener)顺序 ...............................................................................................................14
1.6.2 注解处理 .........................................................................................................................................14
Servlet 接口 ...................... 15
2.1 请求处理方法 ...........................................................................................................................................15
2.1.1 基于 Http 规范的请求处理方法 ...................................................................................................15
2.1.2 附加的方法 ....................................................................................................................................15
2.1.3 有条件 GET 支持 ..........................................................................................................................16
2.2 实例数量 ...................................................................................................................................................16
2.2.1 关于 Single Thread Model..............................................................................................................16
2.3 Servlet 生命周期 .......................................................................................................................................16
2.3.1 加载和实例化 ................................................................................................................................16
2.3.2 初始化 ............................................................................................................................................16
2.3.3 请求处理 ........................................................................................................................................17
2.3.4 终止服务(End of Service) .........................................................................................................27
Request ........................... 29
3.1 HTTP 协议参数 .........................................................................................................................................29
3.1.1 当参数可用时 ................................................................................................................................29
3.2 文件上传 ....................................................................................................................................................30
3.3 属性 ...........................................................................................................................................................30
3.4 头 ...............................................................................................................................................................30
3.5 请求路径元素 ...........................................................................................................................................31
3.6 路径转换方法 ...........................................................................................................................................32
7
3.7 非阻塞 IO...................................................................................................................................................32
3.8 Cookies........................................................................................................................................................33
3.9 SSL 属性 ....................................................................................................................................................33
3.10 国际化 .....................................................................................................................................................34
3.11 请求数据编码 .........................................................................................................................................34
3.12 Request 对象的生命周期 ........................................................................................................................34
Servlet Context ................... 35
4.1 ServletContext 接口介绍 ...........................................................................................................................35
4.2 ServletContext 接口作用范围 ...................................................................................................................35
4.3 初始化参数 ...............................................................................................................................................35
4.4 配置方法 ...................................................................................................................................................35
4.4.1 编程式添加和配置 Servlet.............................................................................................................36
4.4.2 编程式添加和配置 Filter ...............................................................................................................36
4.4.3 编程式添加和配置 Listener............................................................................................................37
4.5 上下文属性 ...............................................................................................................................................39
4.6 资源 ...........................................................................................................................................................39
4.7 多主机和 Servlet 上下文 ..........................................................................................................................40
4.8 Reload 注意事项 .......................................................................................................................................40
4.8.1 临时工作目录 ................................................................................................................................40
Response .......................... 41
5.1 缓冲 ...........................................................................................................................................................41
5.2 头信息 .......................................................................................................................................................41
5.3 非阻塞 IO...................................................................................................................................................42
5.4 简便方法 ...................................................................................................................................................43
5.5 国际化 .......................................................................................................................................................43
5.6 结束响应对象 ...........................................................................................................................................44
5.7 Response 的生命周期 ................................................................................................................................44
过滤器 ............................ 45
6.1 什么是过滤器 ...........................................................................................................................................45
6.1.1 过滤器组件示例 ............................................................................................................................45
6.2 主要概念 ...................................................................................................................................................46
6.2.1 过滤器生命周期 ............................................................................................................................46
6.2.2 包装请求和响应 ............................................................................................................................47
6.2.3 过滤器环境 ....................................................................................................................................47
6.2.4 在 Web 应用中配置过滤器 ...........................................................................................................47
6.2.5 过滤器和 RequestDispatcher..........................................................................................................49
会话 .............................. 51
7.1 会话跟踪机制 ...........................................................................................................................................51
7.1.1 Cookies.............................................................................................................................................51
7.1.2 SSL 会话 .........................................................................................................................................51
7.1.3 URL 重写 .......................................................................................................................................51
7.1.4 会话完整性 ....................................................................................................................................52
7.2 创建会话 ...................................................................................................................................................52
8
7.3 会话范围 ...................................................................................................................................................52
7.4 绑定 Session 属性 .....................................................................................................................................52
7.5 会话超时 ...................................................................................................................................................53
7.6 最后访问时间 ...........................................................................................................................................53
7.7 重要的会话语义 .......................................................................................................................................53
7.7.1 多线程问题 ....................................................................................................................................53
7.7.2 分布式环境 ....................................................................................................................................53
7.7.3 客户端语义 ....................................................................................................................................54
注解和可插拔性 .................... 55
8.1 注解和可插拔性 .......................................................................................................................................55
8.1.1 @WebServlet....................................................................................................................................55
8.1.2 @WebFilter ......................................................................................................................................56
8.1.3 @WebInitParam ...............................................................................................................................56
8.1.4 @WebListener..................................................................................................................................56
8.1.5 @MultipartConfig............................................................................................................................57
8.1.6 其他注解/惯例 ...............................................................................................................................57
8.2 可插拔性 ...................................................................................................................................................57
8.2.1 web.xml 模块 ..................................................................................................................................57
8.2.2 web.xml 和 web-fragment.xml 顺序 ..............................................................................................58
8.2.3 装配 web.xml、web-fragment.xml 描述符和注解 .......................................................................63
8.2.4 共享库 / 运行时可插拔性 ...........................................................................................................72
8.3 JSP 容器可插拔性 .....................................................................................................................................74
8.4 处理注解和 fragment ................................................................................................................................74
分派请求 .......................... 75
9.1 获得一个 RequestDispatcher.....................................................................................................................75
9.1.1 请求调度器路径中的查询字符串 ................................................................................................75
9.2 请求调度器的使用 ...................................................................................................................................75
9.3 Include 方法 ...............................................................................................................................................76
9.3.1 内置请求参数 ................................................................................................................................76
9.4 Forward 方法 .............................................................................................................................................76
9.4.1 查询字符串 ....................................................................................................................................77
9.4.2 转发的请求参数 ............................................................................................................................77
9.5 错误处理 ...................................................................................................................................................77
9.6 获得一个异步上下文对象 .......................................................................................................................77
9.7 Dispatch 方法 .............................................................................................................................................78
9.7.1 查询字符串 ....................................................................................................................................78
9.7.2 调度请求参数 ................................................................................................................................78
Web 应用 .......................... 80
10.1 Web 服务器中的 Web 应用程序 .............................................................................................................80
10.2 与 ServletContext 的关系 .......................................................................................................................80
10.3 Web 应用的要素 ......................................................................................................................................80
10.4 部署层次结构 .........................................................................................................................................80
10.5 目录结构 .................................................................................................................................................80
9
10.5.1 应用程序目录结构示例 ......................................................................................................................81
10.6 Web 应用程序归档文件 ..........................................................................................................................82
10.7 Web 应用程序部署描述文件 ..................................................................................................................82
10.7.1 扩展的依赖关系 ..........................................................................................................................82
10.7.2 Web 应用程序类加载器 ...............................................................................................................83
10.8 更新 Web 应用 ........................................................................................................................................83
10.9 错误处理 .................................................................................................................................................83
10.9.1 请求属性 ......................................................................................................................................83
10.9.2 错误页面 ......................................................................................................................................84
10.9.3 错误过滤器 ..................................................................................................................................85
10.10 欢迎文件 ...............................................................................................................................................85
10.11 Web 应用程序环境 ................................................................................................................................86
10.12 Web 应用程序部署 ................................................................................................................................86
10.13 包含 web.xml 部署描述文件 ...............................................................................................................86
应用生命周期事件 .................. 87
11.1 介绍 .........................................................................................................................................................87
11.2 事件监听器 .............................................................................................................................................87
11.2.1 事件类型和监听器接口 ..............................................................................................................87
11.2.2 监听器使用的一个例子 ..............................................................................................................88
11.3 监听器类配置 .........................................................................................................................................88
11.3.1 提供监听器类 ..............................................................................................................................88
11.3.2 部署声明 ......................................................................................................................................88
11.3.3 监听器注册 ..................................................................................................................................89
11.3.4 关闭时通知 ..................................................................................................................................89
11.4 部署描述符示例 .....................................................................................................................................89
11.5 监听器实例和线程 .................................................................................................................................90
11.6 监听器异常 .............................................................................................................................................90
11.7 分布式容器 .............................................................................................................................................90
11.8 会话事件 .................................................................................................................................................90
映射请求到 Servlet................. 91
12.1 使用 URL 路径 .......................................................................................................................................91
12.2 映射规范 .................................................................................................................................................91
12.2.1 隐式映射 ......................................................................................................................................91
12.2.2 示例映射集合 ..............................................................................................................................92
安全 .............................. 93
13.1 介绍 .........................................................................................................................................................93
13.2 声明式安全 .............................................................................................................................................93
13.3 编程式安全 .............................................................................................................................................93
13.4 编程式安全策略配置 .............................................................................................................................94
13.4.1 @ServletSecurity 注解 ..................................................................................................................95
13.4.2 ServletRegistration.Dynamic 的 setServletSecurity.....................................................................101
13.5 角色 .......................................................................................................................................................101
13.6 认证 .......................................................................................................................................................102
10
13.6.1 HTTP 基本认证 ..........................................................................................................................102
13.6.2 HTTP 摘要认证 ..........................................................................................................................102
13.6.3 基于表单的认证 .........................................................................................................................102
13.6.4 HTTPS 客户端认证 ....................................................................................................................104
13.6.5 其他容器认证机制 ....................................................................................................................104
13.7 服务器跟踪认证信息 ...........................................................................................................................104
13.8 指定安全约束 .......................................................................................................................................104
13.8.1 组合约束 ....................................................................................................................................105
13.8.2 示例 ............................................................................................................................................105
13.8.3 处理请求 ....................................................................................................................................107
13.8.4 未覆盖的 HTTP 协议方法 ........................................................................................................107
13.9 默认策略 ...............................................................................................................................................109
13.10 登录和退出 ......................................................................................................................................... 110
部署描述文件 ..................... 111
14.1 部署描述文件元素 ............................................................................................................................... 111
14.2 部署描述符处理规则 ........................................................................................................................... 111
14.3 部署描述文件 ....................................................................................................................................... 112
14.4 部署描述文件图解 ............................................................................................................................... 112
1、web-app 元素 ................................................................................................................................... 112
2、description 元素 ............................................................................................................................... 113
3、display-name 元素 ........................................................................................................................... 113
4、icon 元素 .......................................................................................................................................... 114
5、distributable 元素 ............................................................................................................................. 114
6、context-param 元素 .......................................................................................................................... 114
7、filter 元素 ......................................................................................................................................... 114
8、filter-mapping 元素 .......................................................................................................................... 115
9、listener 元素 ..................................................................................................................................... 116
10、servlet 元素 .................................................................................................................................... 116
11、servlet-mapping 元素 ..................................................................................................................... 117
12、session-config 元素 ........................................................................................................................ 118
13、mime-mapping 元素 ...................................................................................................................... 118
14、welcome-file-list 元素 .................................................................................................................... 119
15、error-page 元素 .............................................................................................................................. 119
16、jsp-config 元素 ............................................................................................................................... 119
17、security-constraint 元素 .................................................................................................................120
18、login-config 元素 ...........................................................................................................................121
19、security-role 元素 ...........................................................................................................................121
20、env-entry 元素 ................................................................................................................................122
21、ejb-ref 元素 ....................................................................................................................................123
22、ejb-local-ref 元素 ...........................................................................................................................124
23、service-ref 元素 ..............................................................................................................................125
24、resource-ref 元素 ............................................................................................................................126
25、resource-env-ref 元素 ....................................................................................................................127
26、message-destination-ref 元素 .........................................................................................................128
27、message-destination 元素 ..............................................................................................................129
11
28、locale-encoding-mapping-list 元素 ................................................................................................130
14.5 实例 .......................................................................................................................................................131
14.5.1 一个简单的例子 ........................................................................................................................131
14.5.2 安全示例 ....................................................................................................................................132
15.与其它规范有关的要求 .......... 135
15.1 会话 .......................................................................................................................................................135
15.2 Web 应用程序 ........................................................................................................................................135
15.2.1 Web 应用程序类加载器 .............................................................................................................135
15.2.2 Web 应用程序环境 .....................................................................................................................135
15.2.3 Web 模块上下文根 URL 的 JNDI 名称 ....................................................................................136
15.3 安全性 ...................................................................................................................................................136
15.3.1 EJB™调用传播的安全标识 ......................................................................................................137
15.3.2 容器授权的要求 ........................................................................................................................137
15.3.3 容器认证的要求 ........................................................................................................................137
15.4 部署 .......................................................................................................................................................137
15.4.1 部署描述符元素 ........................................................................................................................137
15.4.2 打包和 JAX-WS 组件部署 .......................................................................................................138
15.4.3 处理部署描述符的规则 ............................................................................................................138
15.5 注解和资源注入 ...................................................................................................................................139
15.5.1 @DeclareRoles.............................................................................................................................140
15.5.2 @EJB 注解 ................................................................................................................................140
15.5.3 @EJBs 注解 ...............................................................................................................................141
15.5.4 @Resource 注解 ........................................................................................................................141
15.5.5 @PersistenceContext 注解 .........................................................................................................142
15.5.6 @PersistenceContexts 注解 .......................................................................................................142
15.5.7 @PersistenceUnit 注解 ..............................................................................................................142
15.5.8 @PersistenceUnits Annotation.....................................................................................................142
15.5.9 @PostConstruct 注解 .................................................................................................................142
15.5.10 @PreDestroy 注解 ...................................................................................................................143
15.5.11 @Resources 注解 .....................................................................................................................143
15.5.12 @RunAs 注解 ..........................................................................................................................144
15.5.13 @WebServiceRef 注解 ............................................................................................................144
15.5.14 @WebServiceRefs 注解 ...........................................................................................................145
15.5.15 JavaEE 要求的上下文和依赖注入 ..........................................................................................145
变更历史 ......................... 146
A.1 自 Servlet3.0 以后的变更 ......................................................................................................................146
A.2 自 Servlet 3.0 Proposed Final Draft 以后的变更 ..................................................................................147
A.3 自 Servlet 3.0 Public Review 以后的变更 ............................................................................................147
A.4 自 Servlet 3.0 EDR 以后的变更 ............................................................................................................148
A.5 自 Servlet 2.5 MR6 以后的变更 ............................................................................................................148
A.6 自 Servlet 2.5 MR 5 以后的变更 ...........................................................................................................148
A.6.1 明确 SRV 8.4“Forward 方法” ...................................................................................................148
A.6.2 更新部署描述符“http-method values allowed” ........................................................................148
A.6.3 明确 SRV 7.7.1 “多线程问题” ...................................................................................................149
12
A.7 自 Servlet 2.5 MR 2 以后的变更 ...........................................................................................................149
A.7.1 更新 JavaEE 容器注解的要求 ...................................................................................................149
A.7.2 更新 Java 企业版的要求 ............................................................................................................149
A.7.3 明确 HttpServletRequest.getRequestURL() ................................................................................149
A.7.4 从 HttpSession.getId()移除 IllegalStateException.......................................................................149
A.7.5 ServletContext.getContextPath()...................................................................................................150
A.7.6 web 应用中的 web.xml 的要求 ...................................................................................................150
A.8 自 Servlet 2.4 以后的变更 .....................................................................................................................150
A.8.1 Session 解释 .................................................................................................................................150
A.8.2 过滤所有分派 .............................................................................................................................151
A.8.3 多次出现的 Servlet 映射 ............................................................................................................151
A.8.4 多次出现 Filter 映射 ..................................................................................................................151
A.8.5 授权约束支持其他的 HTTP 方法 .............................................................................................152
A.8.6 最低 J2SE 要求 ...........................................................................................................................153
A.8.7 注解和资源注入 .........................................................................................................................153
A.8.8 移除了 SRV.9.9(“错误处理”)要求 .......................................................................................154
A.8.9 明确 HttpServletRequest.isRequestedSessionIdValid()...............................................................154
A.8.10 明确 SRV.5.5 (“结束响应对象”) ........................................................................................154
A.8.11 明确 ServletRequest.setCharacterEncoding()............................................................................154
A.8.12 Java 企业版要求 ........................................................................................................................154
A.8.13 新增了 Servlet 2.4 MR 更新的变更历史 ................................................................................154
A.8.14 明确同步访问 Session 对象 .....................................................................................................155
A.9 自 Servlet 2.3 以后的变更 .....................................................................................................................155
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。