JSP脚本中的九大内置对象详解(上)

原理:我们都知道,jsp在第一次被请求时会被编译成servlet,实际上是由该servlet来处理用户请求。

了解了上述内容,我们再来说说九大内置对象的实质。它们都是Servlet API中接口的实例,在jsp编译成servlet时进行了默认初始化,所以我们才能在jsp脚本中直接使用它们。

直接上代码:

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=GBK");
pageContext = _jspxFactory.getPageContext(this, request, response,
"error.jsp", true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;

out.write("\r\n");
out.write("\r\n");
out.write("<!doctype html>\r\n");
out.write("<html>\r\n");
out.write("\t<head>\r\n");
out.write("\t\t<title>webDEMO欢迎您</title>\r\n");
out.write("\t</head>\r\n");
out.write("\t<body>\r\n");
out.write("\t\t");
out.write("\r\n");
out.write("\t\t");
out.write("\r\n");
out.write("\t\t<!-- html注释 -->\r\n");
out.write("\t\t<p>webDEMO欢迎您");
new Date();//从这里可以看出,jsp脚本中的内容会编译进该处理方法中
out.write("<br/>");
out.print((++count) );
out.write("<br/>");
out.print(info() );
out.write("<br/>");
out.print(getServletInfo() );
out.write("</p>\r\n");
out.write("\t\t");

String name = config.getInitParameter("name");//int a=1/0;

out.write("\r\n");
out.write("\t\t");
out.print(name );
out.write("\r\n");
out.write("\t</body>\r\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);
}
}

每一个jsp编译成的servlet中都有这一段,看红色标注部分,应该能理解为啥能在jsp脚本中直接使用这九大内置对象了吧。还有一点需要注意,这里只初始化了八个内置对象,对于exception,当我们在jsp中指定其isErrorPage=“true”时,才能看到其初始化的代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值