servlets - Adding response header after doFilter -
i have searched , seen couple of post problem, did not find answer how possible.
what want add header after filter chain,
public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { httpservletresponse httpresp = (httpservletresponse) response; try { httpservletresponsewrapper bufferedresponse = new httpservletresponsewrapper (httpresp); chain.dofilter(request, bufferedresponse); } { // header added @ line not being added. bufferedresponse.setheader("add header: ", "header"); } }
multiple posts talking possible using httpservletresponsewrapper not working me, can me on this.
you can't add header (well, can won't have effect) after response has been committed since @ point http headers have been written client.
you have 3 options.
- write header before call dofilter()
- make sure (large buffer, small response, no calls flush() etc) response not committed before try , add header.
- wrap response before filter method , buffer entire response body in wrapper, add header afterwards , write out response body buffer.
Comments
Post a Comment