zoukankan      html  css  js  c++  java
  • Servlet过滤器和监听器配置范例

    1,Servlet过滤器

     1 <filter>
     2     <filter-name>charset</filter-name>
     3     <filter-class>org.guangsoft.filter.CharsetFilter</filter-class>
     4     <init-param>
     5         <param-name>encoding</param-name>
     6         <param-value>utf-8</param-value>
     7     </init-param>
     8 </filter>
     9 
    10 <filter-mapping>
    11         <filter-name>encoding</filter-name>
    12     <url-pattern>/*</url-pattern>
    13 </filter-mapping>
    14 
    15 Filter:
    16     package org.guangsoft.filter;
    17 
    18 public class CharsetFilter implements Filter 
    19 {
    20     private String encoding;
    21     public void init(FilterConfig filterconfig) throws ServletException 
    22     {
    23         this.encoding = filterconfig.getInitParameter("endcoding");
    24     }
    25     public void doFilter(ServletRequest req, ServletResponse resp,
    26             FilterChain chain) throws IOException, ServletException 
    27     {
    28         req.setCharacterEncoding(encoding);
    29         chain.doFilter(req, resp);
    30     }
    31     public void destroy() 
    32     {
    33     }
    34 }
    35         

    2,Servlet监听器

    Web.xml:
        <listener>
            <listener-class>org.guangsoft.listener.CountListener</listener-class>
    </listener>
    Listener:
    public class CountListener implements HttpSessionListener 
    {
    private ServletContext sc = null; public void sessionCreated(HttpSessionEvent se)
      {
    this.sc = se.getSession().getServletContext(); Integer i = (Integer) this.sc.getAttribute("online"); if (i == null) i = new Integer(0); i = new Integer((i.intValue()) + 1); this.sc.setAttribute("online", i); } public void sessionDestroyed(HttpSessionEvent httpsessionevent)
      { Integer i
    = (Integer) this.sc.getAttribute("online"); if (i == null) i = new Integer(1); i = new Integer((i.intValue()) - 1); this.sc.setAttribute("online", i); } }
  • 相关阅读:
    C#读取系统信息
    C# 读取驱动器盘符及信息
    for循环里的break,continue和return有什么差别
    monkey
    ZXing
    python中os模块的常用接口和异常中Exception的运用
    python中的字典应用实例
    python中的列表和字典
    python中如何单独测试一个函数的作用
    数据挖掘概念与技术PDF
  • 原文地址:https://www.cnblogs.com/guanghe/p/6028192.html
Copyright © 2011-2022 走看看