JSP(JavaServer Pages)是 Sun 定義的動態 Web 技術,本質是 Servlet 的擴展。最大特點:像寫 HTML 一樣寫動態頁面,在靜態模板中嵌入 Java 代碼片段,最終由容器翻譯為 Servlet 執行。
index.jsp。index_jsp.java。index_jsp.class。HttpJspBase → HttpServlet),調用 _jspService() 處理請求。
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements JspSourceDependent, JspSourceImports {
// 隱式對象聲明
private JspWriter out;
private HttpServletRequest request;
private HttpServletResponse response;
private HttpSession session;
private ServletContext application;
private ServletConfig config;
private PageContext pageContext;
private Object page = this;
private Throwable exception;
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// 初始化隱式對象
// 輸出靜態模板內容:out.write("...")
// 執行嵌入 Java 代碼:out.println(new Date().toLocaleString());
// 處理異常、釋放 PageContext
}
}
| 語法 | 格式 | 用途 | 翻譯位置 |
|---|---|---|---|
| 表達式 | <%= expression %> | 輸出值到響應體 | out.print(expression) |
| 腳本片段 | <% java statements %> | 執行任意 Java 代碼 | 直接放入 _jspService() 方法體 |
| 聲明 | <%! declarations %> | 定義類成員(字段、方法) | 類級別,不在 _jspService 內 |
| 註釋 | <%-- comment --%> | JSP 註釋,不輸出到客戶端 | 編譯時移除 |
| HTML 註釋 | <!-- comment --> | 客戶端可見註釋 | 原樣輸出 |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head><title>JSP Demo</title></head>
<body>
<%-- 這是 JSP 註釋 --%>
<% String user = "Admin"; %>
<h1>Welcome, <%= user %></h1>
<%!
private int counter = 0;
public int getCounter() { return ++counter; }
%>
<p>Page views: <%= getCounter() %></p>
</body>
</html>
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.*, java.text.*"
session="true"
buffer="16kb"
autoFlush="true"
isErrorPage="false"
errorPage="/error.jsp"
isELIgnored="false"
%>
常用屬性:import 導包、session 是否創建 Session、buffer 緩沖區大小、errorPage 異常轉發頁面、isELIgnored 是否忽略 EL 表達式。
<%@ include file="/common/header.jsp" %>
編譯階段合併文件,適合靜態片段(頭部、底部、側邊欄)。動態包含請用 <jsp:include page="..." />。
<%@ taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="https://java.sun.com/jsp/jstl/fmt" %>
引入標準標簽庫(JSTL)或自定義標簽庫,替代腳本片段,實現邏輯與視圖分離。
容器自動實例化,無需聲明即可在腳本片段/表達式中直接使用:
| 對象名 | 類型 | 作用域 | 主要用途 |
|---|---|---|---|
| out | JspWriter | page | 輸出響應內容,帶緩沖區 |
| request | HttpServletRequest | request | 獲取請求參數、頭、屬性 |
| response | HttpServletResponse | page | 設置狀態碼、頭、重定向、Cookie |
| config | ServletConfig | page | 獲取初始化參數 |
| session | HttpSession | session | 用戶會話數據存取 |
| application | ServletContext | application | 全局共享數據、資源路徑、屬性 |
| page | Object | page | 當前 Servlet 實例 |
| pageContext | PageContext | page | 頁面上下文,訪問所有作用域、轉發、包含 |
| exception | Throwable | page | 僅在 isErrorPage="true" 頁面可用,異常信息 |
pageContext.getOut() // → out
pageContext.getRequest() // → request
pageContext.getResponse() // → response
pageContext.getSession() // → session
pageContext.getException() // → exception
pageContext.getServletConfig()// → config
pageContext.getServletContext()// → application
pageContext.getPage() // → page
pageContext.setAttribute("key", val, PageContext.REQUEST_SCOPE); // 設置屬性
pageContext.findAttribute("key"); // 按作用域查找
<c:forEach>、<c:if>) + EL 表達式 (${user.name}) 實現邏輯視圖分離。.tag 文件,放入 /WEB-INF/tags/,通過 taglib 引入復用。