JSP 文件在第一次被浏览器访问到时,会被容器翻译成一个 Java 文件,在这个 Java 中可以看到 _jspService 方法,这个方法中初始化了一些 Servlet 对象,这些就是常说的 JSP 中的隐含对象。在 JSP 页面中,可以直接使用,当然,要使用约定的名称,呵呵,这些约定的名称即下表中的左列。本站表达式语言中也有对隐含对象的部分描述。
(JSP 种的隐含对象是9个比 EL 中的11个要少,它们之间有很大不同。後者具体可以参考本站的《Expression Language》的 Implicit Objects 部分或:http://docs.oracle.com/javaee/5/tutorial/doc/bnahq.html#bnaij 的说明。)
这里引用一段 JSP 页面编译後的 Java 的代码,可以很清楚地看到这些隐含对象的声明。
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType("text/html; charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
out.write("<html>\n");
out.write("<head>\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
out.write("<title>Hello, world!</title>\n");
out.write("</head>\n");
out.write("<body>\n");
out.write("Hello, world!\n");
out.write("</body>\n");
out.write("</html>");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}