本文转自与博客园一杯凉茶的博客
前面讲解了servlet,了解了Servlet的继承结构,生命周期等,并且在其中的ServletConfig和ServletContext对象有了一些比较详细的了解,但是我们会发现在Servlet中编写一些HTML代码,是很不方便的一件事情,每次都需要out.println(HTML); 因此就出现了jsp,来解决这样的问题,JSP中的内容就是html,但是能够嵌套Java语言,现在来详细了解一下JSP。
一、JSP1、什么是JSP?
JSP(Java Server Pages):是以Java语言为基础的动态网页开发技术,
特点:
Servlet特点:在Java源码中嵌入html源码
JSP特点:在html源码中嵌入java代码
JSP就是Servlet
1、tomcat获得JSP文件后,先将JSP转成servlet,变成xxx.java(servlet源码),
D:\java\tomcat7.0\apache-tomcat-7.0.53\apache-tomcat-7.0.53\work\Catalina\localhost\test01\org\apache\jsp
tomcat安装目录 引擎 主机 项目 固定包名 这个之下就是存放着jsp变成的servlet文件.java和编译文件.class
2、tomcat将java文件编译成class文件
3、tomcat运行class文件,并将结果输出到浏览器,
实例:
创建一个jsp。查看其转换后的servlet代码。
NewFile.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 hahaha 11 </body> 12 </html>
NewFile_jsp.java
1 /* 2 * Generated by the Jasper component of Apache Tomcat 3 * Version: Apache Tomcat/7.0.53 4 * Generated at: 2017-02-22 02:09:08 UTC 5 * Note: The last modified time of this file was set to 6 * the last modified time of the source file after 7 * generation to assist with modification tracking. 8 */ 9 package org.apache.jsp; 10 11 import javax.servlet.*; 12 import javax.servlet.http.*; 13 import javax.servlet.jsp.*; 14 15 public final class NewFile_jsp extends org.apache.jasper.runtime.HttpJspBase 16 implements org.apache.jasper.runtime.JspSourceDependent { 17 18 private static final javax.servlet.jsp.JspFactory _jspxFactory = 19 javax.servlet.jsp.JspFactory.getDefaultFactory(); 20 21 private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; 22 23 private javax.el.ExpressionFactory _el_expressionfactory; 24 private org.apache.tomcat.InstanceManager _jsp_instancemanager; 25 26 public java.util.Map<java.lang.String,java.lang.Long> getDependants() { 27 return _jspx_dependants; 28 } 29 30 public void _jspInit() { 31 _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); 32 _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); 33 } 34 35 public void _jspDestroy() { 36 } 37 38 public void _jspService(final javax.servlet.http.HttpServletrequest request, final javax.servlet.http.HttpServletResponse response) 39 throws java.io.IOexception, javax.servlet.ServletException { 40 41 final javax.servlet.jsp.PageContext pageContext; 42 javax.servlet.http.HttpSession session = null; 43 final javax.servlet.ServletContext application; 44 final javax.servlet.ServletConfig config; 45 javax.servlet.jsp.JspWriter out = null; 46 final java.lang.Object page = this; 47 javax.servlet.jsp.JspWriter _jspx_out = null; 48 javax.servlet.jsp.PageContext _jspx_page_context = null; 49 50 51 try { 52 response.setContentType("text/html; charset=UTF-8"); 53 pageContext = _jspxFactory.getPageContext(this, request, response, 54 null, true, 8192, true); 55 _jspx_page_context = pageContext; 56 application = pageContext.getServletContext(); 57 config = pageContext.getServletConfig(); 58 session = pageContext.getSession(); 59 out = pageContext.getOut(); 60 _jspx_out = out; 61 62 out.write("\r\n"); 63 out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"); 64 out.write("<html>\r\n"); 65 out.write("<head>\r\n"); 66 out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n"); 67 out.write("<title>Insert title here</title>\r\n"); 68 out.write("</head>\r\n"); 69 out.write("<body>\r\n"); 70 out.write("\thahaha\r\n"); 71 out.write("</body>\r\n"); 72 out.write("</html>"); 73 } catch (java.lang.Throwable t) { 74 if (!(t instanceof javax.servlet.jsp.SkipPageException)){ 75 out = _jspx_out; 76 if (out != null && out.getBufferSize() != 0) 77 try { out.clearBuffer(); } catch (java.io.IOException e) {} 78 if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); 79 else throw new ServletException(t); 80 } 81 } finally { 82 _jspxFactory.releasePageContext(_jspx_page_context); 83 } 84 } 85 }
可以看到public final class NewFile_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent
NewFile_jsp.java继承自HttpJspBase。来看看HttpJspBase的源码
HttpJspBase.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 29 import org.apache.jasper.compiler.Localizer; 30 31 /** 32 * This is the super class of all JSP-generated servlets. 33 * 34 * @author Anil K. Vijendran 35 */ 36 public abstract class HttpJspBase extends HttpServlet implements HttpJspPage { 37 38 private static final long serialVersionUID = 1L; 39 40 protected HttpJspBase() { 41 } 42 43 @Override 44 public final void init(ServletConfig config) 45 throws ServletException 46 { 47 super.init(config); 48 jspInit(); 49 _jspInit(); 50 } 51 52 @Override 53 public String getServletInfo() { 54 return Localizer.getMessage("jsp.engine.info"); 55 } 56 57 @Override 58 public final void destroy() { 59 jspDestroy(); 60 _jspDestroy(); 61 } 62 63 /** 64 * Entry point into service. 65 */ 66 @Override 67 public final void service(HttpServletRequest request, HttpServletResponse response) 68 throws ServletException, IOException 69 { 70 _jspService(request, response); 71 } 72 73 @Override 74 public void jspInit() { 75 } 76 77 public void _jspInit() { 78 } 79 80 @Override 81 public void jspDestroy() { 82 } 83 84 protected void _jspDestroy() { 85 } 86 87 @Override 88 public abstract void _jspService(HttpServletRequest request, 89 HttpServletResponse response) 90 throws ServletException, IOException; 91 }
public abstract class HttpJspBase extends HttpServlet implements HttpJspPage
看到了一个熟悉的类,HttpServlet,我们编写Servlet时就是继承自该类,这里也是继承HttpServlet,并且HttpJspBase的源码会发现,生命周期也是有init()方法,service()方法,destory()方法,相当于_jspService()方法就是servlet的service()方法的执行,所以说JSP也是一个servlet。
我们在JSP写的所有html代码,都会被转换为servlet中的out.write(html)代码来输出。看图
小总结:
对于jsp转换成的servlet源码,之后我们会在进行详细分析,现在只需要知道jsp中的内容在servlet中被转换成什么了,在哪里被转换了即可。其中_jspService()方法的详细内容下面会讲解
注意:jsp 生成java源码,默认第一次生成,之后直接执行,除非内容修改,具体点说,由于JSP只会在客户端第一次请求的时候被编译,因此第一次请求JSP时会感觉比较慢,而之后的请求因为不会编译JSP,所以速度就快多了,如果将Tomcat保存的JSP编译后的class文件删除,Tomcat也会重新编译JSP。在开发Web程序的时候经常需要修改JSP,Tomcat能够自动检测到JSP程序的改动,如果检测到JSP源代码发生了改动,Tomcat会在下次客户端请求JSP时重新编译JSP,而不需要重启Tomcat,这种自动检测功能默认是开启的,检测改动会消耗少量的时间,在部署web应用程序的时候可以在web.xml中将它关掉。这也就是为什么我们能够在jsp页面直接修改内容,而不用重新启动服务器的原因。
因为JSP就是servlet,那么生命周期也就是跟serlvet一样。
JSP和servlet有一点区别就在于:jsp是先部署后编译,而servlet是先编译后部署。
二、JSP语法JSP模版数据:
就是JSP中的HTML代码,它的内容给是固定的,无论程序如何运行模版数据输出到客户端浏览器时都不会发生改变,当我们创建一个JSP时,模版就已经固定了。
元素:JSP中的java部分,包括脚本(JavaScript,或者java代码)以及JSP指令(Directive)与JSP标签(Tag)等,元素决定着程序的流程,元素是不会显示到浏览器的。这几个都会在接下来讲解到
JSP脚本:
1、使用<% 编写java代码 %>,中间java代码必须遵循Java语法,