JSP的相关知识
前提:
如果对于servlet有一定的了解之后,再来学习JSP会是一件很轻松的事情
JSP(Java Server Page ),本质上就是一个servlet,也是sun公司定义的一种开发动态web资源的技术。
先来看看JSP的基本原理,为什么说它实际上就是一个servlet呢,因为在访问jsp页面的时候,就以tomcat服务器来说,它会先把jsp文件给翻译成为java文件,然后再对java文件进行编译,产生字节码文件。简单说,就是jsp文件会变成一个java,所以本质上它就是一个servlet类。
JSP文件中,含有html代码,这是网页的骨架。同时又含有java程序段,用来实现前后端的交互,达到动态网页的效果。但是你不能就这么直接把java代码混在html中,那不就变成了文本直接输出了?所以这时候,你需要用到一些其他的语法来实现。
下面来看个小实例
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>Insert title here</title>
</head>
<body>
<%! int a = 1; %>
<% List list = new ArrayList(); %>
<% int b = 2; %>
<%out.print(a); %>
<%=b %>
</body>
</html>
咱们先不管它具体如何,只要一打开翻译之后的java源文件,就会一目了然。当然首先你要把这个项目给部署到tomcat服务器上,然后再访问这个jsp页面,然后到tomcat目录下的work目录,这是tomcat的工作目录,里面找到你的项目,最终会有一个jsp目录,里面存放着翻译过来的java文件。
打开这个java文件看一看
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/9.0.1
* Generated at: 2017-12-17 12:00:02 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.ArrayList;
import java.util.List;
// 一个final类,继承了 HttpJspBase,其实这个类也是继承了Servlet的
public final class _1_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports {
int a = 1; //这一句对应的就是JSP中的 <%! int a = 1; %> 用于声明成员变量和方法
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
//对应<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
//引入 jstl标签库,其实是封装到了一个Map集合中
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
static {
_jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(2);
_jspx_dependants.put("/WEB-INF/lib/standard.jar", Long.valueOf(1513507951000L));
_jspx_dependants.put("jar:file:/Users/sam/Library/apache-tomcat-9.0.1/webapps/day10_2/WEB-INF/lib/standard.jar!/META-INF/c.tld", Long.valueOf(1098682290000L));
}
//******************中间省略一些代码**************
//重写init()方法
public void _jspInit() {
}
//重写destroy()方法
public void _jspDestroy() {
}
//重写service()方法
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;
}
//jsp中的一些内置对象,pageContext,session,application,out等
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往浏览器写入html标签
out.write("\n");
out.write("\n");
out.write("\n");
out.write("\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
out.write("<html>\n");
out.write("<head>\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
out.write("<title>Insert title here</title>\n");
out.write("</head>\n");
out.write("<body>\n");
out.write(" ");
out.write("\n");
out.write(" \n");
out.write(" ");
//对应的 <% List list = new ArrayList(); %>
List list = new ArrayList();
out.write("\n");
out.write("\n");
out.write(" ");
//对应的<% int b = 2; %>
int b = 2;
out.write("\n");
out.write(" \n");
out.write(" ");
//对应的<%out.print(a); %>
out.print(a);
out.write("\n");
out.write(" ");
//对应的<%=b %>
out.print(b );
out.write("\n");
out.write("</body>\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);
}
}
}
对比着源码来看,就不难理解和记忆JSP中的语法。当然这里只是粗浅的介绍一下JSP和Servlet之间的关联。JSP的工作原理即是如此,jsp标签中的java代码会和html的混在一起,先被tomcat翻译成java文件,然后把所有的html中的标签或者文字一并通过out输出到浏览器交给浏览器进行解析。