【问题标题】:Filters - Ignore the other requests excluding the welcome file过滤器 - 忽略除欢迎文件之外的其他请求
【发布时间】:2013-08-31 07:15:59
【问题描述】:

我写了一个过滤器来决定用户登陆页面在它到达我的欢迎文件之前。欢迎文件的编写方式是从过滤器获取输入并将用户导航到特定页面。

我的欢迎文件标签是...

<welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

而我的过滤器配置是

<filter>
    <filter-name>LandingPageFilter</filter-name>
    <filter-class>com.mypack.test.filters.LandingPageFilter</filter-class>
</filter>

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

当每个请求都通过此过滤器时,上述格式运行良好,我想避免这种情况。当我点击http://localhost:8000/landing 时,它应该只第一次到达过滤器,然后即使我访问http://localhost:8000/landing/edit,它实际上应该绕过过滤器执行相关的servlet。

我也试过这个&lt;url-pattern&gt;/*/index.jsp&lt;/url-pattern&gt;,但没用。为什么我使用它,因为 上下文 可能会有所不同,但应用程序会相同。

【问题讨论】:

    标签: java jsp filter web.xml


    【解决方案1】:

    使用

    &lt;url-pattern&gt;/index.jsp&lt;/url-pattern&gt;

    而不是

    &lt;url-pattern&gt;/*/index.jsp&lt;/url-pattern&gt;

    所以你的过滤器映射部分会变成这样

    <filter-mapping>
        <filter-name>LandingPageFilter</filter-name>
        <url-pattern>/index.jsp</url-pattern>
    </filter-mapping>
    

    不管上下文如何,因为/ 代表上下文路径之后的路径。

    例如,如果您有两个上下文路径,例如 a 和 b,在您的服务器中部署了两个相同的应用程序,并且您正在使用 URL 访问它们

    http://localhost:8000/a/

    和

    http://localhost:8000/b/

    然后url-pattern 中的/ 在上下文根a 和b 之后代表/。所以你的过滤器只会为 index.jsp 执行。

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 2011-07-22
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 2014-07-04
      • 2012-10-08
      相关资源
      最近更新 更多