其本質是Servlet,web應用中的每一個jsp頁面都會由servlet容器生成對應的servlet。html
在tomcat中,jsp生成的servlet在work文件夾下:java
原jsp文件:web
對應的servletexpress
show.jspapache
<%-- 編譯指令--%> <%@page contentType="text/html;charset=UTF-8" language="java" errorPage="error.jsp"%> <%@page info="this is a jsp page"%> <html> <head> <title> 歡迎 </title> </head> <%-- jsp聲明變量和方法--%> <%! private int count; private int num; public String print(){ return "hello"; } %> <body> <%-- jsp註釋,不會出如今瀏覽器的源代碼中。只在服務端。--%> <!--html註釋--> 你訪問! <%out.print(new java.util.Date());%> <%for(int i=0;i<5;i++){ out.print("<font size='"+i+"'>" );%> hello world</font> <%}%> <%-- 變量和方法使用--%> <%out.print(count++); %> <%out.print(print());%> <%-- JSP表達式--%> <%=num++%> <%=getServletInfo()%> <table border="1"> <%-- jsp腳本--%> <%for(int j=0;j<8;j++){%> <%!private int n;%> <tr><td>n++</td></tr> <%}%> <%-- 這裏出現異常,跳轉向error.jsp--%> <%!int m=7; int p=4; %> <%=m/p%> </table> </body> </html>
show_jsp.java瀏覽器
package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class show_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { //jsp中編譯指令page的info屬性 public String getServletInfo() { return "this is a jsp page"; } //jsp中聲明的變量和方法 private int count; private int num; public String print(){ return "hello"; } private int n; int m=7; int p=4; 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; } //初始化JSP/servlet的方法 public void _jspInit() { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName()); } //銷燬JSP/servlet以前的方法 public void _jspDestroy() { } //對用戶請求生成響應的方法 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=UTF-8"); 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("<html>\r\n"); out.write("<head><title>\r\n"); out.write("歡迎\r\n"); out.write("\r\n"); out.write("</title>\r\n"); out.write("</head>\r\n"); out.write("\r\n"); out.write("\r\n"); out.write("<body>\r\n"); out.write("\r\n"); out.write("\r\n"); out.write("<!--html註釋-->\r\n"); out.write("你訪問!\r\n"); out.print(new java.util.Date()); out.write('\r'); out.write('\n'); for(int i=0;i<5;i++){ out.print("<font size='"+i+"'>" ); out.write("\r\n"); out.write("\thello world</font>\r\n"); out.write("\t\r\n"); out.write("\t\r\n"); } out.write("\r\n"); out.write("\r\n"); out.write("\r\n"); out.print(count++); out.write('\r'); out.write('\n'); out.print(print()); out.write('\r'); out.write('\n'); out.print(num++); out.write("\r\n"); out.write("<table border=\"1\">\r\n"); for(int j=0;j<8;j++){ out.write('\r'); out.write('\n'); out.write("\r\n"); out.write("<tr><td>n++</td></tr>\r\n"); out.write("\t\r\n"); } out.write('\r'); out.write('\n'); out.print(getServletInfo()); out.write('\r'); out.write('\n'); out.write('\r'); out.write('\n'); out.print(m/p); out.write("\r\n"); out.write("</table>\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); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
二.JSP的工做原理tomcat