1 2 3 27 28 29 package filters; 30 31 32 import java.io.IOException ; 33 import javax.servlet.Filter ; 34 import javax.servlet.FilterChain ; 35 import javax.servlet.FilterConfig ; 36 import javax.servlet.ServletContext ; 37 import javax.servlet.ServletException ; 38 import javax.servlet.ServletRequest ; 39 import javax.servlet.ServletResponse ; 40 41 42 58 59 public final class ExampleFilter implements Filter { 60 61 62 64 65 68 private String attribute = null; 69 70 71 75 private FilterConfig filterConfig = null; 76 77 78 80 81 84 public void destroy() { 85 86 this.attribute = null; 87 this.filterConfig = null; 88 89 } 90 91 92 103 public void doFilter(ServletRequest request, ServletResponse response, 104 FilterChain chain) 105 throws IOException , ServletException { 106 107 if (attribute != null) 109 request.setAttribute(attribute, this); 110 111 long startTime = System.currentTimeMillis(); 113 chain.doFilter(request, response); 114 long stopTime = System.currentTimeMillis(); 115 filterConfig.getServletContext().log 116 (this.toString() + ": " + (stopTime - startTime) + 117 " milliseconds"); 118 119 } 120 121 122 127 public void init(FilterConfig filterConfig) throws ServletException { 128 129 this.filterConfig = filterConfig; 130 this.attribute = filterConfig.getInitParameter("attribute"); 131 132 } 133 134 135 138 public String toString() { 139 140 if (filterConfig == null) 141 return ("InvokerFilter()"); 142 StringBuffer sb = new StringBuffer ("InvokerFilter("); 143 sb.append(filterConfig); 144 sb.append(")"); 145 return (sb.toString()); 146 147 } 148 149 150 } 151 152 | Popular Tags |