| 對象 | 類型 | 作用域 | 核心用途 |
|---|---|---|---|
| out | JspWriter | 頁面 | 輸出響應內容,緩衝區控制(flush()、clear()、getBufferSize()) |
| request | HttpServletRequest | 請求 | 獲取參數(getParameter)、屬性(set/getAttribute)、Header、IP、路徑 |
| response | HttpServletResponse | 頁面 | 設置狀態/Header/ContentType、重定向(sendRedirect)、獲取輸出流(getWriter) |
| session | HttpSession | 會話 | 用戶會話數據存取、生命週期管理、setMaxInactiveInterval |
| application | ServletContext | 應用 | 全局共享數據、資源路徑獲取、初始化參數 |
| page | Object (this) | 頁面 | 當前 JSP 頁面實例(即 Servlet 實例),極少直接使用 |
| pageContext | PageContext | 頁面 | 統一訪問其它 8 大對象、屬性跨作用域操作、forward/include |
| config | ServletConfig | 頁面 | Servlet 初始化參數(web.xml <init-param>) |
| exception | Throwable | 頁面 | 錯誤頁面 (isErrorPage="true") 中獲取異常信息 |
| 維度 | GET | POST |
|---|---|---|
| 數據可見性 | URL 明文 | 請求體,不可見 |
| 長度限制 | ~2KB (瀏覽器/服務器) | 無實質限制 (maxPostSize) |
| 安全性 | 低 | 高 |
| 效率 | 高 (可緩存/書籤) | 稍低 |
| 適用場景 | 查詢、搜尋、分頁 | 登錄、註冊、修改、上傳 |
| 特性 | 轉發 (request.getRequestDispatcher().forward()) | 重定向 (response.sendRedirect()) |
|---|---|---|
| 本質 | 服務器端內部跳轉 | 客戶端兩次請求 |
| 請求對象 | **共享** (保留參數/屬性) | **新建** (舊請求丟失) |
| URL 變化 | 不變 | 改變 (顯示目標地址) |
| 訪問範圍 | 同一 Web 應用內 | 任意 URL (含外部站點) |
| WEB-INF 訪問 | 可訪問 | **不可** (受保護資源) |
HttpSession。setMaxInactiveInterval(秒)、調用 invalidate())。setAttribute/getAttribute/removeAttribute、getId()、isNew()。out.println("內容"); // 寫入緩衝區
out.flush(); // 強制輸出到客戶端
out.clear(); // 清空緩衝區 (已 flush 拋異常)
out.clearBuffer(); // 清空緩衝區 (已 flush 不拋異常)
out.isAutoFlush(); // 緩衝區滿時自動 flush 或拋異常