日期:2014-05-17  浏览次数:20781 次

刚刚接触Filter求注释能加深理解
?public class CheckLoginFilter  
? implements Filter  
?{  
? protected FilterConfig filterConfig = null;  
? private String redirectURL = null;  
? private List notCheckURLList = new ArrayList();  
? private String sessionKey = null;  
?  
? public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException  
? {  
? HttpServletRequest request = (HttpServletRequest) servletRequest;  
? HttpServletResponse response = (HttpServletResponse) servletResponse;  
?  
? HttpSession session = request.getSession();  
? if(sessionKey == null)  
? {  
? filterChain.doFilter(request, response);  
? return;  
? }  
? if((!checkRequestURIIntNotFilterList(request)) && session.getAttribute(sessionKey) == null)  
? {  
? response.sendRedirect(request.getContextPath() + redirectURL);  
? return;  
? }  
? filterChain.doFilter(servletRequest, servletResponse);  
? }  
?  
? public void destroy()  
? {  
? notCheckURLList.clear();  
? }  
?  
? private boolean checkRequestURIIntNotFilterList(HttpServletRequest request)  
? {  
? String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());  
? return notCheckURLList.contains(uri);  
? }  
?  
? public void init(FilterConfig filterConfig) throws ServletException  
? {  
? this.filterConfig = filterConfig;  
? redirectURL = filterConfig.getInitParameter("redirectURL");  
? sessionKey = filterConfig.getInitParameter("checkSessionKey");  
?  
? String notCheckURLListStr = filterConfig.getInitParameter("notCheckURLList");  
?  
? if(notCheckURLListStr != null)  
? {  
? StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");  
? notCheckURLList.clear();  
? while(st.hasMoreTokens())  
? {  
? notCheckURLList.add(st.nextToken());  
? }  
? }  
? }  
?} 
求专家帮我把这个过滤器方法中一些主要代码加下注释,IF判断条件也帮忙注释一下,在下不胜感激!

------解决方案--------------------
//这是一个检查请求安全权限的Filter,可以配置。起作用时会将不符合条件的请求重定向。
?public class CheckLoginFilter
? implements Filter //过滤器都要实现该接口
?{
? protected FilterConfig filterConfig = null; //用于保存配置信息
? private String redirectURL = null;
? private List notCheckURLList = new ArrayList(); //可以免于处理的URL清单
? private String sessionKey = null; //若null则过滤器不起作用,希望启动本过滤器的时候要设一个非null值
?
? public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
? {
? HttpServletRequest request = (HttpServletRequest) servletRequest; //类型转换,方便后面调用getSession等方法
? HttpServletResponse response = (HttpServletResponse) servletResponse; //类型转换
?
? HttpSession session = request.getSession(); //取当前请求对应的会话
? if(sessionKey == null)
? {
? filterChain.doFilter(request, response); //放行
? return;
? }
? if((!checkRequestURIIntNotFilterList(request)) && session.getAttribute(sessionKey) == null)
? { //如果本URL必