JSP作用
JSP全称为Java Server Pages即Java服务器页面,主要作用是代替Servlet程序回传HTML页面的数据,因为Servlet程序回传HTML页面数据非常繁琐,开发成本和维护成本较高。
package com.pero.jsp.jsp_javaweb;import jakarta.servlet.*;
import jakarta.servlet.http.*;import java.io.IOException;
import java.io.PrintWriter;public class PringHtml extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//通过响应流回传html页面response.setContentType("text/html;charset=UTF-8");PrintWriter writer = response.getWriter();writer.write("<!DOCTYPE html>\r\n");writer.write("<html lang=\"en\">\r\n");writer.write("<head>\r\n");writer.write("<meta charset=\"UTF-8\">\r\n");writer.write("<title>Title</title>\r\n");writer.write("</head>\r\n");writer.write("<body>\r\n");writer.write("这是html界面\r\n");writer.write("</body>\r\n");writer.write("</html>\r\n");}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"version="5.0"><servlet><servlet-name>PringHtml</servlet-name><servlet-class>com.pero.jsp.jsp_javaweb.PringHtml</servlet-class></servlet><servlet-mapping><servlet-name>PringHtml</servlet-name><url-pattern>/pringhtml</url-pattern></servlet-mapping>
</web-app>
创建jsp文件流程:在webapp文件夹下new->JSP/JSPX
jsp页面的访问和html页面一样,都是存放在web目录下
a.html页面访问方式:http://ip:port/工程路径/a.html
b.jsp页面访问方式:http://ip:port/工程路径/b.jsp
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2023/3/13Time: 19:52To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>a.jsp页面
</body>
</html>
JSP本质
jsp页面本质是一个Servlet程序。第一次访问jsp页面的时候,Tomcat服务器会把jsp页面翻译成一个java源文件,并对其进行编译成为.class字节码程序,其内容如下:
在源码中HttpJspBase类直接继承了HttpServlet类,那么jsp翻译出来的java类简介继承了HttpServlet类,也就是说jsp是一个Servlet程序,在底层也是通过响应流把html页面数据回传给客户端。
/** Generated by the Jasper component of Apache Tomcat* Version: Apache Tomcat/10.0.27* Generated at: 2023-03-13 12:14:23 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 jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.jsp.*;public final class a_jsp extends org.apache.jasper.runtime.HttpJspBaseimplements org.apache.jasper.runtime.JspSourceDependent,org.apache.jasper.runtime.JspSourceImports {private static final jakarta.servlet.jsp.JspFactory _jspxFactory =jakarta.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("jakarta.servlet");_jspx_imports_packages.add("jakarta.servlet.http");_jspx_imports_packages.add("jakarta.servlet.jsp");_jspx_imports_classes = null;}private volatile jakarta.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 jakarta.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 jakarta.servlet.http.HttpServletRequest request, final jakarta.servlet.http.HttpServletResponse response)throws java.io.IOException, jakarta.servlet.ServletException {if (!jakarta.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 jakarta.servlet.jsp.PageContext pageContext;jakarta.servlet.http.HttpSession session = null;final jakarta.servlet.ServletContext application;final jakarta.servlet.ServletConfig config;jakarta.servlet.jsp.JspWriter out = null;final java.lang.Object page = this;jakarta.servlet.jsp.JspWriter _jspx_out = null;jakarta.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("<html>\r\n");out.write("<head>\r\n");out.write(" <title>Title</title>\r\n");out.write("</head>\r\n");out.write("<body>\r\n");out.write(" a.jsp页面\r\n");out.write("</body>\r\n");out.write("</html>\r\n");} catch (java.lang.Throwable t) {if (!(t instanceof jakarta.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的page指令
jsp的page指令可以修改jsp页面中一些重要的属性或行为。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
①language属性:表示jsp翻译后是什么语言文件,目前只支持java
②contentType属性:表示jsp返回的数据类型是什么,在源码中是response.setContentType()的参数值
③pageEncoding属性:表示当前jsp页面文件本身的字符集
④import属性:和java原码相同用于导入包或者类
⑤autoFlush属性:设置当out输出缓冲区满后是否自动刷新缓冲区,默认值是true
⑥buffer属性:设置out输出缓冲区的大小,默认值为8kb
★当缓冲区溢出时报错:
java.io.IOException: 错误:JSP缓冲区溢出org.apache.jasper.runtime.JspWriterImpl.bufferOverflow(JspWriterImpl.java:151)org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:330)java.base/java.io.Writer.write(Writer.java:249)org.apache.jsp.a_jsp._jspService(a_jsp.java:163)org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380)org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328)jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
⑦errorPage属性:设置当jsp运行出错时,会自动跳转到该属性配置的页面路径
⑧isErrorPage属性:设置当前jsp页面是否是错误信息页面,默认为false,如果为true可以获取异常信息
java.lang.Throwable exception = org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable(request);
⑨session属性:设置访问当前jsp页面是否会创建HttpSession对象,默认是true。
⑩extends属性:设置jsp翻译出来的java类默认继承谁。
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2023/3/13Time: 19:52To change this template use File | Settings | File Templates.
--%>
<%@page import="java.util.Map" %>
<%@ page contentType="text/html;charset=UTF-8"pageEncoding="utf-8"autoFlush="true"buffer="8kb"errorPage="/error500.jsp"session="true"language="java" %>
<!-- errorPage表示出现错误后自动跳转去的路径这个路径一般是/斜杠打头,表示请求地址为:http://ip:port/工程路径/映射到代码的Web目录
-->
<html>
<head><title>Title</title>
</head>
<body>a.jsp页面<%int i = 10/0;%><view>a.jsp页面</view>
</body>
</html>
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2023/3/13Time: 20:09To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>b.jsp页面
</body>
</html>
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2023/3/13Time: 20:45To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8"isErrorPage="true"language="java" %>
<html>
<head><title>Title</title>
</head>
<body>抱歉,您访问的页面服务器出现错误,正在努力为您抢修!!
</body>
</html>
JSP常用脚本
声明脚本
声明脚本的格式是:<%! 声明java代码 %>
作用:可以给jsp翻译出来的java类定义属性和方法甚至是静态代码块以及内部类等。
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--Created by IntelliJ IDEA.User: AdministratorDate: 2023/3/13Time: 21:01To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<%--1.声明类属性--%>
<%!private Integer id;private String name;private static Map<String, Object> map;
%>
<%--2.声明static静态代码块--%>
<%!static {map = new HashMap<String, Object>();map.put("key0", "value0");map.put("key1", "value1");map.put("key2", "value2");map.put("key3", "value3");map.put("key4", "value4");}
%>
<%--3.声明类方法--%>
<%!public int TestInt() {return 1;}
%>
<%--4.声明内部类--%>
<%!public static class Test{private Integer id = 1;private String name = "pero";}
%>
</body>
</html>
/** Generated by the Jasper component of Apache Tomcat* Version: Apache Tomcat/10.0.27* Generated at: 2023-03-13 13:16:03 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 jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.jsp.*;
import java.util.Map;
import java.util.HashMap;public final class test_jsp extends org.apache.jasper.runtime.HttpJspBaseimplements org.apache.jasper.runtime.JspSourceDependent,org.apache.jasper.runtime.JspSourceImports {private Integer id;private String name;private static Map<String, Object> map;static {map = new HashMap<String, Object>();map.put("key0", "value0");map.put("key1", "value1");map.put("key2", "value2");map.put("key3", "value3");map.put("key4", "value4");}public int TestInt() {return 1;}public static class Test{private Integer id = 1;private String name = "pero";}.....
}
表达式脚本
表达式脚本的格式:<%= 表达式 %>
作用:在jsp页面上输出数据
表达式脚本特点:
1.所有的表达式脚本都会被翻译到_jspService()方法中
2.表达式脚本都会被翻译成为out.print()输出到页面上
3.由于表达式脚本翻译的内容都在_jspService()方法中,所以_jspService()方法中的对象都可以直接使用
4.表达式脚本中的表达式不能以分号结尾
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %><%--Created by IntelliJ IDEA.User: AdministratorDate: 2023/3/13Time: 21:20To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<%!private static Map<String, Object> map;
%>
<%!static {map = new HashMap<String, Object>();map.put("key", "value");}
%>
<%--1.输出整型--%>
<%=
12
%><br>
<%--2.输出浮点型--%>
<%=
12.12
%><br>
<%--3.输出字符串型--%>
<%=
"字符串"
%><br>
<%--4.输出对象--%>
<%=
map
%><br>
<%=
request.getParameter("username")
%>
</body>
</html>
代码脚本
代码脚本格式:<% java语句 %>
代码脚本的作用:可以在jsp页面中编写我们自己需要的功能(写的是java语句)。
代码脚本特点:
1.代码脚本翻译后都在_jspService()方法中
2.代码脚本由于翻译到_jspService()方法中,所以_jspService()方法中的现有对象都可以直接使用
3.可以有多个代码脚本块组合成一个完整的java语句
4.代码脚本还可以和表达式脚本一起组合使用,在jsp页面上输出数据
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2023/3/13Time: 21:42To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<%--1.代码脚本=》if语句--%>
<%int i = 12;if (i == 12) {
%>
<%="我喜欢java语言"%>
<%
} else {
%>
<%="我喜欢java语言too"%>
<%}
%><br>
<%--2.代码脚本=》for循环语句--%>
<%for (int j = 0; j < 10; j++) {
%>
<%System.out.println(j);}
%>
<%--3.翻译后java文件中_jspService()方法内的代码都可以写--%>
<%String username = request.getParameter("username");System.out.println("用户名请求参数:" + username);
%>
<%String name = "pero";
%>
<%=name%><br><table><%for (int j = 0; j < 10; j++) {%><tr><td>第<%= j + 1%>行</td></tr><%}%>
</table>
</body>
</html>
JSP中的三种注释
1.html注释:html注释会被翻译到java源代码中,在_jspService方法中以out.write输出到客户端
test4.jsp页面<!-- html注释 -->
out.write(" test4.jsp页面\r\n");
out.write(" <!-- html注释 -->\r\n");
2.java注释:java注释会被翻译到java源代码中。
<%//单行注释/* 多行java注释 */
%>
//单行注释/* 多行java注释 */
3.jsp注释:jsp注释可以注释掉jsp页面中所有代码
<%-- jsp注释 --%>
jsp中的九大内置对象
jsp中的内置对象是指Tomcat在翻译jsp页面成为Servlet源代码后,内部提供的九大对象。
public void _jspService(final jakarta.servlet.http.HttpServletRequest request, final jakarta.servlet.http.HttpServletResponse response)throws java.io.IOException, jakarta.servlet.ServletException {final jakarta.servlet.jsp.PageContext pageContext;jakarta.servlet.http.HttpSession session = null;java.lang.Throwable exception = org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable(request);if (exception != null) {response.setStatus(jakarta.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR);}final jakarta.servlet.ServletContext application;final jakarta.servlet.ServletConfig config;jakarta.servlet.jsp.JspWriter out = null;final java.lang.Object page = this;jakarta.servlet.jsp.JspWriter _jspx_out = null;jakarta.servlet.jsp.PageContext _jspx_page_context = null;
①request:请求对象
②response:响应对象
③pageContext:jsp的上下文对象
④session:会话对象
⑤application:ServletContext对象
⑥config:ServletConfig对象
⑦out:jsp输出流对象
⑧page:指向当前jsp的对象
⑨exception:异常对象
jsp域对象
①pageContext(PageContextimpl类)当前jsp页面范围内有效
②request(HttpServletRequest类)一次请求内有效
③session(HttpSession类)一次会话范围内有效(打开浏览器访问服务器,直到关闭浏览器)
④application(ServletContext类)整个Web工程范围内都有效(只要web公里内工程不停止数据都在)
域对象是可以像Map一样存取数据的对象,四个域对象功能一样,不同的是它们对数据的存取范围。
四个域对象在使用的时候的优先顺序分别是(从小到大的范围):
pageContext -> request -> session -> application
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2023/3/15Time: 8:45To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h1>scope.jsp页面</h1>
<%//往四个域中保存数据pageContext.setAttribute("key", "pageContext");request.setAttribute("key", "request");session.setAttribute("key", "session");application.setAttribute("key", "application");
%>
pageContext域值为:<%=pageContext.getAttribute("key")%><br>
request域值为:<%=request.getAttribute("key")%><br>
session域值为:<%=session.getAttribute("key")%><br>
application域值为:<%=application.getAttribute("key")%><br>
<%request.getRequestDispatcher("/scope2.jsp").forward(request,response);
%>
</body>
</html>
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2023/3/15Time: 8:45To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><h1>scope.jsp页面</h1>pageContext域值为:<%=pageContext.getAttribute("key")%><br>request域值为:<%=request.getAttribute("key")%><br>session域值为:<%=session.getAttribute("key")%><br>application域值为:<%=application.getAttribute("key")%><br>
</body>
</html>
JSP-out和response输出的区别
response中表示响应,常用于设置返回给客户端的内容(输出);out也是给用户做输出使用。
out.write()代码的数据保存在out缓冲区,response.getWriter().write()保存在response缓冲区。
<%out.write("out输出1 <br/>");out.write("out输出2 <br/>");response.getWriter().write("response输出① <br/>");response.getWriter().write("response输出② <br/>");%>
out.write("out输出1 <br/>");out.write("out输出2 <br/>");response.getWriter().write("response输出① <br/>");response.getWriter().write("response输出② <br/>");
展示效果:
response输出①
response输出②
out输出1
out输出2
<%out.write("out输出1 <br/>");out.flush();out.write("out输出2 <br/>");response.getWriter().write("response输出① <br/>");response.getWriter().write("response输出② <br/>");%>
out.write("out输出1 <br/>");out.flush();out.write("out输出2 <br/>");response.getWriter().write("response输出① <br/>");response.getWriter().write("response输出② <br/>");
展示效果:
out输出1
response输出①
response输出②
out输出2
当jsp页面中所有代码块执行完成后会做以下两个操作:
1.执行out.flush()操作,会把out缓冲区中的数据追加写入到response缓冲区末尾
2.会执行response的刷新操作,把全部数据写给客户端
由于jsp翻译之后底层源码都是使用out来进行输出,所以一般情况下我们在jsp页面中统一使用out进行输出,避免打乱输出内容的顺序。
out.write()输出字符串没有问题;
out.print()输出任意数据没有问题(都转换成字符串后调用write输出);
在jsp页面中可以统一使用out.print()进行输出。
常用标签
静态包含
一个网页中包括:
1.网站导航条、菜单、轮播广告、用户登录信息、天气信息等等
2.页面主题内容
3.加入我们、友情链接、联系我们、版权归属等等
为了更好维护,希望一个单独的jsp页面只维护一份,修改一处其他都统一被修改,采用包含引入的方法。
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2023/3/15Time: 21:01To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
头部信息<br>
主题内容<br>
<%-- <%@include file=""%>为静态包含file属性指定所包含的jsp页面的路径地址中第一个/表示http://ip:port/工程路径/ 映射到代码的web目录静态包含的特点:①静态包含不会翻译被包含的jsp页面②静态包含其实是把被包含的jsp页面的代码拷贝到包含的位置执行输出--%>
<%@include file="/footer.jsp"%>
</body>
</html>
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2023/3/15Time: 21:02To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
页脚信息<br>
</body>
</html>
out.write("\r\n");out.write("\r\n");out.write("<html>\r\n");out.write("<head>\r\n");out.write(" <title>Title</title>\r\n");out.write("</head>\r\n");out.write("<body>\r\n");out.write("头部信息<br>\r\n");out.write("主题内容<br>\r\n");out.write('\r');out.write('\n');out.write("\r\n");out.write("\r\n");out.write("<html>\r\n");out.write("<head>\r\n");out.write(" <title>Title</title>\r\n");out.write("</head>\r\n");out.write("<body>\r\n");out.write("页脚信息<br>\r\n");out.write("</body>\r\n");out.write("</html>\r\n");out.write("\r\n");out.write("</body>\r\n");out.write("</html>\r\n");
动态包含
底层原理:
在main2.jsp页面中存在九大内置对象,在footer.jsp页面中存在九大内置对象
main2.jsp通过JspRuntimeLibrary.include(request, response, "/footer.jsp" + "?" + org.apache.jasper.runtime.JspRuntimeLibrary.URLEncode("username", request.getCharacterEncoding())+ "=" + org.apache.jasper.runtime.JspRuntimeLibrary.URLEncode("root1", request.getCharacterEncoding()) + "&" + org.apache.jasper.runtime.JspRuntimeLibrary.URLEncode("password", request.getCharacterEncoding())+ "=" + org.apache.jasper.runtime.JspRuntimeLibrary.URLEncode("root2", request.getCharacterEncoding()), out, false);
把自己的request、response、out等对象传递给了footer.jsp使用,在out缓冲区中,out.write()输出的信息经过统一拼接后,刷新并传递给response缓冲区
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2023/3/15Time: 21:01To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
头部信息<br>
主题内容<br>
<%-- <jsp:include page=""></jsp:include>为动态包含page属性是指定包含的jsp页面的路径动态包含也可以向静态包含一样,把被包含的内容执行输出到包含位置动态包含的特点:1.动态包含会把包含的jsp页面也都翻译成java代码2.动态包含底层代码使用如下代码去调用被包含的jsp页面执行输出:org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "/footer.jsp", out, false);3.动态包含还可以传递参数--%>
<jsp:include page="/footer.jsp"><jsp:param name="username" value="root1"/><jsp:param name="password" value="root2"/>
</jsp:include>
</body>
</html>
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2023/3/15Time: 21:02To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
页脚信息<br>
<%=request.getParameter("password")%>
</body>
</html>
out.write("\r\n");out.write("\r\n");out.write("<html>\r\n");out.write("<head>\r\n");out.write(" <title>Title</title>\r\n");out.write("</head>\r\n");out.write("<body>\r\n");out.write("头部信息<br>\r\n");out.write("主题内容<br>\r\n");out.write('\r');out.write('\n');org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "/footer.jsp", out, false);out.write("\r\n");out.write("</body>\r\n");out.write("</html>\r\n");
请求转发
<%-- <jsp:forward page=""></jsp:forward>为请求转发page属性设置请求转发的路径--%>
<jsp:forward page="/scope2.jsp"></jsp:forward>
请求转发应用说明
客户端:关键字搜索 -> SearchStudentServlet程序(1.获取请求参数;2.发送sql语句到数据库去查询信息;3.保存查询信息到学生信息,保存到request域中) -> showStudent.jsp(专门用来显示学生数据,遍历查询到的结果输出给客户端显示) -> 客户端显示查询信息
★必须由客户端发请求给servlet,不可以直接访问jsp文件
Listener监听器
Listener监听器是JavaWeb的三大组件之一,三大组件分别为Servlet程序、Filter过滤器、Listener监听器;Listener监听器是JavaEE的规范,也就是接口;监听器的作用是监听某些事物的变化,然后通过回调函数,反馈给客户(程序)去做相应的处理
ServletContextListener监听器
ServletContextListener可以监听ServletContext对象的创建和销毁。
ServletContext对象在web工程启动的时候创建,在web工程停止的时候销毁。
监听到创建和销毁之后都会分别调用SevletContextListener监听器的方法反馈。
两个方法分别为:
public interface ServletContextListener extends EventListener {//在ServletContext对象创建之后马上调用做初始化default void contextInitialized(ServletContextEvent sce) {}//在ServletContext对象销毁之后调用default void contextDestroyed(ServletContextEvent sce) {}
}
package com.pero.jsp.listener;import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;public class MyServletContextListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("ServletContext被创建");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {System.out.println("ServletContext被销毁");}
}
<!-- 配置监听器 --><listener><listener-class>com.pero.jsp.listener.MyServletContextListener</listener-class></listener>