回顾:Servlect常用的对象,过滤器,监听器。
JSP内置对象
到服务器中的work文件中去看看
打开文件中的Out_jsp_java
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/9.0.53 * Generated at: 2021-11-11 09:23:21 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.Calendar; public final class Out_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { 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.util.Calendar"); } 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() { } 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("\r\n"); out.write("\r\n"); out.write("<!DOCTYPE html>\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write("<meta charset=\"UTF-8\">\r\n"); out.write("<title>Out对象</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); out.write('\r'); out.write('\n'); //创建日历类 Calendar calendar =Calendar.getInstance(); int AM_PM=calendar.get(calendar.AM_PM); if(AM_PM==calendar.AM){ out.write("<h1 style=' color:red'>上午时间</h1>"); }else if(AM_PM==calendar.PM){ out.write("<h1 style=' color:green'>下午时间</h1>"); } out.write("\r\n"); out.write("\r\n"); out.write("<h1>\r\n"); out.write("四大作用域(有四个对象)有四个对象共享数据\r\n"); out.write("<hr>\r\n"); out.write("seesion 浏览器不切\r\n"); out.write("<hr>\r\n"); out.write("application 服务器不关闭\r\n"); out.write("<hr>\r\n"); out.write("request:请求转发跳转页面的情况,只能两个页面共享数据\r\n"); out.write("<hr>\r\n"); out.write("pageContext:该对象中只能页面自己用\r\n"); out.write("<hr>\r\n"); out.write("才能用setAttribute的方法\r\n"); out.write("<hr>\r\n"); out.write("将四作用域排序\r\n"); out.write("<hr>\r\n"); out.write("pageContext request seesion application \r\n"); out.write("</h1>\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 { 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); } } }
从上面代码中我们可以找到这些对象
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;
什么是内置对象,在jsp文件运行后,会翻译成一个Servlect类,
在这个类中可以直接调用的对象
九大内置对象的核心表格
四大作用域(有四个对象)有四个对象共享数据
seesion 浏览器不切
application 服务器不关闭
request:请求转发跳转页面的情况,只能两个页面共享数据
pageContext:该对象中只能页面自己用
才能用setAttribute的方法
将四作用域排序
pageContext<request<seesion<application
这些对象应用的方式和Servlect相似
回顾jsp一些重要标签和指令
<%! %>:用于在jsp文件中定义全局变量和方法
<%= %>:用于在jsp文件中将java中的数据结果显示在html文本中
<% %>:用于在jsp文件中执行多行代码,包括if,while,for,创建对象等等
<%-- --%>:用于在jsp文件中的注释
<%@ %>:用于在jsp文件中操作指令代码
Jsp常用的三大指令:page,include,taglib
page指令的常用属性:
import:导入所需要的包
language:当前jsp页面里面可以嵌套的语言
contenType:设置响应流的编码,及通知浏览器用什么编码打开
pageEncoding:设置页面的编码
extends:声明当前jsp的页面继承于哪个类
session:设置jsp页面是否可以使用session内置对象
buffer:设置jsp页面的流的缓冲区大小
autoFlush:是否自动刷新
isELIgnored:是否忽略el表达式
errorPage:当前jsp页面出现异常的时候要跳转到的jsp页面
isErrorPage:当前jsp页面是否是一个错误页面
运行页面结果
代码区
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1 style="color: red;">欢迎来到a.jsp文件</h1> <% String a=(String)request.getAttribute("a"); out.write("通过请求转发获得的数据为"+a); %> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <%-- application, ServlectContext 对象八ServlectContext 共享数据 整个项目中只会有一个对象 tomcat 服务器 一关闭 数据释放 数据的共享更服务器相关 --%> <% //获得静态数据 String info =application.getInitParameter("a"); out.write(info); //动态数据 out.write("<hr>"); application.setAttribute("c", "我是动态数据"); out.write((String)application.getAttribute("c")); %> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <%-- config 第七个对象 类名ServlectConfig 增加静态数据 --%> <% String name = config.getServletName(); out.write(name); %> </body> </html>
<%@ page language="java" errorPage="error.jsp" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> *{ font-size: 29px; color: red; } </style> </head> <body> <%-- <%int a=100/0; %> --%> <hr> <% int [] arr =new int [3]; System.out.println(arr[3]); %> <hr> <%-- <% Integer i =new Integer ("abd"); %> --%> <hr> </body> </html>
<%@ page language="java" isErrorPage="true" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1> 除数不能为0:用户 </h1> <h1><%= exception.getMessage() %></h1> <h3><%=exception.toString() %></h3> <h4><%=exception.hashCode()%></h3> </body> </html>
<%@page import="java.util.Calendar"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Out对象</title> </head> <body> <%--<!-- jsp九大对象之一:out对象 对应的类 JspWripiter --> --%> <% //创建日历类 Calendar calendar =Calendar.getInstance(); int AM_PM=calendar.get(calendar.AM_PM); if(AM_PM==calendar.AM){ out.write("<h1 style=' color:red'>上午时间</h1>"); }else if(AM_PM==calendar.PM){ out.write("<h1 style=' color:green'>下午时间</h1>"); } %> <h1> 四大作用域(有四个对象)有四个对象共享数据 <hr> seesion 浏览器不切 <hr> application 服务器不关闭 <hr> request:请求转发跳转页面的情况,只能两个页面共享数据 <hr> pageContext:该对象中只能页面自己用 <hr> 才能用setAttribute的方法 <hr> 将四作用域排序 <hr> pageContext request seesion application </h1> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <%--对象 page 对象 六 page, Servlect(this) jsp翻译出来类的对象 --%> <% out.write((page.equals(this)?"page是当前对象":"page不是当前对象")); %> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Request对象</title> </head> <body> <%-- jsp九大对象 request 类名HttpSevlectRequest --%> <% String name=request.getParameter("name"); out.write("<h1>我收到浏览器发送的数据为</h1>"+name); out.write("<h2 style='color:red'>http://localhost:8080/Day_13/Request.jsp?name=xao</h2>"); //携带数据跳转页面 request.setAttribute("a", "apple"); request.getRequestDispatcher("a.jsp").forward(request, response); %> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <%-- jsp九大内置对象 Respone对象三类名 HttpServlectRespone--%> <h1 style="color: red;">Respone.jsp</h1> <% response.sendRedirect("Out.jsp"); out.write("Hellow JSP"); %> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <%---- 对象四Session HttpSession Session放的数据为同已过浏览器服务器 所有Servlect 和 jsp 都能共享Session里数据 --%> <% session.setAttribute("b", "我是session对象的定义b"); session.setAttribute("c", "我是session对象的定义c"); out.write((String)session.getAttribute("b")); out.write((String)session.getAttribute("c")); %> </body> </html>