2010-06-09 126 views
7

在Jetty 6.1中設置HTTP標頭(基於文件名模式)的最佳方式是什麼?是否可以通過jetty.xml(或jetty-web.xml)?或者我必須修改web.xml?使用Jetty設置HTTP標頭

回答

8

您可以按配置添加標題。在的jetty.xml,把下面的例子:

<New id="RewriteHandler" class="org.eclipse.jetty.rewrite.handler.RewriteHandler"> 
    <Set name="rules"> 
    <Array type="org.eclipse.jetty.rewrite.handler.Rule"> 

     <Item> 
     <New id="header" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule"> 
      <Set name="pattern">*.jsp</Set> 
      <Set name="name">myheader</Set> 
      <Set name="value">the value of myheader</Set> 
     </New> 
     </Item> 

    </Array> 
    </Set> 
</New> 

<Set name="handler"> 
    <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection"> 
    <Set name="handlers"> 
     <Array type="org.eclipse.jetty.server.Handler"> 
     <Item> 
      <Ref id="RewriteHandler"/> 
     </Item> 
     </Array> 
    </Set> 
    </New> 
</Set> 

對什麼是可能的更多例子見RewriteHandler api(它已經可以在Jetty 6 RewriteHandler

的信息,這裏是碼頭插件我Maven的配置:

<plugin> 
    <groupId>org.mortbay.jetty</groupId> 
    <artifactId>jetty-maven-plugin</artifactId> 
    <version>8.1.5.v20120716</version> 
    <configuration> 
    <jettyXml>${basedir}/src/main/etc/jetty.xml</jettyXml> 
    </configuration> 
    <dependencies> 
    <dependency> 
     <groupId>org.eclipse.jetty</groupId> 
     <artifactId>jetty-http</artifactId> 
     <version>8.1.5.v20120716</version> 
     <type>jar</type> 
     <scope>runtime</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.eclipse.jetty</groupId> 
     <artifactId>jetty-rewrite</artifactId> 
     <version>8.1.5.v20120716</version> 
     <type>jar</type> 
     <scope>runtime</scope> 
    </dependency> 
    </dependencies> 
</plugin> 
+0

鏈接已死 – Rushil 2018-02-13 19:12:16

8

通用給我的回答是當然,這個的:

<web-app> 

    <filter> 
    <filter-name>headersFilter</filter-name> 
    <filter-class>com.example.MyHeadersFilter</filter-class> 
    </filter> 

    <filter-mapping> 
    <filter-name>headersFilter</filter-name> 
    <url-pattern>*</url-pattern> 
    </filter-mapping> 

    ... 

</web-app> 

public class MyHeadersFilter implements Filter { 

    @Override 
    public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) 
      throws IOException, 
      ServletException { 

     final HttpServletRequest httpRequest = (HttpServletRequest) request; 
     final HttpServletResponse httpResponse = (HttpServletResponse) response; 

     final String requestUri = httpRequest.getRequestURI(); 

     if (requestUri.matches(...)) { 
      httpResponse.addHeader(...); 
     } 

     chain.doFilter(request, response); 
    } 
} 

這應該在任何JavaEE的Web容器中工作(並且可以由多個可配置與<init-param> S)。

但是沒有辦法在Jetty中完全聲明這樣做嗎?

+0

接受,直到有人發現了怎麼辦呢聲明。 – 2010-06-12 18:23:12