[servlets] Giving multiple URL patterns to Servlet Filter

I am using a Servlet Filter in my JSF application. I have three groups of Web pages in my application, and I want to check Authentication for these pages in my Servlet Filter:

my Folders

/Admin/ *.xhtml

/Supervisor/*.xhtml
/Employee/*.xhtml

and I am writing web.xml like

<filter>
    <filter-name>LoginFilter</filter-name>
    <filter-class>com.ems.admin.servlet.LoginFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>LoginFilter</filter-name>
    <url-pattern>/Employee/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>LoginFilter</filter-name>
    <url-pattern>/Admin/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>LoginFilter</filter-name>
    <url-pattern>/Supervisor/*</url-pattern>
</filter-mapping>

but requests like

http://localhost:8080/EMS2/faces/Html/Admin/Upload.xhtml

are not entering into Filter.

I have to provide security to these 3 folders.

How to solve this problem ?

This question is related to servlets servlet-filters url-pattern

The answer is


In case you are using the annotation method for filter definition (as opposed to defining them in the web.xml), you can do so by just putting an array of mappings in the @WebFilter annotation:

/**
 * Filter implementation class LoginFilter
 */
@WebFilter(urlPatterns = { "/faces/Html/Employee","/faces/Html/Admin", "/faces/Html/Supervisor"})
public class LoginFilter implements Filter {
    ...

And just as an FYI, this same thing works for servlets using the servlet annotation too:

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet({"/faces/Html/Employee", "/faces/Html/Admin", "/faces/Html/Supervisor"})
public class LoginServlet extends HttpServlet {
    ...

Examples related to servlets

Google Recaptcha v3 example demo Difference between request.getSession() and request.getSession(true) init-param and context-param java.lang.NoClassDefFoundError: org/json/JSONObject how to fix Cannot call sendRedirect() after the response has been committed? getting error HTTP Status 405 - HTTP method GET is not supported by this URL but not used `get` ever? Create a simple Login page using eclipse and mysql Spring get current ApplicationContext insert data into database using servlet and jsp in eclipse What is WEB-INF used for in a Java EE web application?

Examples related to servlet-filters

How to add a filter class in Spring Boot? error: package javax.servlet does not exist Giving multiple URL patterns to Servlet Filter Http Servlet request lose params from POST body after read it once How to define servlet filter order of execution using annotations in WAR How to redirect in a servlet filter? How can I get the request URL from a Java Filter? Can I exclude some concrete urls from <url-pattern> inside <filter-mapping>? Adding an HTTP Header to the request in a servlet filter How to use a servlet filter in Java to change an incoming servlet request url?

Examples related to url-pattern

What is the significance of url-pattern in web.xml and how to configure servlet? Giving multiple URL patterns to Servlet Filter Difference between / and /* in servlet mapping url pattern