`
cfan_haifeng
  • 浏览: 120351 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

javaee-jsp原理

阅读更多

1.jsp原理图

 

 

其步骤如下:
    1.jsp引擎将jsp文件翻译为Servlet源程序;
    2.之后又将Servlet源程序编译为.class类文件;
其中:
     1.Jsp引擎是通常一个Servlet程序,Tomcat中的jsp引擎就是org.apache.jasper.servlet.JspServlet;
     2.在Tomcat中编译的源文件和.class文件放在”[TOMCAT_HOME]\work\Catalina\[主机名,如localhost]\应用程序名称” 目录下;
     3.默认情况下,第一次访问某jsp文件时,才对该jsp文件进行翻译、编译,所以较慢。不过在以后的访问中将不会在出现该情况,因为已经被编译过了,呵呵。

 

 

2.jsp相关类

 

2.1 jsp相关uml图

 

 

   其中,org.apache.jsp.index_jsp是tomcat将index.jsp转化为servlet源代码后的结果。其代码可以在如下目录中看到(当然,你要首先访问index.jsp才会翻译,编译)

 

 

2.2 具体类源代码

 

 

2.2.1 JspPage源代码

 

 

package javax.servlet.jsp;

import javax.servlet.Servlet;

public abstract interface JspPage extends Servlet
{
  public abstract void jspInit();

  public abstract void jspDestroy();
}
 

2.2.2 HttpJspPage源代码

 

 

package javax.servlet.jsp;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract interface HttpJspPage extends JspPage
{
  public abstract void _jspService(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException, IOException;
}
 

 

2.2.2 HttpJspBase 源代码

 

    和HttpJspPage名称虽然很香精 ,但已经位于不同的包中了。下面的源代码是我从tomcat6中的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;
}
 

 

2.2.2 JspSourceDependent源代码

 

 

package org.apache.jasper.runtime;

public abstract interface JspSourceDependent
{
  public abstract Object getDependants();
}
 

 

2.2.2  org.apache.jsp.index_jsp 源代码

 

       上面提到过org.apache.jsp.index_jsp是tomcat将index.jsp转化为servlet的源代码。

 

 

 

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;

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

  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=GB18030");
      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');
      out.write('\n');

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

      out.write("\r\n");
      out.write("\r\n");
      out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
      out.write("<html>\r\n");
      out.write("  <head>\r\n");
      out.write("    <base href=\"");
      out.print(basePath);
      out.write("\">\r\n");
      out.write("    \r\n");
      out.write("    <title>My JSP 'index.jsp' starting page</title>\r\n");
      out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
      out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
      out.write("\t<meta http-equiv=\"expires\" content=\"0\">    \r\n");
      out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
      out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
      out.write("\t<!--\r\n");
      out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
      out.write("\t-->\r\n");
      out.write("  </head>\r\n");
      out.write("  \r\n");
      out.write("  <body>\r\n");
      out.write("    This is my JSP page. <br>\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);
    }
  }
}

 

其对应的index.jsp为

 

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    This is my JSP page. <br>
  </body>
</html>
 

2.2.3 九大隐藏对象

 

    从jsp转换的源码中,很容易的就可以发现了传说中的9大jsp内置隐藏对象中的8个。如下:

 

    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;
 

    至于第9个,只有page指令被设置为:

 

<%@ page isErrorPage="true" %>

 

    才会出现:

 

Throwable exception = org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable(request);

 

    9大内置对象中,exception几乎不用,其他则是耳熟能详。唯独  PageContext pageContext 少见,我们以后娓娓道来。

 

参考:《深入体验Java_Web开发内幕-核心基础\》

……

……

 

 

 

 

 

  • 大小: 6.6 KB
  • 大小: 12.1 KB
  • 大小: 5.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics