文章目录
- 1.基本介绍
- 2.ServletContextListener
- 1.基本介绍
- 2.创建maven项目,导入依赖
- 3.代码演示
- 1.实现ServletContextListener接口
- 2.配置web.xml
- 3.结果
- 3.ServletContextAttributeListener监听器
- 1.基本介绍
- 2.代码实例
- 1.ServletContextAttributeListener.java
- 2.配置web.xml
- 3.创建servlet演示
- 4.结果
- 4.HttpSessionListener
- 1.基本介绍
- 2.代码实例
- 1.HttpSessionListener.java
- 2.配置web.xml
- 3.创建servlet演示
- 4.结果
- 5.HttpSessionAttributeListener
- 1.基本介绍
- 2.代码实例
- 1.HttpSessionAttributeListener.java
- 2.配置web.xml
- 3.创建servlet演示
- 4.结果
- 6.ServletRequestListener
- 1.基本介绍
- 2.代码实例
- 1.ServletRequestListener.java
- 2.配置web.xml
- 3.结果
- 7.其他监听器
- 8.监听器小结
- 1.创建监听器
-
- 2.ServletContextListener
- 3.ServletContextAttributeListener
- 4.HttpSessionListener
- 5.HttpSessionAttributeListener
- 6.ServletRequestListener
1.基本介绍
2.ServletContextListener
1.基本介绍
2.创建maven项目,导入依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
3.代码演示
1.实现ServletContextListener接口
package listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
public class ServletContextListener implements javax.servlet.ServletContextListener {
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
System.out.println("监听到servletContext创建");
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
System.out.println("监听到servletContext销毁 ");
}
}
2.配置web.xml
<listener>
<listener-class>listener.ServletContextListener</listener-class>
</listener>
3.结果
3.ServletContextAttributeListener监听器
1.基本介绍
2.代码实例
1.ServletContextAttributeListener.java
package listener;
import javax.servlet.ServletContextAttributeEvent;
public class ServletContextAttributeListener implements javax.servlet.ServletContextAttributeListener {
public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("ServletContextAttributeListener监听到添加属性" + servletContextAttributeEvent.getName()
+ servletContextAttributeEvent.getValue());
}
public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("ServletContextAttributeListener监听到删除属性" + servletContextAttributeEvent.getName()
+ servletContextAttributeEvent.getValue());
}
public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("ServletContextAttributeListener监听到修改属性" + servletContextAttributeEvent.getName()
+ servletContextAttributeEvent.getValue());
}
}
2.配置web.xml
<listener>
<listener-class>listener.ServletContextAttributeListener</listener-class>
</listener>
3.创建servlet演示
package servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/modify")
public class modifyServletContextAttribute extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = super.getServletContext();
servletContext.setAttribute("name","孙显圣");
servletContext.setAttribute("name","李白");
servletContext.removeAttribute("name");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
4.结果
4.HttpSessionListener
1.基本介绍
2.代码实例
1.HttpSessionListener.java
package listener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
public class HttpSessionListener implements javax.servlet.http.HttpSessionListener {
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
HttpSession session = httpSessionEvent.getSession();
System.out.println("HttpSessionListener监听到session创建id为" + session.getId());
}
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
HttpSession session = httpSessionEvent.getSession();
System.out.println("HttpSessionListener监听到session销毁id为" + session.getId());
}
}
2.配置web.xml
<listener>
<listener-class>listener.HttpSessionListener</listener-class>
</listener>
3.创建servlet演示
package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(urlPatterns = "/modifySession")
public class modifySession extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.invalidate();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
4.结果
5.HttpSessionAttributeListener
1.基本介绍
2.代码实例
1.HttpSessionAttributeListener.java
package listener;
import javax.servlet.http.HttpSessionBindingEvent;
public class HttpSessionAttributeListener implements javax.servlet.http.HttpSessionAttributeListener {
public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
String name = httpSessionBindingEvent.getName();
System.out.println("监听到session创建属性:" + name);
}
public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
String name = httpSessionBindingEvent.getName();
System.out.println("监听到session删除属性:" + name);
}
public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
String name = httpSessionBindingEvent.getName();
System.out.println("监听到session替换属性:" + name);
}
}
2.配置web.xml
<listener>
<listener-class>listener.HttpSessionAttributeListener</listener-class>
</listener>
3.创建servlet演示
package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(urlPatterns = "/modifySessionAttribute")
public class modifySessionAttribute extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.setAttribute("name","孙显圣");
session.setAttribute("name","李白");
session.removeAttribute("name");
}
}
4.结果
6.ServletRequestListener
1.基本介绍
2.代码实例
1.ServletRequestListener.java
package listener;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
public class ServletRequestListener implements javax.servlet.ServletRequestListener {
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
System.out.println("ServletRequestListener 监听到 request对象销毁");
}
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
System.out.println("ServletRequestListener 监听到 request对象创建");
ServletRequest servletRequest = servletRequestEvent.getServletRequest();
System.out.println("访问ip:" + servletRequest.getRemoteAddr());
System.out.println("访问资源:" + ((HttpServletRequest)servletRequest).getRequestURL());
}
}
2.配置web.xml
<listener>
<listener-class>listener.ServletRequestListener</listener-class>
</listener>
3.结果
7.其他监听器
8.监听器小结
1.创建监听器
1.基本概念
2.创建方法
- 实现接口,进行相应操作
- 配置web.xml(告诉tomcat)
2.ServletContextListener
3.ServletContextAttributeListener
4.HttpSessionListener
5.HttpSessionAttributeListener
6.ServletRequestListener