442 CHAPTER 11 ADVANCED FILTERING TECHNIQUES The (Web hosting unlimited bandwidth)
442 CHAPTER 11 ADVANCED FILTERING TECHNIQUES The constructor creates an instance of VisAuditOutStream and passes the IP address and host name of client to the VisAuditOutStream constructor. It also creates a PrintWriter object based on the stream. The VisAuditOutStream will be used by static pages and servlets; the PrintWriter is used by JSP pages. The two other methods that the class overrides are getOutputStream() and getWriter(). These methods hand out your customized stream instead of the response s actual stream. The Filter Logic Finally, we get to the actual filter class in Listing 11-6, VisAuditFilter. You ll recognize the general organization from the preceding chapter s examples. Focus your attention on the doFilter() method. Listing 11-6. VisAuditFilter.java package com.apress.projsp; import java.io.*; import javax.servlet.*; public final class VisAuditFilter implements Filter { private FilterConfig filterConfig = null; public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String clientAddr = request.getRemoteAddr(); String clientHost = request.getRemoteHost(); filterConfig.getServletContext().log(”in VisAuditFilter”); VisAuditResponseWrapper myWrappedResp = new VisAuditResponseWrapper(response, clientAddr, clientHost); chain.doFilter(request, myWrappedResp); PrintWriter writer = myWrappedResp.getWriter(); writer.close(); response.setContentType(myWrappedResp.getContentType()); ReplaceContentOutputStream rcos = (ReplaceContentOutputStream) myWrappedResp.getOutputStream(); byte[] result = rcos.getResult(); response.setContentLength(result.length); ServletOutputStream out = response.getOutputStream(); out.write(result);
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.