JSP学习(1)---JSP基本原理

一.JSP的本质

其本质是Servlet,web应用中的每个jsp页面都会由servlet容器生成对应的servlet。

在tomcat中,jsp生成的servlet在work文件夹下:

原jsp文件:

对应的servlet

show.jsp

<%-- 编译指令--%>
<%@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("
");
      out.write("
");
      out.write("<html>
");
      out.write("<head><title>
");
      out.write("欢迎
");
      out.write("
");
      out.write("</title>
");
      out.write("</head>
");
      out.write("
");
      out.write("
");
      out.write("<body>
");
      out.write("
");
      out.write("
");
      out.write("<!--html注释-->
");
      out.write("你访问!
");
out.print(new java.util.Date());
      out.write('
');
      out.write('
');
for(int i=0;i<5;i++){
    out.print("<font size='"+i+"'>" );
      out.write("
");
      out.write("	hello world</font>
");
      out.write("	
");
      out.write("	
");
}
      out.write("
");
      out.write("
");
      out.write("
");
out.print(count++);

      out.write('
');
      out.write('
');
out.print(print());
      out.write('
');
      out.write('
');
      out.print(num++);
      out.write("
");
      out.write("<table border="1">
");
for(int j=0;j<8;j++){
      out.write('
');
      out.write('
');
      out.write("
");
      out.write("<tr><td>n++</td></tr>
");
      out.write("	
");
}
      out.write('
');
      out.write('
');
      out.print(getServletInfo());
      out.write('
');
      out.write('
');
      out.write('
');
      out.write('
');
      out.print(m/p);
      out.write("
");
      out.write("</table>
");
      out.write("</body>
");
      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的工作原理

原文地址:https://www.cnblogs.com/zhima-hu/p/8177240.html