[TOC]
![](https://img.kancloud.cn/3d/10/3d100eab93d16758a2699d2a7d61d348_1125x347.png)
# 1. out[javax.servlet.jsp.JspWriter]
## 问题01:如何使用out对象向网页中输出内容?
![](https://img.kancloud.cn/76/5e/765ee31e0bbf7786dcf95f958fde3eac_420x173.png)
## 问题02:JspWriter对象和PrintWriter对象有什么区别?
![](https://img.kancloud.cn/db/0e/db0e345cb91a6da49e63c3d2ff5e61e0_577x172.png)
![](https://img.kancloud.cn/e5/b7/e5b7d1853ee2b71af3fcef05826b7d40_657x427.png)
## 问题03:如何解决JspWriter输出顺序与代码不一致的问题?
> **设置缓冲区为0kb**
![](https://img.kancloud.cn/94/4f/944fda4ee5950f15fe98c5aae51038dc_828x287.png)
# 2. request[javax.servlet.http.HttpServletRequest]
| 分类 | 功能 | 应用 |
| --- | --- | --- |
| 行信息 | 获取请求行(方式、协议、请求URI)| 限制请求方式和协议、转换相对路径|
| 头信息 | 获取请求头(客户端IP、referer)| 限制请求客户端、实现防盗链|
| 主体 | 获取请求参数|获得单个值或者多个请求参数传递的值(单个、多个) |
| 编码 | 设置请求编码| 防止请求参数的值发生乱码【重要】(post乱码、get乱码)|
| 视图跳转 | 请求转发| 将请求递交给下一个视图,继续进行处理(请求转发、相对**web应用**路径)|
## 问题04:如何获取请求行信息?如何限制请求方式?
* [ ] getMethod() 获得客户端向服务器端传送数据的方法,如get,post
* [ ] getProtocol()获得客户端向服务器端传送数据所依据的协议名称
![](https://img.kancloud.cn/47/72/477240520b159c34e11afcdc0c5f8365_807x282.png)
![](https://img.kancloud.cn/91/87/918700fba5c6667fc3ee4fbeed50cda5_632x219.png)
## 问题05:如何转换相对路径?
```
String path = request.getContextPath();
```
> Returns the portion of the request URI that indicates **the context of the request**. The context path always **comes first** in a request URI. The path **starts with** a "/" character but **does not end with** a "/" character.
![](https://img.kancloud.cn/26/18/261852607208f81c4ea0a3ba8af06f76_702x129.png)
> **注意:得到的路径最后没有"/",需要手动添加。**
## 问题06:如何获得其他头信息?
* [ ] getHeader(String name) 获得HTTP协议定义的文件头信息
* [ ] getRemoteAddr()获取客户端的IP地址
* [ ] getServerPort() 获取服务器的端口号
![](https://img.kancloud.cn/ca/b9/cab9eb666de4a3836c6b01eb85960c22_839x654.png)
## 问题07:如何实现防盗链?
![](https://img.kancloud.cn/08/4d/084d33cf1d62d893f5b5f0dea3c27782_592x416.gif)
![](https://img.kancloud.cn/31/30/31302e030e2f33340a2f95e87ea1e013_856x195.png)
## 问题08:如何获得请求参数?
```
String username = request.getParameter("username");
```
Returns the value of a request parameter as a **String**, or **null** if the parameter does **not exist**. You should only use this method when you are sure the parameter **has only one value**. If the parameter might have **more than one value**, use **getParameterValues**(java.lang.String).
```
String[] users = request.getParameterValues("user");
```
Returns **an array of String objects** containing all of the values the given request parameter has, or null if the parameter does not exist. If the parameter has **a single value**, the array has **a length of 1**.
![](https://img.kancloud.cn/b4/d9/b4d9fecaeb0de864a6ee1864df4052bf_565x318.gif)
> 数组的显示可以使用:<%=Arrays.toString(字符串数组)%>
## 问题09:如何解决请求乱码?
* [ ] GET提交乱码
```
String username = request.getParameter("username");
username = new String(username.getBytes("ISO8859-1"),"UTF-8");
```
* [ ] POST提交乱码
![](https://img.kancloud.cn/26/d5/26d5696227d8b9d78d4a7fd4e3723ae3_565x318.gif)
```
request.setCharacterEncoding("utf-8");
```
> Overrides the name of the character encoding used in the body of this request. This method must be
called **prior** to **reading request parameters** or **reading input** using getReader().
## 问题10:如何进行视图跳转(请求转发)?
```
request.getRequestDispatcher("URL").forward(request, response);
```
> Returns a RequestDispatcher object that acts as a wrapper for **the resource located at the given path**.
The pathname specified **may be relative**, although it **cannot** extend **outside the current servlet context**. If the path **begins with a "/"** it isinterpreted as relative to the **current context root**.
> Forwards a request from a servlet **to another resource** (servlet, JSP file, or HTML file) on the server. This method allows one servlet **to do preliminary processing** of a request and **another resource to generate the response. **forward** should be called before the response has been committed** to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException.
![](https://img.kancloud.cn/69/e1/69e1970aca4c8449ba1e5d7a927aa8be_447x199.png)
# 3. response[javax.servlet.http.HttpServletResponse]
| 分类 | 功能 | 应用 |
| --- | --- | --- |
| 行信息 | 设置响应行(状态码)| 设置正确状态码、设置异常状态码|
| 头信息 |设置响应头(refresh、禁止缓存) | 可以实现自动刷新和跳转、实现禁止缓存|
| 主体 | 获取out并输出内容|获取out对象,输出内容print、append |
| 编码 | 设置响应编码| 设置响应内容类型以及编码(通过header设置、直接设置)|
| 视图跳转 | 重定向|定向到其他视图(重定向、相对**服务器**路径)|
## 问题11:如何设置响应状态码?显示错误消息页面
* [ ] setStatus(int status)
![](https://img.kancloud.cn/cb/e9/cbe9644e6e9cceb27b916bb7e7845f3b_401x178.png)
> **void javax.servlet.http.HttpServletResponse.setStatus(int sc)**
> **Sets the status code** for this response. This method is used to set the return status code when **there is no error** (for example, for the status codes SC_OK or SC_MOVED_TEMPORARILY). If **there is an error**, and the caller wishes to **invoke an error-page** defined in the web application, the **sendError** method should be used instead.
## 问题12:如何手动唤醒错误处理页面?
![](https://img.kancloud.cn/ab/f9/abf93cb58ce357049447a1fa49558097_779x453.png)
![](https://img.kancloud.cn/22/9a/229a2d0bfa3c0554d7317a63c124e7e6_1042x246.png)
## 问题13:如何实现网页自动跳转或刷新?
* [ ] setHeader
![](https://img.kancloud.cn/47/78/4778f3e347bd5e033c0f2a3c4e049c08_313x196.gif)
![](https://img.kancloud.cn/68/8b/688be66a9a62e2874dd4cd044d728951_1108x190.png)
![](https://img.kancloud.cn/83/00/83008309f144fb20e66167b7289e0bff_545x340.gif)
```
response.setHeader("refresh", "5;url='http://www.pzhu.cn'");
```
## 问题14:如何实现网页禁止缓存?
```
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
```
## 问题15:如何使用response实现输出内容?
```
response.getWriter().print("");
```
![](https://img.kancloud.cn/20/8f/208f965922b5215bd98f616cda44347d_630x246.png)
## 问题16:如何解决响应内容出现乱码?
```
response.setCharacterEncoding("UTF-8");
```
![](https://img.kancloud.cn/b3/83/b38308a6575a5544ffc5c2b00efd5eec_602x248.png)
## 问题17:为什么设置了编码,前端页面还是乱码?尝试切换浏览器编码?
> **void javax.servlet.ServletResponse.setCharacterEncoding(String charset)**
> **In the case of HTTP**, the character encoding is communicated **as part of the Content-Type header** for text media types. Note that the character encoding **cannot be communicated** via HTTP headers if **the servlet does not specify a content type**; however, it is still used to encode text written via the servlet response's writer.
![](https://img.kancloud.cn/6b/34/6b34fe03272138c285a27b7ea2d86161_483x302.png)
> **void javax.servlet.ServletResponse.setCharacterEncoding(String charset)**
**【解决办法】**
> **Sets the character encoding (MIME charset)** of the response being sent tothe client, for example, to UTF-8. If the character encoding has already been set by setContentType or setLocale, this method overrides it. Calling **setContentType** with the String of **text/html** and calling **this method** with the String of **UTF-8** is equivalent withcalling **setContentType** with the String of **text/html; charset=UTF-8**.
> This method can be called repeatedly to change the character encoding.This method has **no effect** if it is called **after getWriter** has been called or after **the response has been committed**.
![](https://img.kancloud.cn/39/f6/39f607bab526b94f338fd181cb60ecf4_675x319.png)
## 问题18:如何实现重定向?
```
response.sendRedirect("URL");
```
> Sends a temporary **redirect** response to the client using the specified redirect location **URL**. This method can **accept relative URLs**; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative **without a leading '/'** the container interprets it as relative to **the current request URI**. If the location is relative **with a leading '/' the** container interprets it as relative to **the servlet container root**.
![](https://img.kancloud.cn/c7/11/c711d09e0224fafb2fa975099b3d5d7d_468x279.png)
* [ ] 注意:多次重定向可能产生以下异常
![](https://img.kancloud.cn/11/69/1169f85517034aa95d002be73538e6bc_991x584.png)
## 问题19:请求转发和重定向有什么区别?
![](https://img.kancloud.cn/2d/3f/2d3f2986500f566b720d546ae903040c_700x387.png)
优点:可以转到服务器内部路径或不可直接访问路径,隐藏目标资源路径。
缺点:可能发生相对路径错误。
* [ ] 程序发生异常唤醒错误页面也是属于请求转发的一种
* [ ] 当在错误页面存在相对路径时就可能出现异常
![](https://img.kancloud.cn/8f/2f/8f2f4d136cbb95ed18a547c4650fb77f_690x149.png)
![](https://img.kancloud.cn/da/e4/dae4a9769b10e67912dc763ce98bf134_901x294.png)
- 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-安全信息系统