Issue
I would like to use a filter for one specific servlet. My code is:
@WebFilter(dispatcherTypes = {
DispatcherType.REQUEST,
DispatcherType.FORWARD,
DispatcherType.INCLUDE
}
, servletNames = { "myServlet" })
public class ImageProtect implements Filter
{
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
System.out.println("Hello from Filter");
chain.doFilter(request, response);
}
}
My problem is that nothing happens. In the eclipse console Cant see the message unfortunately. The servlet is just a stock servlet. What is the problem?
Solution
Add this filter in web.xml file
<filter>
<filter-name>ImageProtect</filter-name>
<filter-class>ImageProtect</filter-class>
</filter>
<filter-mapping>
<filter-name>ImageProtect</filter-name>
<url-pattern>/myServlet</url-pattern>
</filter-mapping>
Answered By - Srinivasu
Answer Checked By - Mildred Charles (JavaFixing Admin)