JSP脚本中包含9个内置对象,都是ServletAPI接口的实例,jsp规范对它们进行了默认初始化由JSP页面对应的_jspService()方法来创建这些实例.它们已经是对象,可以直接使用.
9个内置对象依次如下:
- application: javax.servlet.ServletContet的实例,代表JSP所属的Web应用本身,可用于JSP页面,或者在Servlet之间交换信息.常用方法有 getAttribute(String attName), setAttribute(String attName,String attValue),getInitParameter(String paramName);
- config: javax.servlet.ServletConfig 的实例,代表该JSP的配饰信息,常用方法有: getInitParameter(String paramName),getInitParameternames() Jsp页面通常无需配置,不存在配置信息,,该对象更多在Servlet中有效
- exception : java.lang.Throwable的实例,代表其他页面中的异常和错误,只有当页面是错误处理页面,即编译指令 page 的 isErrorPage属性为 true时,该对象才可以使用.常用方法: getMessage(); printStackTrace();
- out: javax.servlet.jsp.Wruter的实例,代表JSP页面的输出流,用于输出内容,形成HTML页面
- page: 代表页面本身,相当于Servlet中的this,其类型就是生成的Servlet类,能用page的地方就可以用this
- pageContext: javax.servlet.jsp.PageContext 的实例,代表JSP页面的上下文,使用该对象可以访问页面中的共享数据,常用的方法有 getServletContext();getServletConfig();
- request: javax.servlet.http.HttpServletRequest 的实例,封装了一次请求,客户端的请求参数都被封装在该对象里.获取客户端请求参数必须使用该对象,常用方法: getParameter(String paramName); getParameterValues(String paramName);setAttribute(String attName,Object attrValue),getAttribute(String attrName); setCharacterEncoding(String env);
- response: javax.servlet.http.HttpServletResponse的实例,代表服务器对客户端的响应.通常很少使用该对象的直接响应,而是使用 out 对象.除非需要生成非字符响应.而response 对象常用于重定向,常用方法:getOutputStream(); sendRedirect(java.lang.String.location);
- session: javax.servlet.http.HttpSession 的实例,代表一次会话,当客户端浏览器与站点建立连接时,会话开始;当客户端关闭浏览器时,会话结束.常用方法: getAttribute(String attrName);setAttribute(String attrName,Object attrValue);
进入Tomcat 7.0\work\Catalina\localhost\_\org\apache\jsp路径下,JSP页面对应生成的Servlet类文件代码片段如下:
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.59
* Generated at: 2016-12-07 03:20:17 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
public void _jspDestroy() {
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final javax.servlet.jsp.PageContext pageContext;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
response.setContentType("text/html");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, false, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
out = pageContext.getOut();
_jspx_out = out;
out.write("\r\n");
out.write("<!DOCTYPE html>\r\n");
out.write("\r\n");
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
request.setAttribute("year", sdf.format(new java.util.Date()));
request.setAttribute("tomcatUrl", "http://tomcat.apache.org/");
request.setAttribute("tomcatDocUrl", "/docs/");
request.setAttribute("tomcatExamplesUrl", "/examples/");
out.write("\r\n");
out.write("<html lang=\"en\">\r\n");
out.write(" <head>\r\n");
out.write(" <title>");
out.print(request.getServletContext().getServerInfo() );
out.write("</title>\r\n");
out.write(" <link href=\"favicon.ico\" rel=\"icon\" type=\"image/x-icon\" />\r\n");
out.write(" <link href=\"favicon.ico\" rel=\"shortcut icon\" type=\"image/x-icon\" />\r\n");
out.write(" <link href=\"tomcat.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n");
out.write(" </head>\r\n");
out.write("\r\n");
out.write(" <body>\r\n");
out.write(" <div id=\"wrapper\">\r\n");
out.write(" <div id=\"navigation\" class=\"curved container\">\r\n");
out.write(" <span id=\"nav-home\"><a href=\"");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\">Home</a></span>\r\n");
out.write(" <span id=\"nav-hosts\"><a href=\"");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\">Documentation</a></span>\r\n");
out.write(" <span id=\"nav-config\"><a href=\"");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("config/\">Configuration</a></span>\r\n");
out.write(" <span id=\"nav-examples\"><a href=\"");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatExamplesUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\">Examples</a></span>\r\n");
out.write(" <span id=\"nav-wiki\"><a href=\"http://wiki.apache.org/tomcat/FrontPage\">Wiki</a></span>\r\n");
out.write(" <span id=\"nav-lists\"><a href=\"");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("lists.html\">Mailing Lists</a></span>\r\n");
out.write(" <span id=\"nav-help\"><a href=\"");
// ...........................
代码表明:
request,response两个对象是_jspService()方法的形参,当Tomcat调用该方法时,会初始化这两个对象.
而page,pageContext,application,config,session,out都是-jspService()方法的局部变量,由该方法完成初始化.
只有当页面的page指令的isErrorPage 属性为true 时,才可以使用 exception对象,只有异常处理页面对应Servlet时,才会初始化 exception对象.
JSP内置对象的实质: 是_jspService()方法的形参或者局部变量,所以可以直接在jsp脚本中调用这些方法,无需创建它们(脚本将对应Servlet的_jspSewrvice()方法部分).
注意:
由于JSP内置对象都是在_jspService()方法中完成初始化的,因此只能在jsp脚本,jsp输出表达式中使用这些内置对象,不可以在jsp声明中使用它们,否则.系统将提示找不到这些变量,当编写jsp页面时,不要仅停留在jsp页面本身来看问题.