Registering a Servlet Filter with @WebFilter annotation
- javastrokes
- Sep 20, 2016
- 3 min read
In the previous, Java Servlet Filters Example Application post, I configured filters in web.xml file. Instead, you can configure your filters, using @WebFilter annotation.
javax.servlet.annotation.WebFilter
public @interface WebFilter
This @WebFilter annotation is used to declare a servlet filter. During deployment time, web container will process this annotation and the corresponding filter applied to the specified URL patterns, servlet names. A class that use this annotation, must implement javax.servlet.Filter interface.
In this Registering a Servlet Filter with @WebFilter annotation example web application, I am going to configure these filters( LoginFilter & PromocodeFilter) using @WebFilter annotation. Rest of the application is same as we did in the previous 'Java Servlet Filters Example Application'.

Customer.Java
package com.beans;
public class Customer {
private String customerId; private String customerName; private String promoCode; public String getCustomerId() { return customerId; } public void setCustomerId(String customerId) { this.customerId = customerId; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public String getPromoCode() { return promoCode; } public void setPromoCode(String promoCode) { this.promoCode = promoCode; } public String toString(){ return "Dear Customer, your promocode is valid! " +"/n" + "Customer ID: " + customerId + "Customer Name: " + customerName + "Promo Code: " + promoCode; } }
LoginFilter.java
Here, LoginFilter is configured using @WebFilter annotation, instead of configuring it in web.xml.
package com.javastrokes.filter;
import java.io.*; import javax.servlet.*; import javax.servlet.annotation.WebFilter;
import com.beans.Customer;
@WebFilter( urlPatterns = "/login.do", filterName = "loginFilter" )
public class LoginFilter implements Filter { public void init(FilterConfig fc) throws ServletException {} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { PrintWriter out = response.getWriter(); String customerId = request.getParameter("customerId"); String customerName = request.getParameter("customerName"); if( customerId.equals("Ric123") && customerName.equals("Rick")) { Customer customerObject = new Customer(); customerObject.setCustomerId(customerId); customerObject.setCustomerName(customerName); request.setAttribute("customer", customerObject); chain.doFilter(request, response); } else { out.println("You have entered wrong Customer Id or Password!"); RequestDispatcher rs = request.getRequestDispatcher("login.jsp"); rs.include(request, response); } } public void destroy() { } }
PromocodeFilter.java
Here, PromocodeFilter is configured using @WebFilter annotation, instead of configuring it in web.xml.
package com.javastrokes.filter;
import java.io.*; import javax.servlet.*; import javax.servlet.annotation.WebFilter;
import com.beans.Customer;
@WebFilter( urlPatterns = "/login.do", filterName = "promocodeFilter" ) public class PromocodeFilter implements Filter { public void init(FilterConfig fc) throws ServletException {} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { PrintWriter out = response.getWriter(); String promoCode = request.getParameter("promoCode"); if( promoCode.equals("OFF33") ) { Customer customerObject = (Customer)request.getAttribute("customer"); customerObject.setPromoCode(promoCode); request.setAttribute("customer", customerObject); chain.doFilter(request, response); } else { out.println("You have entered wrong Promocode!"); RequestDispatcher rs = request.getRequestDispatcher("login.jsp"); rs.include(request, response); } } public void destroy() { } }
LoginServlet.java
package com.javastrokes.filter;
import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; RequestDispatcher rd; public LoginServlet() { super(); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
rd = getServletContext().getRequestDispatcher("/success.jsp"); rd.forward(request, response); }
}
Check the elements of web.xml. Unlike previous application (Java Servlet Filters Example Application), here web.xml only contains servlet configuration.
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>promoServlet</servlet-name> <servlet-class>com.javastrokes.filter.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>promoServlet</servlet-name> <url-pattern>/login.do</url-pattern> </servlet-mapping>
</web-app>
login.jsp
<!DOCTYPE html> <html> <head> <style type="text/css"> .tftable {font-size:15px;color:#333333;width:100%;border-width: 1px;border-color: #ebab3a;border-collapse: collapse;} .tftable th {font-size:15px;background-color:#e6983b;border-width: 1px;padding: 8px;border-style: solid;border-color: #ebab3a;text-align:left;} .tftable tr {background-color:#f0c169;} .tftable td {font-size:15px;border-width: 1px;padding: 8px;border-style: solid;border-color: #ebab3a;} .tftable tr:hover {background-color:#ffffff;} </style> <meta charset="US-ASCII"> <title>Login Page</title> </head> <body>
<form action="login.do" method="post">
<table class="tftable" border="1"> <tr><th>Customer Details:</th></tr> <tr><td>Customer ID:</td><td> <input type="text" name="customerId"> </td></tr> <tr><td>Customer Name:</td><td><input type="text" name="customerName"></td></tr> <tr><td>Promo Code:</td><td><input type="text" name="promoCode"></td></tr> <tr><td><input type="submit" value="Login"></td></tr> </table>
</form> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=US-ASCII" pageEncoding="US-ASCII"%> <%@ page isELIgnored="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <style type="text/css"> .tftable {font-size:15px;color:#333333;width:100%;border-width: 1px;border-color: #ebab3a;border-collapse: collapse;} .tftable th {font-size:15px;background-color:#e6983b;border-width: 1px;padding: 8px;border-style: solid;border-color: #ebab3a;text-align:left;} .tftable tr {background-color:#f0c169;} .tftable td {font-size:15px;border-width: 1px;padding: 8px;border-style: solid;border-color: #ebab3a;} .tftable tr:hover {background-color:#ffffff;} </style> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title> Success Page</title>
</head>
<body>
<h3>Hello ${customer.customerName}!, You have a valid Promocode!.</h3>
<table class="tftable" border="1"> <tr><th>Customer Details:</th></tr> <tr><td>Customer ID:</td><td>${customer.customerId} </td></tr> <tr><td>Customer Name:</td><td>${customer.customerName}</td></tr> <tr><td>Promo Code:</td><td>${customer.promoCode}</td></tr>
</table>
<a href="login.jsp">Enter PromoCode</a>
</body>
</html>
























Comments