博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Servlet中Session和Context监听器
阅读量:3960 次
发布时间:2019-05-24

本文共 4902 字,大约阅读时间需要 16 分钟。

Session监听器

该接口继承于javax.servlet.http

HttpSessionListener 该接口是监听session对象的创建以及销毁

HttpSessionAttributeListener 则是对session对象属性改变的监听。
在这里插入图片描述
案例代码如下
1.0 继承接口类(实现其方法)

package 监听器;import javax.servlet.annotation.WebListener;import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;import javax.websocket.Session;public class HttpSession implements HttpSessionListener,		HttpSessionAttributeListener {
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("创建session"); } public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("销毁session"); } public void attributeAdded(HttpSessionBindingEvent arg0) {
System.out.println("给session添加属性:"); System.out.println("session对象名"+arg0.getName());//输出session对象的属性 System.out.println("session对象值"+arg0.getValue());//输出session对象的值 } public void attributeRemoved(HttpSessionBindingEvent arg0) {
System.out.println("删除值"); System.out.println("(删除后)对象名:"+arg0.getName()); System.out.println("session的值(删除后):"+arg0.getSession().getAttribute(arg0.getName())); } public void attributeReplaced(HttpSessionBindingEvent arg0) {
System.out.println("session对象名(修改后):"+arg0.getName()); System.out.println("session的值(修改后):"+arg0.getSession().getAttribute(arg0.getName())); }}

2.0 测试的servlet,对session对象的创建以及属性的修改

package 监听器;import java.io.IOException;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;@WebServlet("/testHttpSession")public class testHttpSession extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession(); //对session对象添加值,attributeAdded()方法 session.setAttribute("name", "ysdqh123"); //对session值进行修改,对应的是attributeReplaced()方法 session.setAttribute("name", "ysdqh456"); session.removeAttribute("name"); //删除值 } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response); }}

在web.xml配置文件当中添加此段代码,配置监听器代码

监听器2
监听器.HttpSession

Context监听器

ServletContextListener

在使用该接口的时候,需要实现其方法

函数名 描述
contextInitialized () 监听器的创建
contextDestroyed() 监听器的销毁

ServletContextAttributeListener

同理,在使用该接口的时候,需要实现其方法

函数名 函数
attributeAdded() 属性添加将调用方法
attributeRemoved() 属性删除将调用此方法
attributeReplaced() 属性更新将调用此方法

在此下代码中,俩个接口,以及其共5个方法,皆已实现,代码如下:

package 监听器;import javax.servlet.ServletContextAttributeEvent;import javax.servlet.ServletContextAttributeListener;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;public class ServletContext implements ServletContextAttributeListener, ServletContextListener {
public void attributeAdded(ServletContextAttributeEvent arg0) {
System.out.println("属性添加方法"); System.out.println("对象名、即属性名 :"+arg0.getName()); System.out.println("对象的值 :"+arg0.getServletContext().getAttribute(arg0.getName())); } public void attributeRemoved(ServletContextAttributeEvent arg0) {
System.out.println("属性删除方法"); System.out.println("(删除后)属性名:"+arg0.getName()); System.out.println("(删除后) 值:"+arg0.getServletContext().getAttribute(arg0.getName())); } public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("监听器销毁方法"); } public void attributeReplaced(ServletContextAttributeEvent arg0) {
System.out.println("属性更新方法"); System.out.println("(修改后)属性名 :"+arg0.getName()); System.out.println("(修改后)值:"+arg0.getServletContext().getAttribute(arg0.getName())); } public void contextInitialized(ServletContextEvent arg0) {
System.out.println("监听器创建方法"); } }

测试的servlet

package 监听器;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet("/testcontext")public class testcontext extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.getServletContext().setAttribute("name", "yueyueniao1"); this.getServletContext().setAttribute("name", "yueyueniao2"); this.getServletContext().removeAttribute("name"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response); }}

在web.xml配置文件当中添加此段代码

监听器1
监听器.ServletContext

转载地址:http://samzi.baihongyu.com/

你可能感兴趣的文章
The AnimationClip 'Walk' used by the Animation component 'Pig' must be marked as Legacy.
查看>>
《Linux内核设计与实现》- Linux的进程
查看>>
《Linux内核设计与实现》- 进程的调度
查看>>
inet_ntoa()
查看>>
POSIX消息队列mq_open问题
查看>>
两个数组a[N],b[N],其中A[N]的各个元素值已知,现给b[i]赋值,b[i] = a[0]*a[1]*a[2]…*a[N-1]/a[i];
查看>>
用户态切换到内核态的3种方式
查看>>
笔试常见的智力题(附答案)
查看>>
内核库函数
查看>>
Linux 系统内核空间与用户空间通信的实现与分析
查看>>
linux内核空间和用户空间的区别及交互
查看>>
如何写好应用型学术论文
查看>>
如何查看进程的各种限制
查看>>
64位int类型用printf输出问题
查看>>
网络后台开发面试题目
查看>>
Linux 共享内存限制的查看与设置
查看>>
进程的状态转换
查看>>
如何查看进程的信息(线程数)
查看>>
read的用法
查看>>
查看系统信息(cpu,内存,硬盘,网卡)
查看>>