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); } }

 

转载于:https://www.cnblogs.com/guanghe/p/6028192.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值