In this tutorial you will learn what the difference is in the registration of Filter in older version and servlet 3.0 (Java EE6).
@WebFilter Servlet Example
In this tutorial you will learn what the difference is in the registration of Filter in older version and servlet 3.0 (Java EE6). The example given below will demonstrate you how to use a @WebFilter annotation. Here it is assumed that you already knows the implementation of Filter in servlet. Here I will explain only what is Filter and why it is required to implement. Filter is an object that filters either the request made for a resource or the response generated from the resource, or both. It intercepts the request and response dynamically. Except creation of responses for themselves Filters allows the universal functions to connect the servlet or any other web resources.
In this example I will use @WebFilter annotation to register a Filter class to use with servlet. Here one thing you will have to remember that your annotated Filter class must implement the Filter interface. @WebFilter has the following attributes :
- filterName
- description
- displayName
- initParams
- servletNames
- value
- urlPatterns
- dispatcherTypes
- asyncSupported
Example
FilterTesterServlet.java
package roseindia.net.filterExample; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.annotation.WebServlet; @WebServlet(name= "FilterTesterServlet", urlPatterns= "/filterTesterServlet") public class FilterTesterServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { PrintWriter out= response.getWriter(); out.println("This page will execute after the filter execution"); } }
WebFilterExample.java
package roseindia.net.filterExample; import java.io.IOException; import java.io.PrintWriter; 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.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.annotation.WebFilter; @WebFilter(filterName= "WebFilterExample", urlPatterns={"/filterTesterServlet"}) public class WebFilterExample implements Filter { public void init(FilterConfig config) throws ServletException { System.out.println("init() method is called"); } public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws IOException, ServletException { HttpServletRequest request= (HttpServletRequest)req; HttpServletResponse response= (HttpServletResponse)res; PrintWriter out= response.getWriter(); out.println("<h3>@WebFilter demo example</h3>"); } public void destroy() { System.out.println("destroy() method is called"); } }
Output :
When you will execute the servlet class you will get the output as :