Jsp
是什么
Jsp
本质上是一个Servlet
- 在
HTML
文档中嵌入Java
代码,来生成动态网页
运行流程
- 在web应用内编写Jsp源文件
- 部署到Tomcat
- 在浏览器输入相应URL来访问这个Jsp文件
- 查找与Jsp文件对应的Servlet,如果已经存在,就调用它的服务方法
- 如果Jsp已经更新或者还不存在Servlet,就解析文件系统中的Jsp文件,翻译为Servlet并编译此Servlet,然后初始化并运行此Servlet
Jsp
翻译为Servlet
,一般只发生在客户端首次访问和Jsp
文件有更新时
- 简单解析
Jsp
对应的Servlet
Jsp文件:hello.jsp
<%@ page language="java" import="java.io.File" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>lifeJSP</title> </head> <body> <%! int initVar = 0; int serivceVar = 0; int destryVar = 0; %> <%! public void jspInit() { ++initVar; System.out.println("JspInit()被初始化 " + initVar +" 次"); } public void jspDestory() { ++destryVar; System.out.println("JspDestory()被销毁 " + destryVar + "次"); } %> <% ++serivceVar; System.out.println("JspSerivce()响应了" + serivceVar + "次"); String initTime = "JspInit()被初始化 " + initVar +" 次"; String destoryTime = "JspDestory()被销毁 " + destryVar + "次"; String sericetime = "JspSerivce()响应了" + serivceVar + "次"; File temp = (File)application.getAttribute("javax.servlet.context.tempdir"); String workdir = "工作目录:" + temp.getPath(); %> <h1><%= workdir %></h1> <h1><%= initTime %></h1> <h1><%= destoryTime %></h1> <h1><%= sericetime %></h1> </body> </html>
对应的Servlet
package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class hello_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { public void _jspInit() { } 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 java.lang.String _jspx_method = request.getMethod(); if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) { response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD"); return; } 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; 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\n"); out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n"); out.write("<title>Insert title here</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); out.write("\t"); out.print( "HelloWorld" ); out.write("\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 { if (response.isCommitted()) { out.flush(); } else { 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); } } }
- 由于原Servlet文件内容太多,这里就只保留了了一部分
- 与Jsp文件对应的Servlet类继承org.apache.jasper.runtime.HttpJspBase,HttpJspBase由Tomcat提供,它实现了Servlet API中的javax.servlet.jsp.HttpJspPage接口,该接口继承了javax.servlet.jsp.JspPage接口,而JspPage又继承了javax.servlet.Servlet接口
- 方法
_jspInit()
为Jsp
对应Servlet
的初始化方法 - 方法
_jspDestory()
为Jsp
对应Servlet
的销毁方法 - 方法
_jspService()
为Jsp
对应Servlet
的服务方法 - Jsp对应的Servlet把Jsp文件中的HTML标记都作为打印流的内容输出到客户端