//JSP页面经过Tomcat编译后默认的包
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
//继承HttpJspBase类,该类其实是个Servlet的子类
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;
javax.servlet.http.HttpSession session = null;
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, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
//页面输出流
out = pageContext.getOut();
_jspx_out = out;
//输出流,开始输出页面文档
out.write("<html>\r\n");
out.write("<body>\r\n");
out.write("<center> \r\n");
out.write("Now time is: ");
out.print(new java.util.Date());
out.write(" \r\n");
out.write("</center>\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.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 throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
当启动Tomcat之后,可以在Tomcat的 apache-tomcat-7.0.62\work\Catalina\localhost\_\org\apache\jsp目录下找到如下文件:index.java和index.class。这两个文件都是Tomcat生成的,Tomcat根据JSP页面生成对应Servlet的Java文件及class文件。
index.java
JSP页面中内置了几个对象,如pageContext、application、config、page、session、out等_jspService()方法,这几个内置对象就是在这里定义的。
根据上面的JSP页面工作原理图,可以得到如下结论: JSP文件必须在JSP服务器内运行。JSP文件必须生成Servlet才能执行。每个JSP页面的第一个访问者速度很慢,因为必须等待JSP编译成Servlet。JSP页面的访问者无须安装任何客户端,甚至不需要可以运行Java的运行环境,因为JSP页面输送到客户端的是标准HTML页面。index.jsp页面中的每个字符都由index.java文件的输出流生成.
servlet是在web服务器上的java程序,它提供服务,由它来传递给你html的格式。Servlet API为Servlet提供了统一的编程接口
Servlet必须部署在Servlet容器,才能响应客户端的请求 对外提供服务。要对外统一接口,由容器来调用。
jsp侧重显示;servlet侧重控制逻辑