JSP 转译成 Servlet 之后,容器会准备好九个隐含对象,页面里不用声明就能直接用。它们分别对应 page、config、request、response、out、session、application、pageContext、exception。

记的时候按作用域想:page 只对当前页,request 一次请求,session 一次会话,application 整个应用;pageContext 能跨这四个范围找属性。
| 对象 |
对应类型 |
作用域 / 用途 |
| page |
Object(当前 Servlet 实例) |
代表本页转译后的 Servlet |
| config |
ServletConfig |
读本页 Servlet 的初始化参数 |
| request |
HttpServletRequest |
请求头、参数、Cookie |
| response |
HttpServletResponse |
写回状态码、头、重定向 |
| out |
JspWriter |
向页面输出、管理缓冲区 |
| session |
HttpSession |
当前用户会话 |
| application |
ServletContext |
应用级环境信息 |
| pageContext |
PageContext |
拿其他隐含对象、跨范围找属性 |
| exception |
Throwable |
仅错误页 isErrorPage=true |
一、page 对象
page 代表 JSP 本身,更准确地说是 JSP 被转译后的 Servlet,可以调用 Servlet 类上定义的方法。
二、config 对象
config 存放 Servlet 初始数据结构,实现 javax.servlet.ServletConfig,常用四个方法:
public String getInitParameter(String name)
public java.util.Enumeration getInitParameterNames()
public ServletContext getServletContext()
public String getServletName()
三、request 对象
包含本次请求的全部信息:来源、标头、Cookie、参数。实现 javax.servlet.http.HttpServletRequest,方法可分成四类。
1. 存取属性
void setAttribute(String name, Object value)
java.util.Enumeration getAttributeNames()
Object getAttribute(String name)
void removeAttribute(String name)
2. 取得请求参数
String getParameter(String name)
java.util.Enumeration getParameterNames()
String[] getParameterValues(String name)
java.util.Map getParameterMap()
3. HTTP 标头与 Cookie
String getHeader(String name)
java.util.Enumeration getHeaderNames()
java.util.Enumeration getHeaders(String name)
int getIntHeader(String name)
long getDateHeader(String name)
Cookie[] getCookies()
4. 其他常用方法
String getContextPath() // 站点 Context 路径
String getMethod() // GET / POST
String getProtocol() // HTTP/1.1、HTTP/1.0
String getQueryString() // GET 的查询串
String getRequestedSessionId()
String getRequestURI() // 不含查询串
String getRemoteAddr()
String getRemoteHost()
int getRemotePort()
String getRemoteUser()
void setCharacterEncoding(String encoding) // 解决表单中文
四、response 对象
把 JSP 处理结果传回客户端,实现 javax.servlet.http.HttpServletResponse。
1. 设定表头
void addCookie(Cookie cookie)
void addDateHeader(String name, long date)
void addHeader(String name, String value)
void addIntHeader(String name, int value)
void setDateHeader(String name, long date)
void setHeader(String name, String value)
void setIntHeader(String name, int value)
2. 设定响应状态码
void sendError(int sc)
void sendError(int sc, String msg)
void setStatus(int sc)
3. URL 重写
String encodeRedirectURL(String url) 会对即将交给 sendRedirect() 的 URL 编码,便于在关闭 Cookie 时把 Session ID 接到地址上。
五、out 对象
out 把结果输出到网页上,类型是 JspWriter,用来管理输出缓冲区和输出流。
void clear() // 清除缓冲区(已提交则抛异常)
void clearBuffer() // 清除缓冲区内容
void close() // 关闭输出流
int getBufferSize() // 缓冲区大小
int getRemaining() // 剩余空间
boolean isAutoFlush() // 满时是否自动刷新;false 则满时抛异常
六、session 对象
表示当前用户的会话,实现 javax.servlet.http.HttpSession。
long getCreationTime()
String getId()
long getLastAccessedTime()
long getMaxInactiveInterval()
void invalidate()
boolean isNew()
void setMaxInactiveInterval(int interval)
七、application 对象
最常用来存取环境信息。环境通常存在 ServletContext 里,所以 application 就是当前应用的 Context。
int getMajorVersion()
int getMinorVersion()
String getServerInfo()
String getMimeType(String file)
ServletContext getContext(String uripath)
String getRealPath(String path)
void log(String message)
void log(String message, Throwable throwable)
八、pageContext 对象
pageContext 能拿到其他隐含对象,也能按范围存取属性。
1. 指定范围存取属性
Object getAttribute(String name, int scope)
java.util.Enumeration getAttributeNamesInScope(int scope)
void removeAttribute(String name, int scope)
void setAttribute(String name, Object value, int scope)
// 范围常量:PAGE_SCOPE、REQUEST_SCOPE、SESSION_SCOPE、APPLICATION_SCOPE
2. 取得其他隐含对象
Exception getException()
JspWriter getOut()
Object getPage()
ServletRequest getRequest()
ServletResponse getResponse()
ServletConfig getServletConfig()
ServletContext getServletContext()
HttpSession getSession()
3. 查找与删除属性
Object getAttribute(String name, int scope)
java.util.Enumeration getAttributeNamesInScope(int scope)
int getAttributesScope(String name)
void removeAttribute(String name)
void removeAttribute(String name, int scope)
void setAttribute(String name, Object value, int scope)
Object findAttribute(String name) // 按 page→request→session→application 查找
九、exception 对象
要使用 exception,必须在 page 指令里打开错误页:<%@ page isErrorPage="true" %>。
getMessage()
getLocalizedMessage()
printStackTrace(new java.io.PrintWriter(out))
一句话总结:九个隐含对象就是转译后的 Servlet 环境:先分清四个作用域,再用 pageContext 把它们串起来。