码迷,mamicode.com
首页 > 其他好文 > 详细

OSChina 的全局过滤器源码

时间:2014-12-17 18:32:28      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   ar   io   color   os   sp   for   

源码如下,源码中的 RequestContext 代码请看这里。 很多人很好奇,但其实真的没有任何特殊之处,呵呵
标签: OSCHINA filter

[1].[代码] java代码 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package my.mvc;
 
import java.io.IOException;
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import my.db.DBManager;
 
/**
 * 全局过滤器
 * @author Winter Lau
 * @date 2010-1-13 下午08:36:18
 */
public class ControllerFilter implements Filter {
 
    private ServletContext context;
     
    /**
     * 过滤器初始化
     */
    public void init(FilterConfig cfg) throws ServletException {
        this.context = cfg.getServletContext();
    }
 
    /**
     * 执行过滤操作
     */
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException
    {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)res;
        RequestContext rc = RequestContext.begin(this.context, request, response);
         
        if(rc.uri().indexOf("/.svn/")>=0){
            rc.forbidden();
            return ;
        }
 
        try{
            chain.doFilter(rc.request(), rc.response());
        }finally{
            if(rc!=null) rc.end();
            DBManager.closeConnection();
        }
    }
 
    /**
     * 过滤器释放资源
     */
    public void destroy() {
    }
 
}

OSChina 的全局过滤器源码

标签:des   style   http   ar   io   color   os   sp   for   

原文地址:http://blog.csdn.net/u014311051/article/details/41984527

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!