1.Filter是过滤器,过滤一些不友好或是你不想要的东东,
2.Filter可以实现对请求的过滤和重定向等,也就是说可以操作request和response,session等对象
3.Filter 是struts的核心控制器,负责拦截所有用户请求。
4.Filter(过滤器):你可以写个类实现Filter接口,然后配置到web.xml中,那么Tomcat在接受到Http请求后首先会调用FilterChain中的第一个过滤器,为了能调用下个过滤器或真正的请求(servlet or jsp),所以你的实现类所实现的方法中必须要调用chain.doFilter(request,response),不然会得到空白页面!过滤器通常用做处理乱码,权限控制,也可以管理Hibernate中的session!
5.Filter和servlet基本类似  可以用做servlet来用(struts2.0就是这么干的) 有reqeust请求才能运行
1.Listener是Servlet的监听器,可以监听客户端的请求、服务端的操作等。
通过监听器,可以自动激发一些操作。
2.listner只能监听到以上对象的属性的修改。
3.listener是监听器,通常都是监听并加载一些插件用的,比如spring。log4j等
4.Listener(监听器):利用观察者模式实现,在Tomcat中,如果要对application,session,request,response等对象监听,要实现类似****Listener接口,再配置到web.xml中,那么Tomcat在启动的时候就会把你写的这些监听类实例化成对象加入到一个集合中,此后对你要监听的对象操作时,都将会调用listener中的方法,比如HttpSessionListener 可以用来监听当前在线的所有用户!
5.listener是监听器 是系统启动就运行 一般监听都是用来事先加载系统文件的
敬请补充or贴代码

解决方案 »

  1.   


    public class MusicStoreContextListener implements ServletContextListener
    {
        public void contextInitialized(ServletContextEvent event)
        {
            ServletContext sc = event.getServletContext();      
     
            // initialize the customer service email address that's used throughout the web site
            String custServEmail = sc.getInitParameter("custServEmail");
            sc.setAttribute("custServEmail", custServEmail);        // initialize the current year that's used in the copyright notice
            GregorianCalendar currentDate = new GregorianCalendar();
            int currentYear = currentDate.get(Calendar.YEAR);
            sc.setAttribute("currentYear", currentYear);
            
            // initialize the array of years that's used for the credit card year
            ArrayList<String> creditCardYears = new ArrayList<String>();
            for (int i = 0; i < 8; i++)
            {
                int year = currentYear + i; 
                String yearString = Integer.toString(year);
                creditCardYears.add(yearString);
            }
            sc.setAttribute("creditCardYears", creditCardYears);
        }
        
        public void contextDestroyed(ServletContextEvent event)
        {
            // no cleanup necessary
        }    
    }
      

  2.   


    <!-- Listener & Filter Test -->
    <!-- Listener需要实现javax.servlet.ServletContextListener接口 -->
    <listener>
    <listener-class>com.listenerdemo.MyListener1</listener-class>
    </listener>

    <!-- Filter需要实现javax.servlet.Filter接口 -->
    <filter>
    <filter-name>MyFilter1</filter-name>
    <filter-class>com.filterdemo.MyFilter1</filter-class>
    </filter>
      

  3.   

     怎么一个有ServletContext,另一个没有?