我编写了一个过滤器来在用户登陆页面到达我的欢迎文件之前决定它。欢迎文件的编写方式是从过滤器获取输入并将用户导航到特定页面。
我的欢迎文件标签是...
<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。
我也尝试过这个<url-pattern>/*/index.jsp</url-pattern>
但没有用。为什么我使用它,因为上下文可能会有所不同,但应用程序是相同的。
最佳答案
使用
<url-pattern>/index.jsp</url-pattern>
而不是
<url-pattern>/*/index.jsp</url-pattern>
所以你的过滤器映射部分会变成这样
<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 执行。
关于java - 过滤器 - 忽略除欢迎文件之外的其他请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18484108/