如何直接提供HTML页面时应用servlet过滤器? [英] How do I apply a servlet filter when serving an HTML page directly?

查看:119
本文介绍了如何直接提供HTML页面时应用servlet过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我使用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.0encoding =UTF-8?> 
< web-app xmlns =http://java.sun.com/xml/ns/javaeexmlns: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
版本= 2.5 >
< display-name> PuzzleBazar< / display-name>

<! - 要投放的默认页面 - >
< 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>

<! -
这个Guice侦听器劫持所有进一步的过滤器和servlet。额外的
过滤器和servlet必须通过调用
serve(String).with(Class< ;? extends HttpServlet>)和
filter()来配置
ServletModule#configureServlets字符串).through(Class< ;? extends Filter)
- >
< listener>
< listener-class> com.puzzlebazar.server.guice.MyGuiceServletContextListener
< / listener-class>
< / listener>

< / web-app>

我的 appengine-web.xml 是:

 <?xml version =1.0encoding =utf-8?> 
< appengine-web-app xmlns =http://appengine.google.com/ns/1.0>
< application> puzzlebazaar< / application>
< version> 1< / version>
<会话启用>真实< /会话启用>

<! - 配置java.util.logging - >
< system-properties>
< property name =java.util.logging.config.filevalue =WEB-INF / logging.properties/>
< / system-properties>

< / appengine-web-app>

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

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

但我的 SecurityCookieFilter.doFilter 永远不会被调用。我尝试了诸如*。html *< url-pattern> *< / url-pattern> 但无济于事。任何想法我应该如何做到这一点?

解决方案

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


First off, I'm using Google AppEngine and Guice, but I suspect my problem is not related to these.

When the user connect to my (GWT) webapp, the URL is a direct html page. For example, in development mode, it is: http://127.0.0.1:8888/Puzzlebazar.html?gwt.codesvr=127.0.0.1:9997. Now, I setup my web.xml in the following way:

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

And my appengine-web.xml is:

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

Since I'm using Guice, I have to configure extra filters in my ServletModule, where I do this:

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

But my SecurityCookieFilter.doFilter is never called. I tried things like "*.html*" or <url-pattern>*</url-pattern> but to no avail. Any idea how I should do this?

解决方案

You've probably configured html files to be served up as static content in your appengine-web.xml. Static file serving doesn't involve your app at all, so you can't filter the output.

这篇关于如何直接提供HTML页面时应用servlet过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆