javaWEB總結(11):JSP簡介及原理

前言

本文主要經過一個簡單小例子,介紹JSP的原理。
html


1.項目結構




2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>javaWeb_09</display-name>
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

3.login.jsp

<%@ page language="java" 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=ISO-8859-1">
<title>my first jsp</title>
</head>
<body>
	<% out.write("Hello World") ;%>
</body>
</html>

4.運行後頁面顯示



5.打開eclipse的workspace,

找到相似於下面的地址:java

D:\Users\Administrator\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp10\work\Catalina\localhost\javaWeb_11\org\apache\jspweb


咱們看到有一個login_jsp.java文件,打開它。
express


6.login_jsp.java

login_jsp繼承了HttpJspBase,實現了JspSourceDependent接口,裏面有相似於servlet的_JspInit(),_JspDestroy等方法。
apache


/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/7.0.54
 * Generated at: 2016-11-26 07:51:43 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.*;

public final class login_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 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() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  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 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=ISO-8859-1\">\r\n");
      out.write("<title>my first jsp</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("\t");
 out.write("Hello World") ;
      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 { 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);
    }
  }
}

7.ctrl+shift+t找到HttpJspBase的源碼



8.HttpJspBase

很明顯HttpJspBase繼承了HttpServlet實現了HttpJspPage接口
tomcat


/*    */ 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
/*    */ {
/*    */   private static final long serialVersionUID = 1L;
/*    */   
/*    */   public final void init(ServletConfig config)
/*    */     throws ServletException
/*    */   {
/* 47 */     super.init(config);
/* 48 */     jspInit();
/* 49 */     _jspInit();
/*    */   }
/*    */   
/*    */   public String getServletInfo()
/*    */   {
/* 54 */     return Localizer.getMessage("jsp.engine.info");
/*    */   }
/*    */   
/*    */   public final void destroy()
/*    */   {
/* 59 */     jspDestroy();
/* 60 */     _jspDestroy();
/*    */   }
/*    */   
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */   public final void service(HttpServletRequest request, HttpServletResponse response)
/*    */     throws ServletException, IOException
/*    */   {
/* 70 */     _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;
/*    */ }

總結

(1)JSP是簡化Servlet編寫的一種技術,它將Java代碼和HTML語句混合在同一個文件中編寫,只對網頁中的要動態產生的內容採用Java代碼來編寫,而對固定不變的靜態內容採用普通靜態HTML頁面的方式編寫。
session

(2)新建一個jsp頁面,在<% %>中便可編寫java代碼。app

(3)jsp能夠放在web應用程序中除了WEB-INF及其子目錄之外的其餘任何目錄中。less

(4)JSP運行原理:JSP本質上是一個servlet,每一個JSP在第一次被訪問時,JSP引擎將它翻譯成一個servlet源程序,接着再把servlet源程序翻譯成servlet的class類文件,而後由web容器(Servlet引擎)像調用普通Servlet同樣來裝載和解釋執行這個由JSP頁面翻譯成的Servlet程序。eclipse



參考視頻:點擊打開連接

相關文章
相關標籤/搜索