[TOC]

# 1. out[javax.servlet.jsp.JspWriter]
## 問題01:如何使用out對象向網頁中輸出內容?

## 問題02:JspWriter對象和PrintWriter對象有什么區別?


## 問題03:如何解決JspWriter輸出順序與代碼不一致的問題?
> **設置緩沖區為0kb**

# 2. request[javax.servlet.http.HttpServletRequest]
| 分類 | 功能 | 應用 |
| --- | --- | --- |
| 行信息 | 獲取請求行(方式、協議、請求URI)| 限制請求方式和協議、轉換相對路徑|
| 頭信息 | 獲取請求頭(客戶端IP、referer)| 限制請求客戶端、實現防盜鏈|
| 主體 | 獲取請求參數|獲得單個值或者多個請求參數傳遞的值(單個、多個) |
| 編碼 | 設置請求編碼| 防止請求參數的值發生亂碼【重要】(post亂碼、get亂碼)|
| 視圖跳轉 | 請求轉發| 將請求遞交給下一個視圖,繼續進行處理(請求轉發、相對**web應用**路徑)|
## 問題04:如何獲取請求行信息?如何限制請求方式?
* [ ] getMethod() 獲得客戶端向服務器端傳送數據的方法,如get,post
* [ ] getProtocol()獲得客戶端向服務器端傳送數據所依據的協議名稱


## 問題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.

> **注意:得到的路徑最后沒有"/",需要手動添加。**
## 問題06:如何獲得其他頭信息?
* [ ] getHeader(String name) 獲得HTTP協議定義的文件頭信息
* [ ] getRemoteAddr()獲取客戶端的IP地址
* [ ] getServerPort() 獲取服務器的端口號

## 問題07:如何實現防盜鏈?


## 問題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**.

> 數組的顯示可以使用:<%=Arrays.toString(字符串數組)%>
## 問題09:如何解決請求亂碼?
* [ ] GET提交亂碼
```
String username = request.getParameter("username");
username = new String(username.getBytes("ISO8859-1"),"UTF-8");
```
* [ ] POST提交亂碼

```
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.

# 3. response[javax.servlet.http.HttpServletResponse]
| 分類 | 功能 | 應用 |
| --- | --- | --- |
| 行信息 | 設置響應行(狀態碼)| 設置正確狀態碼、設置異常狀態碼|
| 頭信息 |設置響應頭(refresh、禁止緩存) | 可以實現自動刷新和跳轉、實現禁止緩存|
| 主體 | 獲取out并輸出內容|獲取out對象,輸出內容print、append |
| 編碼 | 設置響應編碼| 設置響應內容類型以及編碼(通過header設置、直接設置)|
| 視圖跳轉 | 重定向|定向到其他視圖(重定向、相對**服務器**路徑)|
## 問題11:如何設置響應狀態碼?顯示錯誤消息頁面
* [ ] setStatus(int status)

> **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:如何手動喚醒錯誤處理頁面?


## 問題13:如何實現網頁自動跳轉或刷新?
* [ ] setHeader



```
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("");
```

## 問題16:如何解決響應內容出現亂碼?
```
response.setCharacterEncoding("UTF-8");
```

## 問題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.

> **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**.

## 問題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**.

* [ ] 注意:多次重定向可能產生以下異常

## 問題19:請求轉發和重定向有什么區別?

優點:可以轉到服務器內部路徑或不可直接訪問路徑,隱藏目標資源路徑。
缺點:可能發生相對路徑錯誤。
* [ ] 程序發生異常喚醒錯誤頁面也是屬于請求轉發的一種
* [ ] 當在錯誤頁面存在相對路徑時就可能出現異常


- 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-安全信息系統