Women in Technology

Hear us Roar



Article:
  Two Servlet Filters Every Web Application Should Have
Subject:   Geting Binary Data
Date:   2004-05-15 09:37:11
From:   gselvara
Hi,
I am getting binary data in the resultant html page only for some pages (when using compression). Could you help me out? I can see that the compressing page message getting printed twice in the system out for the pages that get binary data. I think they are getting compressed twice?
Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • Geting Binary Data
    2004-05-17 09:08:44  gselvara [View]

    Well I found a way to get over this. It seems that in my application the same jsp gets called twice or thrice this means the output is compressed over and over giving me funny characters that looks like binary data (with help from nagesh@bea.com and his http://ftpna2.bea.com/pub/downloads/Gzipfilter_war.zip) thanks nagesh. Here the code fix,

    package com.jspbook;

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class GZIPFilter implements Filter {
    private static final String WL_GZIP_FILTER = "com.jspbook.GZIPFilter";

    public void doFilter(ServletRequest req, ServletResponse res,
    FilterChain chain) throws IOException, ServletException {
    if(!(res instanceof HttpServletResponse) || !(req instanceof HttpServletRequest)){
    chain.doFilter(req, res);
    return;
    }

    if (req instanceof HttpServletRequest) {
    HttpServletResponse httpservletresponse = (HttpServletResponse)res;
    HttpServletRequest httpservletrequest = (HttpServletRequest)req;
    if(httpservletrequest.getAttribute(WL_GZIP_FILTER) != null){
    chain.doFilter(req, res);
    return;
    }
    httpservletrequest.setAttribute(WL_GZIP_FILTER, "true");
    boolean flag = false;

    String ae = httpservletrequest.getHeader("accept-encoding");
    if (ae != null && ae.indexOf("gzip") != -1) {
    GZIPResponseWrapper wrappedResponse =
    new GZIPResponseWrapper(httpservletresponse);
    chain.doFilter(req, wrappedResponse);
    wrappedResponse.finishResponse();
    return;
    }else{
    chain.doFilter(req, res);
    return;
    }
    }else{
    chain.doFilter(req, res);
    return;
    }
    }
    public void init(FilterConfig filterConfig) {
    }
    public void destroy() {
    }
    }