I have implemented a filter as below -
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
@WebFilter("/login.jsf")
public class ServiceFilter implements ContainerRequestFilter, ContainerResponseFilter, Filter {
public ServiceFilter() {}
@PostConstruct
public void init() {
logger.debug("initialized.");
}
@Override
public void destroy() {
logger.debug("destroyed");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
//nothing here
}
...other method overides
}
When I deploy it on Wildfly10
, I see the below gets printed
TIMESTAMP DEBUG ServiceFilter:65 - Initialized (ServiceFilter@39df1f0b)
TIMESTAMP DEBUG ServiceFilter:65 - Initialized (ServiceFilter@1a33dd9)
ServiceFilter
class is also registered as a root resource in one of the class which implements javax.ws.rs.core.Application
. This class acts as an entry point for rest and web resources.
How can I avoid the ServiceFilter
class from being initialized twice? Or, is this okay as the filter implements both servlet and rest filters? Or, should I move the implementation in their own class files?
PS: The above stated behavior doesn't actually hinder any functionality offered by my application (I think and could be otherwise) but just wanted to make sure that I'm doing it right.