javaWeb知识点—JSP

 

JSP---由服务器编译成java文件(实质上是一个servlet---java文件进一步编译成class文件。

客户端浏览器向服务器端请求jsp页面时,实际上执行的是编译之后的class文件,该class文件通过流的形式向客户端输出信息。

 

JSP文件在客户端首次请求时,执行向java---class的转换,故而首次访问时,会显得有些慢。

 

Tomcat下,JSP变成生成的Java文件继承自org.apache.jasper.runtime .HttpJspBase,而HttpJspBase实际上是HttpServlet的子类。

 

Tomcat60\lib\jasper.jar

package org.apache.jasper.runtime;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.HttpJspPage;
import org.apache.jasper.compiler.Localizer;

public abstract class HttpJspBase extends HttpServlet
  implements HttpJspPage

{
  public final void init(ServletConfig config)
    throws ServletException
  {
    super.init(config);
    jspInit();
    _jspInit();
  }

  public String getServletInfo() {

    return Localizer.getMessage("jsp.engine.info");

  }

  public final void destroy() {

    jspDestroy();

    _jspDestroy();

  }

  public final void service(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException

  {

    _jspService(request, response);

  }
  public void jspInit()

  {

  }

  public void _jspInit()

  {

  }
  public void jspDestroy()

  {

  }
  protected void _jspDestroy()

  {

  }

  public abstract void _jspService(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)

    throws ServletException, IOException;
}

 

 

Tomcat下,JSP编译生成的javaclass文件在Tomcat60\work\Catalina 目录下,如:



javaWeb知识点—JSP
 
 

 

head.jsp编译之后的内容如下(主要包含3个方法initservicedestroy):

package org.apache.jsp.jsp.public_;

 

import javax.servlet.*;

import javax.servlet.http.*;

import javax.servlet.jsp.*;

 

public final class head_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;

 

  static {

    _jspx_dependants = new java.util.ArrayList(1);

    _jspx_dependants.add("/WEB-INF/tld/next-web.tld");

  }

 

  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;

    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\n");

      out.write("\r\n");

      out.write("<HTML>\r\n");

      out.write("\t<HEAD>\r\n");

      out.write("\t<style type=\"text/css\">\r\n");

      out.write("\t</style>\r\n");

      out.write("\t</HEAD>\r\n");

      out.write("\t<body>\r\n");

      out.write("\t\t<img alt=\"\" width=\"100%\" height=\"100%\" src=\"images/pic.JPG\">\r\n");

      out.write("\t</body>\r\n");

      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);

    }

  }

}

 

 Head.jsp编译之前内容如下:

 

<%@ page contentType="text/html; charset=UTF-8" %>

<%@ taglib uri="/tags/next-web" prefix="next"%>

<HTML>

         <HEAD>

         <style type="text/css">

         </style>

         </HEAD>

         <body>

                   <img alt="" width="100%" height="100%" src="images/pic.JPG">

         </body>

</HTML>

 

JSPservlet在实际执行时,实际上都是单例模式,所以成员变量是非线程安全的。

JSP页面会编译成servet,每个servlet在容器中只有一个实例。

 

JSP声明<%! 声明部分 %>

JSP声明部分定义的变量和方法对应servlet中的成员变量和成员方法。

 

 

你可能感兴趣的:(jsp)