web监听器就好比观察者模式里面的观察者,当被观察者发生变化时候,观察者做出一些反应
我们在使用web监听器的时候,当application应用启动销毁、request创建销毁,session创建销毁,都能做到监听
使用监听器,需要在web.xml中进行配置,我们就先来看个最简单的监听器吧
监听域对象的创建和销毁
web.xml
<listener>
<listener-class>com.safly.HelloServletContextListner</listener-class>
</listener>
HelloServletContextListner
package com.safly;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class HelloServletContextListner implements ServletContextListener,
ServletRequestListener, HttpSessionListener {
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("context 对象被销毁");
}
public void contextInitialized(ServletContextEvent sce) {
System.out.println("context对象被创建");
}
public void requestDestroyed(ServletRequestEvent rre) {
System.out.println("request被销毁");
}
public void requestInitialized(ServletRequestEvent rre) {
System.out.println("request被创建");
}
public void sessionCreated(HttpSessionEvent se) {
System.out.println("session被创建");
}
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("session被销毁");
}
}
index.jsp页面很简单就一句话
<body>
<h1>Page</h1>
</body>
我们在server上运行index.jsp页面时候
浏览器输入 http://localhost:8080/day01/index.jsp
context对象被创建
request被创建
session被创建
request被销毁
当刷新此页面后
request被创建
request被销毁
关闭服务器后
context 对象被销毁
打开另外一个浏览器,开一个新的浏览器窗口 http://localhost:8090/javaweb/index.jsp
因为是新的浏览器窗口,会产生一个新的session
request被创建
session被创建
request被销毁
监听域对象的创建和销毁
request是一个请求,请求后,当一个相应返回时,即被销毁,当发送一个请求时,即被创建
延续上个案例–配置不变,HelloServletContextListner不变
来看下index.jsp
<body>
<h1>Page</h1>
<a href="test.jsp">To Test Page</a>
<%
request.setAttribute("requestKey","requestValue");
%>
</body>
跳转到的test.jsp页面
<body>
<h1>Test Page</h1>
<%=request.getAttribute("requestKey")%>
</body>
http://localhost:8080/day01/index.jsp
context对象被创建
request被创建
session被创建
request被销毁
点击链接跳转到test.jsp
提交页面后 获取不到那个requestKey–null,刷新页面后
request被创建
request被销毁
为什么获取不到requestKey呢?是因为通过index.jsp中,request已经返回响应,request已经销毁了,点击test.jsp超连接,是重新发送了一个请求,是一个新的request,所以获取不到index.jsp页面中的requestKey值
所以我们将index.jsp改成如下jsp:forward转发的方式:
<body>
<h1>Page</h1>
<%
request.setAttribute("requestKey","requestValue");
%>
<jsp:forward page="test.jsp"></jsp:forward>
</body>
http://localhost:8090/javaweb/index.jsp
这样就可以获取requestKey值了(requestValue)
context对象被创建,
request被创建
session被创建
request被销毁
刷新后
request被创建
request被销毁
或者我们还可以通过servlet转发实现上楼相同的效果:
延续上个案例–配置不变,HelloServletContextListner不变
index.jsp这里面也是一个超链接,但是这个超链接跟如下的链接是不一样的,本例的请求只是经过一个servlet,是转发的方式,而如下的链接是重新的一个新的请求
<a href="test.jsp">To Test Page</a>
<body>
<h1>Page</h1>
<a href="TestServlet">TestServlet</a>
</body>
TestServlet
package com.safly;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("requestKey", "requestValue");
request.getRequestDispatcher("/test.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
test.jsp
<body>
<h1>Test Page</h1>
<%=request.getAttribute("requestKey")%>
</body>
http://localhost:8090/javaweb/index.jsp
启动服务器context对象被创建,
request被创建
session被创建
request被销毁
点击链接后 转发是一个链接 获取到requestValue
request被创建
request被销毁
那我们看看重定向的方式:
其他的jsp页面,配置,HelloServletContextListner不变
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("requestKey", "requestValue");
response.sendRedirect("test.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
http://localhost:8090/javaweb/index.jsp
启动服务器context对象被创建,
request被创建
session被创建
request被销毁
点击链接–获取不到那个值,因为重定向是重新发了一个新的请求
request被创建
request被销毁
request被创建
request被销毁
我们来了解下其他的监听器
attributeListner
web.xml
<listener>
<listener-class>com.safly.TestAttributeListner</listener-class>
</listener>
TestAttributeListner
package com.safly;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class TestAttributeListner implements ServletContextAttributeListener,
ServletRequestAttributeListener,HttpSessionAttributeListener{
public void attributeAdded(ServletContextAttributeEvent scab) {
System.out.println("向context添加");
}
public void attributeRemoved(ServletContextAttributeEvent scab) {
System.out.println("向context删除");
}
public void attributeReplaced(ServletContextAttributeEvent scab) {
System.out.println("向context替换");
}
public void attributeAdded(ServletRequestAttributeEvent srae) {
System.out.println("向request添加");
}
public void attributeRemoved(ServletRequestAttributeEvent srae) {
System.out.println("向request删除");
}
public void attributeReplaced(ServletRequestAttributeEvent srae) {
System.out.println("向request替换");
}
public void attributeAdded(HttpSessionBindingEvent se) {
}
public void attributeRemoved(HttpSessionBindingEvent se) {
}
public void attributeReplaced(HttpSessionBindingEvent se) {
}
}
index.jsp
<body>
<h1>Test Page</h1>
<%
request.setAttribute("name","ABCD");
System.out.println("-----------");
request.setAttribute("name","DEFGs");
System.out.println("-----------");
request.removeAttribute("name");
System.out.println("-----------");
%>
</body>
http://localhost:8080/day01/index.jsp
向context添加
向request添加
向request替换
向request删除
HttpSessionBindingListener
HttpSessionBindingListener不需要在web.xml中进行配置
Customer
package com.safly;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class Customer implements HttpSessionBindingListener{
public void valueBound(HttpSessionBindingEvent event) {
System.out.println("绑定到session");
Object value = event.getValue();
System.out.println(value == this);
System.out.println(event.getName());
System.out.println(event.getSession());
}
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.println("从session中解除");
}
}
index.jsp
<body>
<%
Customer customer = new Customer();
session.setAttribute("customer",customer);
System.out.println("-----------");
session.removeAttribute("customer");
%>
</body>
http://localhost:8080/day01/index.jsp
绑定到session
true
customer
org.apache.catalina.session.StandardSessionFacade@16be535
-----------
从session中解除