过滤器和事件监听

过滤器实现步骤:

1.新建一般java类 继承接口Filter 并实现接口中所有的方法在方法doFilter()中 写入代码

2 在web.xml 中注册过滤器(将需要过滤的页面用一个文件夹装起来,配置时 过滤这个文件夹,从而避免过滤了不需要过滤的页面)

代码示例:

1.

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class FirstFilter implements Filter {

 public void destroy() {
   }

 public void doFilter(ServletRequest req, ServletResponse resp,
   FilterChain chain) throws IOException, ServletException {
  
  System.out.println("First 执行了过滤器!!!!");
  
  //请求继续执行 否则将过滤的页面都不能执行
ain.doFilter(req, resp);
  
 }

 public void init(FilterConfig arg0) throws ServletException {
  
   }

}

2.配置过滤器

<!--
  <filter>
   <filter-name>first_filter</filter-name>
   <filter-class>com.zuxia.filter.过滤器类名</filter-class>  
  </filter>
  
  <filter>
   <filter-name>second_filter</filter-name>
   <filter-class>com.zuxia.filter.SecondFilter</filter-class>  
  </filter>
  
  <filter-mapping>
   <filter-name>second_filter</filter-name>
   <url-pattern>/*</url-pattern>必须以“/”开头 (/表示项目的根目录(WebRoot)* 代表过滤该目录下的所有文件)
 </filter-mapping>
  
  
  <filter-mapping>
   <filter-name>first_filter</filter-name>
   <url-pattern>/*</url-pattern>必须以“/”开头 (/表示项目的根目录(WebRoot))
  </filter-mapping> 
 -->

事件监听的步骤:

1. 编写Java类,实现指定的监听接口,重写接口中的方法。

application (ServletContextListener , ServletContextAttributeListener )

session (HttpSessionListener ,HttpSessionAttributeListener )

 
 2. 在web.xml文件中配置监听器(注册监听器)

代码示例:

1. application

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
lic class TestApplicationListener implements ServletContextListener,ServletContextAttributeListener{

 public void contextDestroyed(ServletContextEvent e) {
  
//  //将访问的次数,写入到文件中
 }

 public void contextInitialized(ServletContextEvent e) {
  File f = new File(TestApplicationListener.class.getClassLoader().getResource("count.txt").getPath().substring(1));//得到根目录的路径的方法(web服务器中)
  
  try {
  
   FileReader fr = new FileReader(f);
   
   BufferedReader bf = new BufferedReader(fr);
   
   String count = bf.readLine();
   
   if(count!=null && !count.equals("")){
    e.getServletContext().setAttribute("sumcount", count);
   }
   
   bf.close();
   
   fr.close();
  } catch (Exception e2) {
   e2.printStackTrace();
  }
  
  
 }

 public void attributeAdded(ServletContextAttributeEvent e) {
  
  System.out.println("你添加了属性。。。。。。。");
  
  System.out.println(e.getName()+":"+e.getValue());
  
 }

 public void attributeRemoved(ServletContextAttributeEvent arg0) {
  // TODO Auto-generated method stub
  
 }

 public void attributeReplaced(ServletContextAttributeEvent e) {
  // TODO Auto-generated method stub
  
  File f = new File(TestApplicationListener.class.getClassLoader().getResource("count.txt").getPath().substring(1));
  
  try {
  
   FileWriter fw = new FileWriter(f);
   
   fw.write(e.getServletContext().getAttribute("sumcount")+"");
   
   fw.close();
   
  } catch (Exception e2) {
   e2.printStackTrace();
  }
 }
 
}

session   :


import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class TestSessionListener implements HttpSessionListener{

 public void sessionCreated(HttpSessionEvent e) {
  
  ServletContext application = e.getSession().getServletContext();
  
  Object obj = application.getAttribute("sumcount");
  
  int count = obj==null ? 1 : Integer.parseInt(obj+"")+1;
  
  application.setAttribute("sumcount",count);
  
 }

 public void sessionDestroyed(HttpSessionEvent arg0) {
  // TODO Auto-generated method stub
  
 }

}
 

<!-- 注册监听器 -->
 <listener>
  <listener-class>com.zuxia.listener.TestApplicationListener</listener-class>
 </listener>
 
 <listener>
  <listener-class>com.zuxia.listener.TestSessionListener</listener-class>
 </listener>

 

本文出自 “涓涓细流” 博客,谢绝转载!

你可能感兴趣的:(职场,事件监听,过滤器,休闲)