JavaWeb Listener 监听器(第十一次作业)

1.创建监听器:

在chepter11创建一个chapter11.listener包,编写一个mylistener类,用来实现若干个监听器接口

mylistener.java如下:

package chapter11.listener;
 
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
 
/**
 * Application Lifecycle Listener implementation class mylistener
 *
 */
@WebListener
public class mylistener implements ServletContextListener, HttpSessionListener, ServletRequestListener {
 
    /**
     * Default constructor. 
     */
 
    /**
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent arg0)  { 
         // TODO Auto-generated method stub
        System.out.println("servletsession被创建了");
    }
 
    /**
     * @see ServletRequestListener#requestDestroyed(ServletRequestEvent)
     */
    public void requestDestroyed(ServletRequestEvent arg0)  { 
         // TODO Auto-generated method stub
        System.out.println("servletrequest被销毁了");
    }
 
    /**
     * @see ServletRequestListener#requestInitialized(ServletRequestEvent)
     */
    public void requestInitialized(ServletRequestEvent arg0)  { 
         // TODO Auto-generated method stub
        System.out.println("servletrequest被创建了");
    }
 
    /**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent arg0)  { 
         // TODO Auto-generated method stub
        System.out.println("servletsession被销毁了");
    }
 
    /**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent arg0)  { 
         // TODO Auto-generated method stub
        System.out.println("servletcontext被销毁了");
    }
 
    /**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent arg0)  { 
         // TODO Auto-generated method stub
        System.out.println("servletcontext被创建了");
    }
    
}

 2.添加监听信息类,在web.xml文件中添加 mylistener事件监听器信息;

<listener>
      <listener-class>
          chapter11.listener.mylistener
      </listener-class>
  </listener> 

运行mylistener.java结果:

JavaWeb Listener 监听器(第十一次作业) 

 3.设置监听超时信息;

在web.xml文件中设置session的超时时间为2min

<session-config>
      <session-timeout>2</session-timeout>
  </session-config>

运行结果:

JavaWeb Listener 监听器(第十一次作业) 

 2.创建测试页面:

在chepter11中新建一个testattribute.jsp用来观察属性事件监听器的作用

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
    <h3>这是个测试对象属性信息监听器的页面</h3>
    <%
        getServletContext().setAttribute("username", "itcast");
        getServletContext().setAttribute("username", "itheima");
        getServletContext().removeAttribute("username");
        session.setAttribute("username", "itcast");
        session.setAttribute("username", "itheima");
        session.removeAttribute("username");
        request.setAttribute("username", "itcast");
        request.setAttribute("username", "itheima");
        request.removeAttribute("username");
    %>
</body>
</html>

 创建监听器,在chapter11.listener里创建myattributelistener.java的监听类;

package chapter11.listener;
 
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
 
/**
 * Application Lifecycle Listener implementation class myattributelistener
 *
 */
@WebListener
public class myattributelistener implements ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener {
 
    /**
     * Default constructor. 
     */
    public myattributelistener() {
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see ServletContextAttributeListener#attributeAdded(ServletContextAttributeEvent)
     */
    public void attributeAdded(ServletContextAttributeEvent sae)  { 
         // TODO Auto-generated method stub
        String name = sae.getName();
        System.out.println("servletcontext添加属性:"+ name + "="+sae.getServletContext().getAttribute(name));
    }
 
    /**
     * @see ServletContextAttributeListener#attributeRemoved(ServletContextAttributeEvent)
     */
    public void attributeRemoved(ServletContextAttributeEvent sae)  { 
         // TODO Auto-generated method stub
        String name = sae.getName();
        System.out.println("servletcontext移除属性:"+name);
    }
 
    /**
     * @see ServletRequestAttributeListener#attributeRemoved(ServletRequestAttributeEvent)
     */
    public void attributeRemoved(ServletRequestAttributeEvent sra)  { 
         // TODO Auto-generated method stub
        String name = sra.getName();
        System.out.println("servletrequest移除属性"+name);
    }
 
    /**
     * @see ServletRequestAttributeListener#attributeAdded(ServletRequestAttributeEvent)
     */
    public void attributeAdded(ServletRequestAttributeEvent sra)  { 
         // TODO Auto-generated method stub
        String name = sra.getName();
        System.out.println("servletrequest添加属性:"+ name + "="+sra.getServletRequest().getAttribute(name));
    }
 
    /**
     * @see ServletRequestAttributeListener#attributeReplaced(ServletRequestAttributeEvent)
     */
    public void attributeReplaced(ServletRequestAttributeEvent sra)  { 
         // TODO Auto-generated method stub
        String name = sra.getName();
        System.out.println("servletrequest替换属性:"+ name + "="+sra.getServletRequest().getAttribute(name));
        }
 
    /**
     * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
     */
    public void attributeAdded(HttpSessionBindingEvent hbe)  { 
         // TODO Auto-generated method stub
        String name = hbe.getName();
        System.out.println("httpsession添加属性:"+ name + "="+hbe.getSession().getAttribute(name));
        
    }
 
    /**
     * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
     */
    public void attributeRemoved(HttpSessionBindingEvent hbe)  { 
         // TODO Auto-generated method stub
        String name = hbe.getName();
        System.out.println("httpsession移除属性:"+name);
    }
 
    /**
     * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
     */
    public void attributeReplaced(HttpSessionBindingEvent hbe)  { 
         // TODO Auto-generated method stub
        String name = hbe.getName();
        System.out.println("httpsession替换属性:"+ name + "="+hbe.getSession().getAttribute(name));
    }
 
    /**
     * @see ServletContextAttributeListener#attributeReplaced(ServletContextAttributeEvent)
     */
    public void attributeReplaced(ServletContextAttributeEvent sea)  { 
         // TODO Auto-generated method stub
        String name = sea.getName();
        System.out.println("servletcontext替换属性:"+ name + "="+sea.getServletContext().getAttribute(name));
    }
    
}

添加监听信息,在web.xml文件中输入一下内容:

 <listener>
      <listener-class>
          chapter11.listener.myattributelistener
      </listener-class>
  </listener>

 

JavaWeb Listener 监听器(第十一次作业) 

 

 

 

上一篇:减少该死的 if else 嵌套


下一篇:数据库卡死 listener.log文件太大问题,停止tnslistener服务可以直接删除