JSP概述

5 篇文章 0 订阅


  1. Jsp是什么

  • Jsp本质上是一个Servlet
  • HTML文档中嵌入Java代码,来生成动态网页
  • 运行流程

    1. 在web应用内编写Jsp源文件
    2. 部署到Tomcat
    3. 在浏览器输入相应URL来访问这个Jsp文件
    4. 查找与Jsp文件对应的Servlet,如果已经存在,就调用它的服务方法
    5. 如果Jsp已经更新或者还不存在Servlet,就解析文件系统中的Jsp文件,翻译为Servlet并编译此Servlet,然后初始化并运行此Servlet
    • Jsp翻译为Servlet,一般只发生在客户端首次访问和Jsp文件有更新时
  • 简单解析Jsp对应的Servlet
    Jsp文件:hello.jsp
  • <%@ page language="java" import="java.io.File" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>lifeJSP</title>
    </head>
    <body>
        <%!
            int initVar =  0;
            int serivceVar = 0;
            int destryVar = 0;
        %>
        <%!
            public void jspInit() {
                ++initVar;
                System.out.println("JspInit()被初始化  " + initVar +" 次");
            }
            public void jspDestory() {
                ++destryVar;
                System.out.println("JspDestory()被销毁 " + destryVar + "次");
            }
        %>
    
        <%
            ++serivceVar;
            System.out.println("JspSerivce()响应了" + serivceVar + "次");
            String initTime = "JspInit()被初始化  " + initVar +" 次";
            String destoryTime = "JspDestory()被销毁 " + destryVar + "次";
            String sericetime = "JspSerivce()响应了" + serivceVar + "次";
            File temp = (File)application.getAttribute("javax.servlet.context.tempdir");
            String workdir = "工作目录:" + temp.getPath();
        %>
        <h1><%= workdir %></h1>
        <h1><%= initTime %></h1>
        <h1><%= destoryTime %></h1>
        <h1><%= sericetime %></h1>
    </body>
    </html>

    对应的Servlet

    
    package org.apache.jsp;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    
    public final class hello_jsp extends org.apache.jasper.runtime.HttpJspBase
        implements org.apache.jasper.runtime.JspSourceDependent,
                     org.apache.jasper.runtime.JspSourceImports {
    
      public void _jspInit() {
      }
    
      public void _jspDestroy() {
      }
    
      public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
          throws java.io.IOException, javax.servlet.ServletException {
    
        final java.lang.String _jspx_method = request.getMethod();
        if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
          response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");
          return;
        }
    
        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("\r\n");
          out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
          out.write("<html>\r\n");
          out.write("<head>\r\n");
          out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
          out.write("<title>Insert title here</title>\r\n");
          out.write("</head>\r\n");
          out.write("<body>\r\n");
          out.write("\t");
          out.print( "HelloWorld" );
          out.write("\r\n");
          out.write("</body>\r\n");
          out.write("</html>");
        } catch (java.lang.Throwable t) {
          if (!(t instanceof javax.servlet.jsp.SkipPageException)){
            out = _jspx_out;
            if (out != null && out.getBufferSize() != 0)
              try {
                if (response.isCommitted()) {
                  out.flush();
                } else {
                  out.clearBuffer();
                }
              } catch (java.io.IOException e) {}
            if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
            else throw new ServletException(t);
          }
        } finally {
          _jspxFactory.releasePageContext(_jspx_page_context);
        }
      }
    }
    • 由于原Servlet文件内容太多,这里就只保留了了一部分
    • 与Jsp文件对应的Servlet类继承org.apache.jasper.runtime.HttpJspBase,HttpJspBase由Tomcat提供,它实现了Servlet API中的javax.servlet.jsp.HttpJspPage接口,该接口继承了javax.servlet.jsp.JspPage接口,而JspPage又继承了javax.servlet.Servlet接口
    • 方法_jspInit()Jsp对应Servlet的初始化方法
    • 方法_jspDestory()Jsp对应Servlet的销毁方法
    • 方法_jspService()Jsp对应Servlet的服务方法
    • Jsp对应的Servlet把Jsp文件中的HTML标记都作为打印流的内容输出到客户端
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值