JSP隐式对象

http://www.runoob.com/jsp/jsp-implicit-objects.html

实际上,我们可以到JSP编译后的servlet中去找,进入TOMCAT安装目录的work下:

/work/Catalina/localhost/contextPath/org/apache/jsp/WEB_002dINF/jsp/index_jsp.java

在其中找到_jspService方法:

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;

......

request, response, pageContext, session, application, config, out, page, 再加一个exception,就是JSP九大隐式对象了。

其中 pageContext,request,session,application 就是我们所说的JSP四大域对象。

因为我们在JSP页面的<%....%>中写入的任何内容在最终编译后都会成为JSP对应的servlet类的_jspService()方法的一部分,所以这9个隐式对象无需在页面中声明就可以直接使用。

而<%!...%>中的内容则会成为JSP对应的servlet类的成员变量。

<%=...%>中的内容则会成为JSP对应的servlet类的_jspService()方法中out.println(...)的参数。

另外,还有EL的隐式对象与TAG文件的隐式对象,咱们以后说。

原文地址:https://www.cnblogs.com/kingsniper13/p/7692275.html