jsp九大隐式对象

本文深入解析JSP本质与Servlet之间的联系,解释JSP引擎如何将JSP翻译为servlet,以及九大隐式对象在JSP开发中的重要应用。包括out对象的输出机制与pageContext对象的域管理功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在没学jsp之前就知道jsp的本质是servlet,只是就知道这句话而不知道为什么这样说,学了之后才明白这句话,也明白为什么要在学jsp之前学servlet。
在jsp页面第一次被访问时,web容器都会把请求交给jsp引擎处理,jsp引擎会事先把jsp翻译成一个_jspServlet,就是一个servlet,然后按照servlet的调用方式进行调用。也是因此,第一次访问jsp就会比较慢,而第二次访问如果jsp引擎发现jsp没有发生变化,就不在翻译,而直接调用,此时就会快很多。
public final class _1_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {

private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

private static java.util.List _jspx_dependants;

private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;

public Object getDependants() {
return _jspx_dependants;
}

public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
}

public void _jspDestroy() {
}

public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {

PageContext pageContext = null;
HttpSession session = null;
Throwable exception = org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable(request);
if (exception != null) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
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('\r');
out.write('\n');

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
out.write("<html>\r\n");
out.write(" <head>\r\n");
out.write(" <base href=\"");
out.print(basePath);
out.write("\">\r\n");
out.write(" \r\n");
out.write(" <title>My JSP '1.jsp' starting page</title>\r\n");
out.write(" \r\n");
out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n");
out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
out.write("\t<!--\r\n");
out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
out.write("\t-->\r\n");
out.write("\r\n");
out.write(" </head>\r\n");
out.write(" \r\n");
out.write(" <body>\r\n");
out.write(" This is my JSP page. <br>\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
} 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);
else log(t.getMessage(), t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}

以上是一个jsp翻译的servlet类,jsp引擎在调用_jspServlet时,会传递或创建九个与web开发相关的对象供_jspServlet使用。从以上jsp翻译的servlet类中就能后找到它们,request、response、session、config、application(就是servletContext)、page、exception、out、pageContext,总共九个对象。其中前六个在servlet中已经详细讲过,而page指jsp对象本身,exception此对象只在页面设置isErrorPage="true"时才会出现,即代表设置当前页面为异常处理页面,所以在jsp九大隐式对象中要讲的只有out和pageContext。
一、out对象。
out对象用来向客户机输出数据,它的对象类型为JspWriter,相当于带有缓存功能的PrintWriter,设置jsp页面的page指令的buffer属性可以控制缓存的大小,甚至关闭缓存,默认缓存大小为8k。只有想out对象中写入了内容,且满足以下三个条件的任意一个时out才会调用ServletResponse.getWriter方法,并通过方法返回的PrintWriter对象将out里的内容写入servlet引擎提供的缓存区。(1)关闭了out的缓存功能(2)缓存区已满(3)真个jsp页面结束。
  <%

out.write("aaa");
response.getWriter().write("ccc");
%>

页面访问结果是"ccc"在前,"aaa"在后。
二、pageContext对象。
PegeContext对象代表jsp页面运行环境,PegeContext对象是web开发中的四大域之一(其他三个request、session、servletContext),可以用来存放数据,它的使用范围在四大域中最小,只在整个jsp页面。
PageContext封装了其他八大域的对象的引用,此在自定义标签开发会用到。良好的jsp页面里是不应该出现java代码,而自定义标签就是用来把jsp里的java代码移除,再通过自定义标签的引用,来实现java代码的功能。
PageContext对象封装了访问其他域的方法,可以向其他域存储、获得或移除数据,它提供了操纵其他域的一个入口。
还有一个很为重要的一个方法,findAttribute(String name),它首先从PageContext域中寻找,找不到再从request域中寻找,找不到再从session域中寻找,再找不到就从application域中寻找,最后找不到返回null值。
String data="xxx";
request.setAttribute("data", data);
String data1=(String)pageContext.getAttribute("data", PageContext.REQUEST_SCOPE);
String data2=(String)pageContext.findAttribute("data");

另外,PageContext对象定义了forward方法和两个include方法来分别简化和替代RequestDispatcher.forward方法和include方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值