概念
- Java server pages:Java服务器端页面
可以理解为:一个特殊的页面,其中可以指定 定义html标签,又可以定义 java代码
用于简化书写.
原理
- 服务器解析请去消息,找是否有 index.jsp 资源.
- 如果找到了,会将 index.jsp 转换为 .java文件
- 编译 .java文件,生成 .class字节码文件
- 由字节码文件提供访问
//启动 tomcat的时候可以查看,关闭的时候也可以
//在 idea中:Using CATALINA_BASE: 配置目录和源码中
//该目录的 work子目录是服务器解析的源码
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports {}
jsp 的脚本
- jsp定义 Java代码的方式:
<% Java代码 %> :定义的 Java代码,在 service方法中,service方法中可以定义什么,该脚本就可以定义什么.
<%! Java代码 %> :定义的Java代码,在 jsp转换后的Java类的成员位置.
<%= Java代码 %>:定义的Java代码,会输出到页面上.输出语句中可以定义什么,该脚本就可以定义什么.
<%@ page import="java.sql.SQLOutput" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<%System.out.println("hello jsp");%>
<%! int a = 12; %>
<%= a %>
</body>
</html>
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.sql.SQLOutput;
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports {
//这里定义了成员变量
int a = 12;
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 static final java.util.Set<java.lang.String> _jspx_imports_packages;
private static final java.util.Set<java.lang.String> _jspx_imports_classes;
static {
_jspx_imports_packages = new java.util.HashSet<>();
_jspx_imports_packages.add("javax.servlet");
_jspx_imports_packages.add("javax.servlet.http");
_jspx_imports_packages.add("javax.servlet.jsp");
_jspx_imports_classes = new java.util.HashSet<>();
_jspx_imports_classes.add("java.sql.SQLOutput");
}
private volatile javax.el.ExpressionFactory _el_expressionfactory;
private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public java.util.Set<java.lang.String> getPackageImports() {
return _jspx_imports_packages;
}
public java.util.Set<java.lang.String> getClassImports() {
return _jspx_imports_classes;
}
public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
if (_el_expressionfactory == null) {
synchronized (this) {
if (_el_expressionfactory == null) {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
}
}
}
return _el_expressionfactory;
}
public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
if (_jsp_instancemanager == null) {
synchronized (this) {
if (_jsp_instancemanager == null) {
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
}
}
return _jsp_instancemanager;
}
public void _jspInit() {
}
public void _jspDestroy() {
}
//主要看这个方法:我们可以使用 request, response
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
if (!javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
final java.lang.String _jspx_method = request.getMethod();
if ("OPTIONS".equals(_jspx_method)) {
response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
return;
}
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) {
response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS");
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("\n");
out.write("\n");
out.write("<html>\n");
out.write("<head>\n");
out.write(" <title>首页</title>\n");
out.write("</head>\n");
out.write("<body>\n");
//输出语句
System.out.println("hello jsp");
out.write('\n');
out.write('\n');
//打印成员变量
out.print( a );
out.write("\n");
out.write("</body>\n");
out.write("</html>\n");
} 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页面中不需要获取和创建,可以直接使用的对象
- 九大内置对象
- request :HttpServletRequest :请求对象/获取请求资源 request域对象(一次请求多个资源共享)
- response :HttpServletResponse :响应对象/响应响应的数据封装到响应对象
- session :HttpSession :会话对象/实现多个用户之间数据共享
- out :JspWriter (把他当作流对象字符输出流)可以间数据输出到页面上.和 response.getWriter()类似.
response.getWriter() 和 out.write() 的区别
在 tomcat服务器真正给客户端做出响应之前,会先找 out缓冲区数据.response.getWriter()数据输出永远在 out.writer()之前 - config :ServletConfig :ServletConfig对象/配置Servlet信息
- exception :Throwable :异常对象/当页面需要声明 isErrorPage=true
- pageContext :PageContext :jsp的上下文对象/当前页面实现数据共享
- application :ServletContext :ServletContext对象/全局域对象代表整个web应用
- page :Object :之当前jsp的对象