JSP Filter question

Discussion in 'Java' started by Steve Sobol, Sep 23, 2004.

  1. Steve Sobol

    Steve Sobol Guest

    So I have this filter I'm playing around with. It's a simple one, replaces all
    instances of my daughter's name with the word "Princess" wrapped in a couple
    html tags.

    It captures the output of any filters after it in a CharArray, and then does a
    regex search and replace.

    It works perfectly *if* there is a filter sitting after it in the chain. If
    not, however, chain.doFilter results in an empty CharArray. Is there any way to
    make this thing work if this filter is the only one installed in my webapp?

    (Ignore the package and class names - I'm eventually going to use this filter
    for something else but didn't feel like changing the names. The code obviously
    doesn't have anything to do with logging in to anything. :>)

    package net.justthe.csfilter;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class LoginFilter implements Filter {

    public void doFilter(ServletRequest rq, ServletResponse rs, FilterChain chain)
    throws IOException, ServletException {

    LoginRewriteWrapper ourRs = new
    LoginRewriteWrapper((HttpServletResponse)rs);
    chain.doFilter(rq,ourRs);

    String ourOutput=ourRs.toString();

    if (ourOutput.length==0) {
    ourOutput=rs.
    }



    Pattern p=Pattern.compile("eryne");
    Matcher m=p.matcher(ourOutput);

    String massagedOutput=m.replaceAll("<B><FONT
    COLOR=RED>Princess</FONT></B>");

    rs.setContentLength(massagedOutput.length());

    ServletOutputStream out = rs.getOutputStream();
    out.write(massagedOutput.getBytes());
    out.close();

    }

    public void destroy() {}

    public void init(FilterConfig filterConfig) {}


    }


    package net.justthe.csfilter;

    import java.io.CharArrayWriter;
    import java.io.PrintWriter;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpServletResponseWrapper;

    class LoginRewriteWrapper extends HttpServletResponseWrapper {

    private CharArrayWriter output;

    public String toString() {
    return output.toString();
    }

    public LoginRewriteWrapper(HttpServletResponse response) {
    super(response);
    output = new CharArrayWriter();
    }

    public PrintWriter getWriter() {
    return new PrintWriter(output);
    }

    }




    --
    JustThe.net Internet & New Media Services, http://JustThe.net/
    Steven J. Sobol, Geek In Charge / 888.480.4NET (4638) /
    PGP Key available from your friendly local key server (0xE3AE35ED)
    Apple Valley, California Nothing scares me anymore. I have three kids.
     
    Steve Sobol, Sep 23, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. javaguy44
    Replies:
    0
    Views:
    2,731
    javaguy44
    Apr 1, 2004
  2. Todd
    Replies:
    5
    Views:
    1,773
    Bryce
    Jul 27, 2005
  3. Harry Bosch
    Replies:
    5
    Views:
    1,985
    Harry Bosch
    Dec 6, 2005
  4. Replies:
    0
    Views:
    4,544
  5. zax75
    Replies:
    1
    Views:
    1,141
Loading...

Share This Page