> 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()?

thanks,
Ian.

/**
 * 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
HttpServletResponseWrapper
    {
        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 );
            }
            else
            {
                super.setHeader( s, s1 );
            }
        }
    }
}


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

<filter>
        <filter-name>removeHeader</filter-name>
        <filter-class>HttpRemoveHeaderFilter</filter-class>
        <init-param>
                <param-name>remove</param-name>
                <param-value>Server</param-value>
        </init-param>
</filter>

<filter-mapping>
        <filter-name>removeHeader</filter-name>
        <url-pattern>/servlet</url-pattern>
</filter-mapping>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to