【问题标题】:How do I apply a servlet filter when serving an HTML page directly?直接提供 HTML 页面时如何应用 servlet 过滤器?
【发布时间】:2011-02-15 18:04:15
【问题描述】:

首先,我使用的是 Google AppEngine 和 Guice,但我怀疑我的问题与这些无关。

当用户连接到我的 (GWT) webapp 时,URL 是一个直接的 html 页面。例如,在开发模式下为:http://127.0.0.1:8888/Puzzlebazar.html?gwt.codesvr=127.0.0.1:9997。现在,我通过以下方式设置我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 version="2.5">
 <display-name>PuzzleBazar</display-name>

 <!-- Default page to serve -->
 <welcome-file-list>
  <welcome-file>Puzzlebazar.html</welcome-file>
 </welcome-file-list>


 <filter>
  <filter-name>guiceFilter</filter-name>
  <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
 </filter>

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

 <!--
  This Guice listener hijacks all further filters and servlets. Extra
  filters and servlets have to be configured in your
  ServletModule#configureServlets() by calling
  serve(String).with(Class<? extends HttpServlet>) and
  filter(String).through(Class<? extends Filter)
 -->
 <listener>
  <listener-class>com.puzzlebazar.server.guice.MyGuiceServletContextListener
  </listener-class>
 </listener>

</web-app>

而我的appengine-web.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>puzzlebazaar</application>
    <version>1</version>
    <sessions-enabled>true</sessions-enabled>

    <!-- Configure java.util.logging -->
    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>     
    </system-properties>

</appengine-web-app>

由于我使用的是 Guice,我必须在我的 ServletModule 中配置额外的过滤器,我这样做:

filter("*.html").through( SecurityCookieFilter.class );

但我的SecurityCookieFilter.doFilter 永远不会被调用。我尝试了"*.html*"&lt;url-pattern&gt;*&lt;/url-pattern&gt; 之类的方法,但无济于事。知道我应该怎么做吗?

【问题讨论】:

  • 我发现它可以通过切换到“.jsp”而不是“.html”来工作。

标签: google-app-engine tomcat gwt servlets guice


【解决方案1】:

您可能已将 html 文件配置为在 appengine-web.xml 中作为静态内容提供。静态文件服务根本不涉及您的应用,因此您无法过滤输出。

【讨论】:

  • 谢谢。我已经在上面添加了 appengine-web.xml。有什么遗漏吗?
  • 看起来您的 html 是作为静态文件提供的,正如我所说,这意味着它根本不会触及您的代码。如果您希望文件可进行后处理,则需要创建一个 servlet 来提供文件。
  • 所以,你不能在 html 页面上使用过滤器,但它可以在 servlet 和 jsp 上工作?
【解决方案2】:

如果您需要(像我一样)让 GAE 为静态 HTML 文件添加某些 http 响应标头,您可以通过在 appengine-web.xml 中将它们声明为静态资源来做到这一点,如下所示:

<static-files>
  <include path="/my_static-files" >
    <http-header name="Access-Control-Allow-Origin" value="http://example.org" />
  </include>
</static-files>

我在GAE documentation找到了这个

【讨论】:

  • 为此欢呼,因为它可以为人们省去很多麻烦。对我来说不幸的是,这有点不完美,因为我想要域的 301 重定向(从 www. 到裸),所以如果他们支持一个域属性,就像他们做一个路径一样,那将是完美的(会服务位置标头)。但是 AFAIK(在阅读了其余的文档之后)这是不可能的
猜你喜欢
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 2021-05-29
  • 2013-02-13
  • 2021-12-03
相关资源
最近更新 更多