tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ian Stevens" <>
Subject Using javax.servlet.Filter to alter HTTP headers (was RE: How can the Server header in an HTTP response be customised?)
Date Tue, 29 Jun 2004 15:09:04 GMT
> Is it possible to programmatically change the Server header?  
> Barring that, is it possible to set a Server header for a 
> servlet within its web.xml file?
> My least preferred method is to change the Server header 
> within Tomcat's server.xml file.

I looked at Tomcat's source code, and that Server header is hard-coded with
values read in from a properties file and set in source.  As such, there
looks to be no way to specify a different value in the server.xml or
web.xml.  A poor design decision in my mind, but nevertheless.

I was thinking that a javax.servlet.Filter which intercepts a call to set an
HTTP header would do the trick.  I wrote a naive Filter (listed below) and
added parameters to my web.xml (also shown below) to accomplish this.  Its
init() and doFilter() methods are called, but the setHeader() method of the
HttpServletResponseWrapper is never hit.

Can calls to setHeader() in Tomcat source, specifically HttpConnector, be
wrapped with Filter objects? or does that only apply to calls within servlet
source?  How can I wrap all calls to setHeader()?


 * The javax.servlet.Filter to set HTTP headers to null.
public class HttpRemoveHeaderFilter implements Filter
    private String headerToRemove;

    public void destroy()

    public void doFilter( ServletRequest request, ServletResponse response,
FilterChain chain ) throws IOException, ServletException
        chain.doFilter( request, new RemoveHeaderWrapper(
(HttpServletResponse)response, headerToRemove ) );

    public void init( FilterConfig config ) throws ServletException
        headerToRemove = config.getInitParameter( "remove" );

    private final class RemoveHeaderWrapper extends
        private String headerToRemove;

        public RemoveHeaderWrapper( HttpServletResponse response, String
headerToRemove )
            super( response );
            this.headerToRemove = headerToRemove;

        public void setHeader( String s, String s1 )
            if( s.matches( headerToRemove ) )
                super.setHeader( s, null );
                super.setHeader( s, s1 );

Below are the associated contents of my web.xml file.



To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message