On Thu, 2014-03-13 at 23:16 -0400, Tim Watts wrote: > On Thu, 2014-03-13 at 19:11 -0700, Brendan Miller wrote: > > To be clear, I'm trying to modify the response that comes back from the > > service. I have a header that I may or may not need to set based on the > > response code. I believe the service sets the response code? > > That's true. Actually, let me correct myself: that may not be strictly true. I don't think there's anything in the spec that says the response code will be set when service() returns UNLESS the request results in an error.
-- Tim. > Unfortunately, by the time your filter gets control again, > the response will very likely be committed thus shutting out any > possibility of setting any headers. > > Doing this in a Filter, while intuitively sensible, will always be a > highly fragile solution. You would have to set a buffer large enough to > accommodate the largest conceivable response size. Once a response is > committed you can't set the response code or headers -- they're already > on their way to the client. There's no getting around this. > > I think the best approach is to conditionally set the header before any > response is generated. If your application has an MVC structure you > could do this just before the Controller invokes the View. You should > know the response code by then. > > HTH > > -- Tim > > > > > > > On Thu, Mar 13, 2014 at 6:20 PM, Martin Gainty <mgai...@hotmail.com> wrote: > > > > > you'll need to pass your modified response to service method of servlet > > > which is *in* the filterChain > > > > > > > > > ApplicationFilterChain::internalDoFilter(ServletRequest request, > > > ServletResponse response) > > > throws IOException, ServletException > > > > > > { > > > ............ > > > servlet.service(request, response); > > > > > > ........... > > > > > > } > > > > > > Martin > > > > > > > > > > > > > > > > > > > > > > Date: Thu, 13 Mar 2014 17:51:59 -0700 > > > > Subject: filter question > > > > From: catph...@catphive.net > > > > To: users@tomcat.apache.org > > > > > > > > I have a filter with doFilter method like this: > > > > > > > > public void doFilter(ServletRequest request, > > > > ServletResponse response, > > > > FilterChain chain) > > > > throws IOException, ServletException { > > > > HttpServletRequest req = (HttpServletRequest) request; > > > > HttpServletResponse resp = (HttpServletResponse) response; > > > > > > > > resp.setHeader("Cache-Control", > > > > "must-revalidate, max-age=0, post-check=0, > > > > pre-check=0"); > > > > > > > > chain.doFilter(request, response); > > > > } > > > > > > > > This sets the header. However, if I set the header *after* > > > chain.doFilter, > > > > the header is not set. Why is this? > > > > > > > > public void doFilter(ServletRequest request, > > > > ServletResponse response, > > > > FilterChain chain) > > > > throws IOException, ServletException { > > > > HttpServletRequest req = (HttpServletRequest) request; > > > > HttpServletResponse resp = (HttpServletResponse) response; > > > > > > > > chain.doFilter(request, response); > > > > > > > > resp.setHeader("Cache-Control", > > > > "must-revalidate, max-age=0, post-check=0, > > > > pre-check=0"); > > > > } > > > > > > > > Programmatically I can see the header is null. > > > > > > > > Has the content already been sent to the web browser after > > > chain.doFilter? > > > > If so, is there a way to delay sending data to the browser? I need to > > > > inspect the status code in the response before setting my header (to > > > > prevent 404's from being cached). > > > > > > > > Thanks, > > > > Brendan Miller > > > > > > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org > --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org