javaWEB总结(11):JSP简介及原理

2017-01-13 15:02:17来源:csdn作者:daochuwenziyao人点击


前言

本文主要通过一个简单小例子,介绍JSP的原理。



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,

找到类似于下面的地址:


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


我们看到有一个login_jsp.java文件,打开它。



6.login_jsp.java

login_jsp继承了HttpJspBase,实现了JspSourceDependent接口,里面有类似于servlet的_JspInit(),_JspDestroy等方法。


/*
* 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接口


/**/ 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页面的方式编写。


(2)新建一个jsp页面,在<% %>中即可编写java代码。


(3)jsp可以放在web应用程序中除了WEB-INF及其子目录以外的其他任何目录中。


(4)JSP运行原理:JSP本质上是一个servlet,每个JSP在第一次被访问时,JSP引擎将它翻译成一个servlet源程序,接着再把servlet源程序翻译成servlet的class类文件,然后由web容器(Servlet引擎)像调用普通Servlet一样来装载和解释执行这个由JSP页面翻译成的Servlet程序。




参考视频:点击打开链接

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台