Return-Path: Delivered-To: apmail-jakarta-tomcat-user-archive@www.apache.org Received: (qmail 82016 invoked from network); 29 Jun 2004 15:09:54 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 29 Jun 2004 15:09:54 -0000 Received: (qmail 2997 invoked by uid 500); 29 Jun 2004 15:09:17 -0000 Delivered-To: apmail-jakarta-tomcat-user-archive@jakarta.apache.org Received: (qmail 2966 invoked by uid 500); 29 Jun 2004 15:09:17 -0000 Mailing-List: contact tomcat-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Tomcat Users List" Reply-To: "Tomcat Users List" Delivered-To: mailing list tomcat-user@jakarta.apache.org Received: (qmail 2944 invoked by uid 99); 29 Jun 2004 15:09:16 -0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received: from [209.250.128.26] (HELO smtp.tor.pathcom.com) (209.250.128.26) by apache.org (qpsmtpd/0.27.1) with ESMTP; Tue, 29 Jun 2004 08:09:16 -0700 Received: from TK421 (gwpptc.tor.axxent.ca [207.188.72.110]) by smtp.tor.pathcom.com (8.12.10/8.12.6) with ESMTP id i5TF96ba011276 for ; Tue, 29 Jun 2004 11:09:06 -0400 Message-Id: <200406291509.i5TF96ba011276@smtp.tor.pathcom.com> From: "Ian Stevens" To: "'Tomcat Users List'" 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 11:09:04 -0400 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook, Build 11.0.5510 Thread-Index: AcRaAyT04L8hgrgESKWbdliouwP7RwD5SyEg In-Reply-To: <200406241551.i5OFpgba019494@smtp.tor.pathcom.com> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N > 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. removeHeader HttpRemoveHeaderFilter remove Server removeHeader /servlet --------------------------------------------------------------------- To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: tomcat-user-help@jakarta.apache.org