Package filters

Source Code of filters.HitCounterFilter

/*
*
* Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the proprietary information of Sun Microsystems, Inc. 
* Use is subject to license terms.
*
*/

package filters;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Timestamp;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import database.BookDetails;
import cart.*;
import java.util.*;
import util.Counter;

public final class HitCounterFilter implements Filter {
    private FilterConfig filterConfig = null;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }
   
    public void destroy() {
        this.filterConfig = null;
    }
    public void doFilter(ServletRequest request, ServletResponse response,
         FilterChain chain) throws IOException, ServletException {

         if (filterConfig == null)
            return;

         StringWriter sw = new StringWriter();
         PrintWriter writer = new PrintWriter(sw);
        
         Counter counter = (Counter)filterConfig.getServletContext().getAttribute("hitCounter");
         writer.println();
         writer.println("=======================================================");
         writer.println("The number of hits is: " + counter.incCounter());
         writer.println("=======================================================");

         // Log the resulting string
         writer.flush();
         filterConfig.getServletContext().log(sw.getBuffer().toString());
        
         PrintWriter out = response.getWriter();
         CharResponseWrapper wrapper = new CharResponseWrapper((HttpServletResponse)response);
         chain.doFilter(request, wrapper);
         int index = wrapper.getData().indexOf("</body></html>");
         out.write(wrapper.getData().substring(0, index-1));
         out.println("<center>You are visitor number <font color=\"red\">" + counter.getCounter() + "</font></center>");
         out.println("</body></html>");
    }

    public String toString() {
         if (filterConfig == null
            return ("OrderFilter()");
         StringBuffer sb = new StringBuffer("OrderFilter(");
         sb.append(filterConfig);
         sb.append(")");
         return (sb.toString());

    }
}
TOP

Related Classes of filters.HitCounterFilter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.