0

I am new to the servlet technology. I set up Java 7, Tomcat8 and Eclipse. I have created several servlets on Eclipse and everything is working fine.

Today I created a simple servlet filter. But its not executing before any servlet . I can see the init method gets executed using the console messages. As per servlet 3.0 I am not using web.xml but @WebFilter("/FilterDemo") annotation.

here is the servlet filter code,

package net.codejava;

import java.io.IOException;
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;

/**
 * Servlet Filter implementation class FilterDemo
 */
@WebFilter("/FilterDemo")
public class FilterDemo implements Filter {

/** 
 * Default constructor. 
 */
public FilterDemo() {
    // TODO Auto-generated constructor stub
}

/**
 * @see Filter#destroy()
 */
public void destroy() {
    // TODO Auto-generated method stub
    System.out.println("Destroy is called.");
}

/**
 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    // TODO Auto-generated method stub
    // place your code here

    String ipAddress = request.getRemoteAddr();
    System.out.println("Do Filter is called.");
    System.out.println(ipAddress);

    // pass the request along the filter chain
    chain.doFilter(request, response);
}

/**
 * @see Filter#init(FilterConfig)
 */
public void init(FilterConfig fConfig) throws ServletException {
    // TODO Auto-generated method stub
    System.out.println("Filter Init is called."); 
}

}

Screenshot of Workspace: enter image description here

Any help will be greatly appreciated.

user3427540
  • 1,162
  • 1
  • 14
  • 30
  • Kindly share mapping for the servlet for which the filter triggers – Mudassar Jun 24 '15 at 14:23
  • @Mudassar: I know we need to add mapping for the servlet for which the filter will trigger.I thought Eclipse will create automatically for me. Could you please tell me where do i need to add that mapping – user3427540 Jun 24 '15 at 14:27
  • :) yes we do need mapping for the servlet like this @WebServlet("/FilterDemo") public class AnyServlet extends HttpServlet – Mudassar Jun 24 '15 at 14:30

4 Answers4

1

Filters sit in front of servlets. In your annotation, you specified that the filter should only filter requests that go to /FilterDemo. What you'll need to do is map the filter to either the same URL as one of your servlets, or specify the names of the servlets that you want the filter to do processing for (using the servletNames parameter of the @WebFilter annotation.

Ian McLaird
  • 5,507
  • 2
  • 22
  • 31
  • Thanks alot Ian, I have used `@WebFilter(filterName = "/FilterDemo",urlPatterns = "/HelloServlet" )` , now everything working as expected. – user3427540 Jun 24 '15 at 14:34
1

You have to declare your filter mapping in web.xml despite the fact you are using @WebFilter annotation.

Szarpul
  • 1,531
  • 11
  • 21
  • 1
    Web.xml is not compulsory, This worked for me `@WebFilter(filterName = "/FilterDemo",urlPatterns = "/HelloServlet" )` – user3427540 Jun 24 '15 at 14:35
  • No you don't. You only *need* the web.xml file if you need the filters to execute in a particular order. – Ian McLaird Jun 24 '15 at 14:39
  • Yes, but I think it is recommended, because when you have more then one filter, the order in which you list your filters in web,xml is the order in which they will be executed. – Szarpul Jun 24 '15 at 14:40
1

As per servlet 3.0

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet{...}

@WebFilter(filterName="filter1", urlPatterns={ "/LoginServlet" })
public class AuthenticationFilter implements Filter {..}

but you should still keep the <url-pattern> in web.xml, because it's required as per XSD, although it can be empty:

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

See also Using Tomcat, @WebFilter doesn't work with inside web.xml

Community
  • 1
  • 1
Mudassar
  • 3,135
  • 17
  • 22
0

See how to map Servlet to the filter.

web.xml:

<filter>  
<filter-name>f1</filter-name>  
<filter-class>MyFilter</filter-class>  
</filter>  

<filter-mapping>  
<filter-name>f1</filter-name>  
<url-pattern>/servlet1</url-pattern> 
</filter-mapping>  

here /servlet1 is url pattern of your servlet(Which you want to execute).