Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6240669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:36:43+00:00 2026-05-24T11:36:43+00:00

I tried to do… Source FilterHolder myHolder = new FilterHolder(new Filter() { public void

  • 0

I tried to do…

Source

FilterHolder myHolder
    = new FilterHolder(new Filter() {
        public void init(FilterConfig fc) throws ServletException {
        }

        public void doFilter(ServletRequest req, ServletResponse resp,
                FilterChain fc) throws IOException, ServletException {
            HttpServletRequest httpReq = (HttpServletRequest) req;
            HttpServletResponse httpResp = (HttpServletResponse) resp;

            // HERE:
            InputStream is = httpReq.getInputStream();
            // (read is to a string and output it, works,
            // but swallows all data forever)

            fc.doFilter(httpReq, httpResp);
        }

        public void destroy() {
        }
    });

… but swallows all data and real servlets don’t get anything.

I just want to "read" POST request contents and output them for debugging.

NOTE 1: I don’t want to "intercept" requests, they should go through as before.

NOTE 2: An additional hint, how to do the same with POST responses would be very kind.

EDIT Replaced Reader with InputStream. Reader didn’t work at all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:36:45+00:00Added an answer on May 24, 2026 at 11:36 am

    Got it! I’m using a wrapper for InputStream and OutputStream each.
    Tested. Works for both directions.

    HttpRequestCopyFilter

    final class HttpRequestCopyFilter implements Filter {
    
    private final OutputStream copyOutput;
    
    public HttpRequestCopyFilter(OutputStream copyOutput) {
        this.copyOutput = copyOutput;
    }
    
    public void init(FilterConfig arg0) throws ServletException {
    }
    
    public void destroy() {
    }
    
    private void flushCopy() throws IOException {
        copyOutput.flush();
    }
    
    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain fc) throws IOException, ServletException {
        HttpServletRequest httpReq = (HttpServletRequest) req;
        HttpServletResponse httpResp = (HttpServletResponse) resp;
    
        HttpServletRequestWrapper requestWrapper =
                new HttpServletRequestWrapper(httpReq) {
    
                    @Override
                    public ServletInputStream getInputStream()
                            throws IOException {
                        final ServletInputStream original =
                                super.getInputStream();
    
                        return new ServletInputStream() {
    
                            @Override
                            public int read() throws IOException {
                                int c = original.read();
                                if (c >= 0) {
                                    copyOutput.write(c);
                                    flushCopy();
                                }
                                return c;
                            }
    
                            @Override
                            public int read(byte[] b) throws IOException {
                                int count = original.read(b);
                                if (count >= 0) {
                                    copyOutput.write(b, 0, count);
                                    flushCopy();
                                }
                                return count;
                            }
    
                            @Override
                            public int read(byte[] b, int off, int len)
                                    throws IOException {
                                int count = original.read(b, off, len);
                                if (count >= 0) {
                                    copyOutput.write(b, off, count);
                                    flushCopy();
                                }
                                return count;
                            }
                        };
                    }
                };
    
        fc.doFilter(requestWrapper, httpResp);
    }
    }
    

    HttpResponseCopyFilter

    final class HttpResponseCopyFilter implements Filter {
    
    private final OutputStream copyOutput;
    
    public HttpResponseCopyFilter(OutputStream copyOutput) {
        this.copyOutput = copyOutput;
    }
    
    public void init(FilterConfig arg0) throws ServletException {
    }
    
    public void destroy() {
    }
    
    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain fc) throws IOException, ServletException {
        HttpServletRequest httpReq = (HttpServletRequest) req;
        HttpServletResponse httpResp = (HttpServletResponse) resp;
    
        HttpServletResponseWrapper responseWrapper =
                new HttpServletResponseWrapper(httpResp) {
    
                    @Override
                    public ServletOutputStream getOutputStream()
                            throws IOException {
                        final ServletOutputStream original =
                                super.getOutputStream();
                        return new ServletOutputStream() {
    
                            @Override
                            public void write(int b) throws IOException {
                                original.write(b);
                                copyOutput.write(b);
                                flush();
                            }
    
                            @Override
                            public void write(byte[] b) throws IOException {
                                original.write(b);
                                copyOutput.write(b);
                                flush();
                            }
    
                            @Override
                            public void write(byte[] b, int off, int len)
                                    throws IOException {
                                original.write(b, off, len);
                                copyOutput.write(b, off, len);
                                flush();
                            }
    
                            @Override
                            public void flush() throws IOException {
                                original.flush();
                                copyOutput.flush();
                                super.flush();
                            }
    
                            @Override
                            public void close() throws IOException {
                                original.close();
                                copyOutput.flush(); // DON'T CLOSE COPY-OUTPUT !!!
                                super.close();
                            }
                        };
                    }
                };
    
        fc.doFilter(httpReq, responseWrapper);
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tried this: int idx4 = ppt.addMovie(new File(Animation.avi).getCanonicalPath(), MovieShape.MOVIE_AVI); MovieShape pict4 = new MovieShape(idx4, idx4);
tried using the following but it's not the solution controller: var list = new
Tried to search but no specific answers (I am very new in this)... With
Tried with : IList<IList<string>> matrix = new List<new List<string>()>(); but I can't. How can
Tried to create a SQL 2005 DB project. In Vs, New Project >> Sql
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
Tried something like this: HttpApplication app = s as HttpApplication; //s is sender of
Tried following the instructions here: How to use Google app engine with my own
Tried this: $('.link').click(function(e) { $.getScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0', function() { $('body').append('<p>GOOGLE API (UDS) is loaded</p>'); }); return
Tried a bunch of things but I can't get it to work consistently amid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.