1 6 7 package abc; 8 9 import java.io.*; 10 import java.net.*; 11 import java.util.*; 12 import java.text.*; 13 import javax.servlet.*; 14 import javax.servlet.http.*; 15 16 import javax.servlet.Filter ; 17 import javax.servlet.FilterChain ; 18 import javax.servlet.FilterConfig ; 19 import javax.servlet.ServletContext ; 20 import javax.servlet.ServletException ; 21 import javax.servlet.ServletRequest ; 22 import javax.servlet.ServletResponse ; 23 24 29 30 public class NewSimpleFilter implements Filter { 31 32 private FilterConfig filterConfig = null; 36 37 public NewSimpleFilter() { 38 } 39 40 private void doBeforeProcessing(ServletRequest request, ServletResponse response) 41 throws IOException, ServletException { 42 if (debug) log("NewSimpleFilter:DoBeforeProcessing"); 43 48 68 69 } 70 71 private void doAfterProcessing(ServletRequest request, ServletResponse response) 72 throws IOException, ServletException { 73 if (debug) log("NewSimpleFilter:DoAfterProcessing"); 74 79 91 93 100 } 101 102 111 public void doFilter(ServletRequest request, ServletResponse response, 112 FilterChain chain) 113 throws IOException, ServletException { 114 115 if (debug) log("NewSimpleFilter:doFilter()"); 116 117 doBeforeProcessing(request, response); 118 119 Throwable problem = null; 120 121 try { 122 chain.doFilter(request, response); 123 } 124 catch(Throwable t) { 125 problem = t; 131 t.printStackTrace(); 132 } 133 134 doAfterProcessing(request, response); 135 136 if (problem != null) { 141 if (problem instanceof ServletException ) throw (ServletException )problem; 142 if (problem instanceof IOException) throw (IOException)problem; 143 sendProcessingError(problem, response); 144 } 145 } 146 147 148 151 public FilterConfig getFilterConfig() { 152 return (this.filterConfig); 153 } 154 155 156 161 public void setFilterConfig(FilterConfig filterConfig) { 162 163 this.filterConfig = filterConfig; 164 } 165 166 170 public void destroy() { 171 } 172 173 174 178 public void init(FilterConfig filterConfig) { 179 180 this.filterConfig = filterConfig; 181 if (filterConfig != null) { 182 if (debug) { 183 log("NewSimpleFilter:Initializing filter"); 184 } 185 } 186 } 187 188 191 public String toString() { 192 193 if (filterConfig == null) return ("NewSimpleFilter()"); 194 StringBuffer sb = new StringBuffer ("NewSimpleFilter("); 195 sb.append(filterConfig); 196 sb.append(")"); 197 return (sb.toString()); 198 199 } 200 201 202 203 private void sendProcessingError(Throwable t, ServletResponse response) { 204 205 String stackTrace = getStackTrace(t); 206 207 if(stackTrace != null && !stackTrace.equals("")) { 208 209 try { 210 211 response.setContentType("text/html"); 212 PrintStream ps = new PrintStream(response.getOutputStream()); 213 PrintWriter pw = new PrintWriter(ps); 214 pw.print("<html>\n<head>\n<title>Error</title>\n</head>\n<body>\n"); 216 pw.print("<h1>The resource did not process correctly</h1>\n<pre>\n"); 218 pw.print(stackTrace); 219 pw.print("</pre></body>\n</html>"); pw.close(); 221 ps.close(); 222 response.getOutputStream().close();; 223 } 224 225 catch(Exception ex){ } 226 } 227 else { 228 try { 229 PrintStream ps = new PrintStream(response.getOutputStream()); 230 t.printStackTrace(ps); 231 ps.close(); 232 response.getOutputStream().close();; 233 } 234 catch(Exception ex){ } 235 } 236 } 237 238 public static String getStackTrace(Throwable t) { 239 240 String stackTrace = null; 241 242 try { 243 StringWriter sw = new StringWriter(); 244 PrintWriter pw = new PrintWriter(sw); 245 t.printStackTrace(pw); 246 pw.close(); 247 sw.close(); 248 stackTrace = sw.getBuffer().toString(); 249 } 250 catch(Exception ex) {} 251 return stackTrace; 252 } 253 254 public void log(String msg) { 255 filterConfig.getServletContext().log(msg); 256 } 257 258 private static final boolean debug = true; 259 } 260 | Popular Tags |