Tomcat displaying welcome-page before filter for jsp is executed

拟墨画扇 提交于 2019-12-12 01:53:45

问题


I am using Tomcat 6.0.36 and the welcome-page is /Login.jsp I have a filter in place so that it can display a different login page for mobile devices. It works with URL mywebsite.com/Login.jsp, but the filter is bypassed when the URL is just mywebsite.com.

Is there a way to force it to execute?

I have found this page but it doesn't work in my case:

How to map a filter for welcome-file in web.xml for Tomcat?

Thanks

My web.xml:

<welcome-file-list>
    <welcome-file>/Login.jsp</welcome-file>
</welcome-file-list>
...
<filter>
    <display-name>LoginPageFilter</display-name>
    <filter-name>LoginPageFilter</filter-name>
    <filter-class>filters.LoginPageFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>LoginPageFilter</filter-name>
    <url-pattern>/Login.jsp</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

The filter - I had deleted it and put one quickly back together

public class LoginPageFilter implements Filter 
{
    public LoginPageFilter()  { }

    public void init ( FilterConfig fConfig ) throws ServletException { }

    public void doFilter ( ServletRequest request, ServletResponse response,
        FilterChain chain ) throws IOException, 
    ServletException 
    {
        System.out.println ( "Filter being executed" );
        chain.doFilter(request, response);
    }

    public void destroy() { }
}

If the URL is

http://localhost:8080/gymfit/Login.jsp

then the message is printed to the console.

When the URL is

http://localhost:8080/gymfit/

the same page is displayed but the message is not printed out to the console


回答1:


look at this line, this means only the request to '/Login.jsp' will the filter being executed

    <url-pattern>/Login.jsp</url-pattern>

if you want to apply this filter to all the path, change the config to:

    <url-pattern>/*</url-pattern>


来源:https://stackoverflow.com/questions/16456762/tomcat-displaying-welcome-page-before-filter-for-jsp-is-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!