`
eapcmg
  • 浏览: 446 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论
阅读更多

      原文:http://zwdsmileface.iteye.com/blog/2192709

 

 

JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术。
  JSP这门技术的最大的特点在于,写jsp就像在写html,但它相比html而言,html只能为用户提供静态数据,而Jsp技术允许在页面中嵌套java代码,为用户提供动态数据。
JSP原理
Web服务器是如何调用并执行一个jsp页面的?
  浏览器向服务器发请求,不管访问的是什么资源,其实都是在访问Servlet,所以当访问一个jsp页面时,其实也是在访问一个 Servlet,服务器在执行jsp的时候,首先把jsp翻译成一个Servlet,所以我们访问jsp时,其实不是在访问jsp,而是在访问jsp翻译 过后的那个Servlet,例如下面的代码:
index.jsp

Java代码  收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2.  <%  
  3.  String path = request.getContextPath();  
  4.  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5.  %>  
  6.    
  7.  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8.  <html>  
  9.    <head>  
  10.      <base href="<%=basePath%>">       
  11.      <title>First Jsp</title>       
  12.   </head>     
  13.    <body>  
  14.      <%  
  15.          out.print("Hello Jsp");  
  16.      %>  
  17.    </body>  
  18.  </html>  


当我们通过浏览器访问index.jsp时,服务器首先将index.jsp翻译成一个index_jsp.class,在Tomcat服务器的work\Catalina\localhost\项目名\org\apache\jsp目录下可以看到index_jsp.class的源代码文件index_jsp.java
index_jsp.java的代码如下:

Java代码  收藏代码
  1. package org.apache.jsp;  
  2.   
  3.  import javax.servlet.*;  
  4.  import javax.servlet.http.*;  
  5.  import javax.servlet.jsp.*;  
  6.  import java.util.*;  
  7.    
  8.  public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase  
  9.      implements org.apache.jasper.runtime.JspSourceDependent {  
  10.    
  11.    private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();  
  12.    
  13.    private static java.util.List _jspx_dependants;  
  14.    
  15.    private javax.el.ExpressionFactory _el_expressionfactory;  
  16.    private org.apache.AnnotationProcessor _jsp_annotationprocessor;  
  17.    
  18.    public Object getDependants() {  
  19.      return _jspx_dependants;  
  20.    }  
  21.    
  22.    public void _jspInit() {  
  23.      _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();  
  24.      _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());  
  25.    }  
  26.    
  27.    public void _jspDestroy() {  
  28.    }  
  29.    
  30.    public void _jspService(HttpServletRequest request, HttpServletResponse response)  
  31.          throws java.io.IOException, ServletException {  
  32.    
  33.      PageContext pageContext = null;  
  34.      HttpSession session = null;  
  35.      ServletContext application = null;  
  36.      ServletConfig config = null;  
  37.      JspWriter out = null;  
  38.      Object page = this;  
  39.      JspWriter _jspx_out = null;  
  40.      PageContext _jspx_page_context = null;  
  41.    
  42.    
  43.      try {  
  44.        response.setContentType("text/html;charset=UTF-8");  
  45.        pageContext = _jspxFactory.getPageContext(this, request, response,  
  46.                    nulltrue8192true);  
  47.        _jspx_page_context = pageContext;  
  48.        application = pageContext.getServletContext();  
  49.        config = pageContext.getServletConfig();  
  50.        session = pageContext.getSession();  
  51.        out = pageContext.getOut();  
  52.        _jspx_out = out;  
  53.    
  54.        out.write('\r');  
  55.        out.write('\n');   
  56.  String path = request.getContextPath();  
  57.  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  58.    
  59.        out.write("\r\n");  
  60.        out.write("\r\n");  
  61.        out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");  
  62.        out.write("<html>\r\n");  
  63.        out.write("  <head>\r\n");  
  64.        out.write("    <base href=\"");  
  65.        out.print(basePath);  
  66.        out.write("\">\r\n");  
  67.        out.write("    \r\n");  
  68.        out.write("    <title>First Jsp</title>\r\n");  
  69.        out.write("\t\r\n");  
  70.        out.write("  </head>\r\n");  
  71.        out.write("  \r\n");  
  72.        out.write("  <body>\r\n");  
  73.        out.write("    ");  
  74.    
  75.          out.print("Hello Jsp");  
  76.        
  77.        out.write("\r\n");  
  78.        out.write("  </body>\r\n");  
  79.        out.write("</html>\r\n");  
  80.      } catch (Throwable t) {  
  81.        if (!(t instanceof SkipPageException)){  
  82.          out = _jspx_out;  
  83.          if (out != null && out.getBufferSize() != 0)  
  84.            try { out.clearBuffer(); } catch (java.io.IOException e) {}  
  85.          if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);  
  86.        }  
  87.      } finally {  
  88.        _jspxFactory.releasePageContext(_jspx_page_context);  
  89.      }  
  90.    }  
  91.  }  


我们可以看到,index_jsp这个类是继承 org.apache.jasper.runtime.HttpJspBase这个类的,通过查看Tomcat服务器的源代码,可以知道在apache-tomcat-6.0.20-src\java\org\apache\jasper\runtime目录下存HttpJspBase这个类的源代码文件
我们可以看看HttpJsBase这个类的源代码,如下所示:

Java代码  收藏代码
  1. /* 
  2.   * Licensed to the Apache Software Foundation (ASF) under one or more 
  3.   * contributor license agreements.  See the NOTICE file distributed with 
  4.   * this work for additional information regarding copyright ownership. 
  5.   * The ASF licenses this file to You under the Apache License, Version 2.0 
  6.   * (the "License"); you may not use this file except in compliance with 
  7.   * the License.  You may obtain a copy of the License at 
  8.   *  
  9.   *      http://www.apache.org/licenses/LICENSE-2.0 
  10.   *  
  11.   * Unless required by applicable law or agreed to in writing, software 
  12.   * distributed under the License is distributed on an "AS IS" BASIS, 
  13.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  14.   * See the License for the specific language governing permissions and 
  15.   * limitations under the License. 
  16.   */  
  17.    
  18.  package org.apache.jasper.runtime;  
  19.    
  20.  import java.io.IOException;  
  21.    
  22.  import javax.servlet.ServletConfig;  
  23.  import javax.servlet.ServletException;  
  24.  import javax.servlet.http.HttpServlet;  
  25.  import javax.servlet.http.HttpServletRequest;  
  26.  import javax.servlet.http.HttpServletResponse;  
  27.  import javax.servlet.jsp.HttpJspPage;  
  28.  import javax.servlet.jsp.JspFactory;  
  29.    
  30.  import org.apache.jasper.compiler.Localizer;  
  31.    
  32.  /** 
  33.   * This is the super class of all JSP-generated servlets. 
  34.   * 
  35.   * @author Anil K. Vijendran 
  36.   */  
  37.  public abstract class HttpJspBase   
  38.      extends HttpServlet   
  39.      implements HttpJspPage   
  40.            
  41.        
  42.  {  
  43.        
  44.      protected HttpJspBase() {  
  45.      }  
  46.    
  47.      public final void init(ServletConfig config)   
  48.      throws ServletException n  
  49.      {  
  50.          super.init(config);  
  51.          jspInit();  
  52.          _jspInit();  
  53.     }  
  54.        
  55.      public String getServletInfo() {  
  56.      return Localizer.getMessage("jsp.engine.info");  
  57.      }  
  58.    
  59.      public final void destroy() {  
  60.      jspDestroy();  
  61.      _jspDestroy();  
  62.      }  
  63.    
  64.      /** 
  65.       * Entry point into service. 
  66.       */  
  67.      public final void service(HttpServletRequest request, HttpServletResponse response)   
  68.      throws ServletException, IOException   
  69.      {  
  70.          _jspService(request, response);  
  71.      }  
  72.        
  73.      public void jspInit() {  
  74.      }  
  75.    
  76.      public void _jspInit() {  
  77.      }  
  78.    
  79.      public void jspDestroy() {  
  80.      }  
  81.    
  82.      protected void _jspDestroy() {  
  83.      }  
  84.    
  85.      public abstract void _jspService(HttpServletRequest request,   
  86.                       HttpServletResponse response)   
  87.      throws ServletException, IOException;  
  88.  }  


HttpJspBase类是继承HttpServlet的,所以HttpJspBase类是一个Servlet,而index_jsp又是继承 HttpJspBase类的,所以index_jsp类也是一个Servlet,所以当浏览器访问服务器上的index.jsp页面时,其实就是在访问 index_jsp这个Servlet,index_jsp这个Servlet使用_jspService这个方法处理请求。

Jsp页面中的html排版标签是如何被发送到客户端的?

浏览器接收到的这些数据

Java代码  收藏代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2.  <html>  
  3.    <head>  
  4.      <base href="http://localhost:8080/JavaWeb_Jsp_Study_20140603/">  
  5.        
  6.      <title>First Jsp</title>  
  7.        
  8.    </head>  
  9.      
  10.    <body>  
  11.      Hello Jsp  
  12.    </body>  
  13.  </html>  


都是在_jspService方法中使用如下的代码输出给浏览器的:

Java代码  收藏代码
  1. out.write('\r');  
  2. out.write('\n');   
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5.   
  6.       out.write("\r\n");  
  7.       out.write("\r\n");  
  8.       out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");  
  9.       out.write("<html>\r\n");  
  10.       out.write("  <head>\r\n");  
  11.       out.write("    <base href=\"");  
  12.       out.print(basePath);  
  13.       out.write("\">\r\n");  
  14.       out.write("    \r\n");  
  15.       out.write("    <title>First Jsp</title>\r\n");  
  16.       out.write("\t\r\n");  
  17.       out.write("  </head>\r\n");  
  18.       out.write("  \r\n");  
  19.       out.write("  <body>\r\n");  
  20.       out.write("    ");  
  21.   
  22.       out.print("Hello Jsp");  
  23.       
  24.       out.write("\r\n");  
  25.       out.write("  </body>\r\n");  
  26.       out.write("</html>\r\n");  


  在jsp中编写的java代码和html代码都会被翻译到_jspService方法中去,在jsp中编写的java代码会原封不动地翻译成 java代码,如<%out.print("Hello Jsp");%>直接翻译成out.print("Hello Jsp");,而HTML代码则会翻译成使用out.write("<html标签>\r\n");的形式输出到浏览器。在jsp页面中编写 的html排版标签都是以out.write("<html标签>\r\n");的形式输出到浏览器,浏览器拿到html代码后才能够解析执 行html代码。

Jsp页面中的java代码服务器是如何执行的?

  在jsp中编写的java代码会被翻译到_jspService方法中去,当执行_jspService方法处理请求时,就会执行在jsp编写的java代码了,所以Jsp页面中的java代码服务器是通过调用_jspService方法处理请求时执行的。

Web服务器在调用jsp时,会给jsp提供一些什么java对象?

  查看_jspService方法可以看到,Web服务器在调用jsp时,会给Jsp提供如下的8个java对象

Java代码  收藏代码
  1. PageContext pageContext;  
  2.  HttpSession session;  
  3.  ServletContext application;  
  4.  ServletConfig config;  
  5.  JspWriter out;  
  6.  Object page = this;  
  7.  HttpServletRequest request,   
  8.  HttpServletResponse response  


  其中page对象,request和response已经完成了实例化,而其它5个没有实例化的对象通过下面的方式实例化

Java代码  收藏代码
  1. pageContext = _jspxFactory.getPageContext(this, request, response,nulltrue8192true);  
  2.  application = pageContext.getServletContext();  
  3.  config = pageContext.getServletConfig();  
  4.  session = pageContext.getSession();  
  5.  out = pageContext.getOut();  


这8个java对象在Jsp页面中是可以直接使用的,如下所示:

Java代码  收藏代码
  1. <%  
  2.         session.setAttribute("name""session对象");//使用session对象,设置session对象的属性  
  3.          out.print(session.getAttribute("name")+"<br/>");//获取session对象的属性  
  4.          pageContext.setAttribute("name""pageContext对象");//使用pageContext对象,设置pageContext对象的属性  
  5.          out.print(pageContext.getAttribute("name")+"<br/>");//获取pageContext对象的属性  
  6.         application.setAttribute("name""application对象");//使用application对象,设置application对象的属性  
  7.         out.print(application.getAttribute("name")+"<br/>");//获取application对象的属性  
  8.         out.print("Hello Jsp"+"<br/>");//使用out对象  
  9.         out.print("服务器调用index.jsp页面时翻译成的类的名字是:"+page.getClass()+"<br/>");//使用page对象  
  10.         out.print("处理请求的Servlet的名字是:"+config.getServletName()+"<br/>");//使用config对象  
  11.         out.print(response.getContentType()+"<br/>");//使用response对象  
  12.         out.print(request.getContextPath()+"<br/>");//使用request对象  
  13. %>  


Tomcat服务器的执行流程
第一次执行:

客户端通过电脑连接服务器,因为是请求是动态的,所以所有的请求交给WEB容器来处理
在容器中找到需要执行的*.jsp文件
之后*.jsp文件通过转换变为*.java文件
*.java文件经过编译后,形成*.class文件
最终服务器要执行形成的*.class文件

第二次执行:

因为已经存在了*.class文件,所以不在需要转换和编译的过程

修改后执行:

       1.源文件已经被修改过了,所以需要重新转换,重新编译。

分享到:
评论

相关推荐

    了解JSP工作原理.ppt

    了解JSP工作原理 JSP(Java Server Pages)是一种服务器端的WEB程序开发技术,由Sun公司发布,具有跨平台、通用性好、安全可靠等特点。JSP是基于Java的,页面程序由HTML或XML标记和JSP脚本共同组成,文件以“ .jsp...

    jsp工作原理

    ### JSP工作原理详解 #### 一、JSP的基本概念 JSP(JavaServer Pages)是一种基于Java技术的Web开发技术,它允许开发者在HTML页面中嵌入Java代码,从而实现动态网页的功能。JSP技术使得开发人员能够在静态网页的...

    JSP的工作原理.pptx

    ### JSP工作原理 1. **服务器端运行**: JSP应用程序是在服务器端运行的,当用户通过浏览器发送请求到服务器,服务器负责处理这些请求并生成响应。响应通常是以HTML格式返回给客户端的浏览器,用户在浏览器中看到的...

    jsp基础(jsp语法工作原理及基本规范)

    ### JSP基础(jsp语法工作原理及基本规范) #### 一、JSP简介 JavaServer Pages (JSP) 是一种基于Java技术的服务器端脚本技术,它允许开发者在HTML文档中嵌入Java代码,从而创建动态网页。JSP页面在第一次被请求时...

    JSP开发技术大全(一本最好的学习JSP的书)

    学习JSP开发的最佳参考资料,解压密码为www.csai.cn。

    易语言jsp编辑器

    这有助于提升开发者对易语言的掌握程度,同时也能加深对JSP工作原理的理解。 在源码中,我们可以看到易语言是如何处理JSP文件的解析、编辑和保存等操作的。易语言提供了丰富的API接口,使得开发者可以方便地进行...

    jsp实现-交易平台JSP源码

    1. **JSP工作原理** JSP是Java技术的一部分,它允许开发者将静态内容(HTML、CSS、JavaScript)与动态内容(Java代码)混合编写。当用户请求一个JSP页面时,服务器会将其转换成一个Servlet(Java类),然后编译并...

    JSP+JavaBean+Servlet工作原理

    总结起来,JSP+JavaBean+Servlet的工作原理是一个典型的Web应用程序开发模型,通过合理的分工,实现了高效、可维护的动态网站开发。在实际应用中,开发者通常会结合其他框架,如Spring MVC,进一步提升开发效率和...

    jsp高级开发与教程

    通过实例,读者可以学习如何在实际项目中有效地运用JSP,从而加深对JSP工作原理的理解。实例通常会涉及以下几个方面: 1. JSP基本语法:包括脚本元素(scriptlets、表达式、声明)、指令(page、include、taglib)...

    Jsp站内搜索实例(Jsp)

    **JSP工作原理**: JSP文件首先被JSP容器(如Tomcat)转换成Servlet,然后编译成Java字节码执行。在执行过程中,JSP的静态部分(HTML、CSS、JavaScript)会被直接发送到浏览器,而动态部分(Java代码)则会在服务器...

    jsp课程资料源代码

    **二、JSP工作原理** 当用户请求一个JSP页面时,服务器会将JSP转换为一个Servlet,然后执行Servlet的service方法生成响应。这个过程叫做JSP的编译阶段。生成的Servlet类会处理后续的所有请求,直到服务器重启或者JSP...

    JSP 英文文献+翻译1

    **JSP工作原理** JSP页面在服务器端被编译为一个Servlet,这个Servlet负责处理用户的请求和生成响应。当客户端发送请求到服务器时,服务器会执行JSP页面对应的Servlet,并将结果以HTML的形式返回给浏览器。这一过程...

    jsp编辑器.rar

    9. **源码分析**:通过分析"易语言jsp编辑器源码",可以了解编辑器如何实现这些功能,比如解析JSP语法、与服务器交互、处理用户输入等,对于学习JSP编辑器开发或者理解JSP工作原理都非常有帮助。 10. **开源项目**...

    jsp_手册

    通过解压并部署到Web服务器,可以直接运行这些例子,从而加深对JSP工作原理的理解。 总之,JSP_手册提供的资料全面覆盖了JSP 2.0的核心概念和实践,对于任何想要掌握或提升JSP技能的开发者来说,都是宝贵的资源。...

    分享JSP学习经验总结

    ### 一、JSP 工作原理 1. **JSP 文件转换**:当JSP文件首次被请求时,JSP引擎(通常是一个名为JspServlet的特殊Servlet)会将JSP文件转化为一个Servlet源码。如果在转化过程中出现语法错误,引擎会终止转换并向...

    jsp下载

    JSP工作原理 - **翻译阶段**:当JSP第一次被请求时,容器(如Tomcat)将其转换为一个Java Servlet类。 - **编译阶段**:Servlet类被Java编译器编译成字节码。 - **加载与实例化**:服务器创建Servlet的实例,并...

    jsp时尚编程百例

    这些基础知识是理解JSP工作原理的关键。 JSP中的核心概念包括JSP指令、脚本元素和动作。JSP指令如`&lt;%@ page %&gt;`, `&lt;%@ include %&gt;`, 和`&lt;jsp:forward&gt;`,用于设置页面属性、引入其他文件或控制页面流程。脚本元素则...

    简单jsp应用

    2. JSP工作原理 - JSP翻译:当客户端请求JSP页面时,服务器会将JSP文件转换成一个Servlet源码文件。 - 编译与执行:Servlet源码被编译成字节码,并在服务器上运行,生成HTML响应发送回客户端。 3. JSP生命周期 -...

    jsp运行原理

    ### JSP运行原理详解 #### 一、JSP的基本概念 JSP(Java Server Pages)是一种结合了HTML标记和Java代码的技术,用于创建动态网页。它允许开发人员将业务逻辑和展示逻辑分离,使得Web应用更加模块化和易于维护。 ...

Global site tag (gtag.js) - Google Analytics