0

I was really confused by this parts of web.xml:

<filter>
    <filter-name>filter</filter-name>
    <filter-class>com.labwork.filter.Filter</filter-class>
</filter>

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

This is the real problem. For example, I have several Servlets and static html pages. All request to them pass through filter. May be, i want to forward to servlet/html page from filter. How can i do this, if all request will be forward to filter? Or, may be, i don't understand principles.

6
  • 1
    All the request will be forwarded to the filter, then to your servlet/html if they passed the condition you provided on your filter. Commented May 10, 2017 at 0:11
  • @lxcky it provided by chain.doFilter? and how can i explicitly forward to servlet / html ? Commented May 10, 2017 at 0:23
  • Do you mean you want some request to go directly to their servlet/html pages and not go to the filter? If so, you should limit your url-pattern. /* means every request. Commented May 10, 2017 at 0:25
  • @lxcky No, I want all my request go throw filter. But i wan't specify in filter explicitly which servlet / html i want to forward. Commented May 10, 2017 at 0:29
  • Alright, let me compose an answer and see if it works out for you. Commented May 10, 2017 at 0:31

2 Answers 2

1

Based on our conversation in the comment section, you want to filter every request with an exception of a few pages.

Let's say you want to exempt the login.html from filtering, what you could do is get the request URI and check if that string contains login.html like this:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {                
    String path = ((HttpServletRequest) request).getRequestURI();
    if (path.contains("login.html")) //login page                                                
        chain.doFilter(request, response); //proceed to the page
    } else {            
        //conditions here
    }
}

I must state though that this is not the standard practice. If you're doing this then you should review your design choices.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to specify which servlet/html the request should be forwarded to, you can add <servlet-name> and <servlet-mapping> sections in your web.xml.

For example:

<servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>examples.Login</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
</servlet-mapping>

OR

<servlet>
    <servlet-name>LoginServlet</servlet-name>
    <jsp-file>/login.html</jsp-file>
</servlet>

<servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
</servlet-mapping>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.