zoukankan      html  css  js  c++  java
  • Jsp的原理

    在学习javaweb中jsp这块的时候,会详细了解到jsp的内容,一般,从网上的解释来看,说的比较难懂,JSPJSP教程


    可以看到这样的解释:

    JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。JSP技术有点类似ASP技术,它是在传统的网页HTML(标准通用标记语言的子集)文件(*.htm,*.html)中插入Java程序段(Scriptlet)和JSP标记(tag),从而形成JSP文件,后缀名为(*.jsp)。 用JSP开发的Web应用是跨平台的,既能在Linux下运行,也能在其他操作系统上运行。


    其实啊,JSP是一种特殊的Servlet,当JSP页面首次被访问时,容器(Tomcat)会先把JSP编译成Servlet,然后再去执行Servlet。所以JSP其实就是一个Servlet!


    当你去查看tomcat下的work目录,你随便找一个带有jsp的项目,打开来看,比如我的miniblog项目,


    当客户端访问find.jsp时,服务器通过find.jsp产生find_jsp.java,即servlet,再把find_jsp.java编译成find_jsp.class,最后通过find_jsp.class完成响应,


    来看一看jsp(find_jsp.java)里面的内容就明白了:

    /*
     * Generated by the Jasper component of Apache Tomcat
     * Version: Apache Tomcat/7.0.42
     * Generated at: 2016-10-25 05:29:30 UTC
     * Note: The last modified time of this file was set to
     *       the last modified time of the source file after
     *       generation to assist with modification tracking.
     */
    package org.apache.jsp;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    import java.util.*;
    
    public final class find_jsp extends org.apache.jasper.runtime.HttpJspBase
        implements org.apache.jasper.runtime.JspSourceDependent {
    
      private static final javax.servlet.jsp.JspFactory _jspxFactory =
              javax.servlet.jsp.JspFactory.getDefaultFactory();
    
      private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
    
      private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody;
      private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fitems;
      private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fc_005fchoose;
      private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fc_005fwhen_0026_005ftest;
      private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fc_005fotherwise;
      private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fc_005fset_0026_005fvar_005fvalue_005fnobody;
      private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fc_005fif_0026_005ftest;
      private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fend_005fbegin;
    
      private javax.el.ExpressionFactory _el_expressionfactory;
      private org.apache.tomcat.InstanceManager _jsp_instancemanager;
    
      public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
        return _jspx_dependants;
      }
    
      public void _jspInit() {
        _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
        _005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fitems = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
        _005fjspx_005ftagPool_005fc_005fchoose = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
        _005fjspx_005ftagPool_005fc_005fwhen_0026_005ftest = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
        _005fjspx_005ftagPool_005fc_005fotherwise = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
        _005fjspx_005ftagPool_005fc_005fset_0026_005fvar_005fvalue_005fnobody = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
        _005fjspx_005ftagPool_005fc_005fif_0026_005ftest = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
        _005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fend_005fbegin = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());
        _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
        _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
      }
    
      public void _jspDestroy() {
        _005fjspx_005ftagPool_005fc_005furl_0026_005fvalue_005fnobody.release();
        _005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fitems.release();
        _005fjspx_005ftagPool_005fc_005fchoose.release();
        _005fjspx_005ftagPool_005fc_005fwhen_0026_005ftest.release();
        _005fjspx_005ftagPool_005fc_005fotherwise.release();
        _005fjspx_005ftagPool_005fc_005fset_0026_005fvar_005fvalue_005fnobody.release();
        _005fjspx_005ftagPool_005fc_005fif_0026_005ftest.release();
        _005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fend_005fbegin.release();
      }
    
      public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
            throws java.io.IOException, javax.servlet.ServletException {
    
        final javax.servlet.jsp.PageContext pageContext;
        javax.servlet.http.HttpSession session = null;
        final javax.servlet.ServletContext application;
        final javax.servlet.ServletConfig config;
        javax.servlet.jsp.JspWriter out = null;
        final java.lang.Object page = this;
        javax.servlet.jsp.JspWriter _jspx_out = null;
        javax.servlet.jsp.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('
    ');
          out.write('
    ');
    
    	String path = request.getContextPath();
    	String basePath = request.getScheme() + "://"
    			+ request.getServerName() + ":" + request.getServerPort()
    			+ path + "/";
    
          out.write("
    ");
          out.write("
    ");
          out.write("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    ");
          out.write("<html>
    ");
          out.write("<head>
    ");
          out.write("
    ");
          out.write("<base href="");
          out.print(basePath);
          out.write("">
    ");
          out.write("
    ");
          out.write("<meta charset="UTF-8">
    ");
          out.write("<link rel="icon" href="assets/img/favicon.ico">
    ");
          out.write("<title>miniblog-find</title>
    ");
          out.write("<meta http-equiv="pragma" content="no-cache">
    ");
          out.write("<meta http-equiv="cache-control" content="no-cache">
    ");
          out.write("<meta http-equiv="expires" content="0">
    ");
          out.write("<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    ");
          out.write("<meta http-equiv="description" content="This is my page">
    ");
    当然,这只是片段,但我们可以看出,你会发现,在JSP中的静态信息(例如<html>等)在“真身”中都是使用out.write()完成打印!这些静态信息都是作为字符串输出给了客户端。JSP的整篇内容都会放到名为_jspService的方法中!


    所以,从上面我们可以看出,jsp实际上通过servlet来完成的,这也是我们为什么将之称其为运行在服务端的语言(Java Server Pages)。

  • 相关阅读:
    MySQL事务隔离级别和MVCC
    windows环境下查看端口是否被占用
    jar找不到问题解决
    Java注解
    Fastjson
    mybatis
    Idea快捷键
    Date与String的相互转换
    Windows快捷键
    [转]linux awk命令详解
  • 原文地址:https://www.cnblogs.com/Arry10/p/7731800.html
Copyright © 2011-2022 走看看