Java Servlet @WebFilter Annotation Example
- Details
- Written by Nam Ha Minh
- Last Updated on 26 June 2019   |   Print Email
1. Syntax of @WebFilter annotation
@WebFilter( attribute1=value1, attribute2=value2, ... ) public class TheFilter implements javax.servlet.Filter { // implements Filter's methods: init(), doFilter() and destroy() }
2. Attributes of @WebFilter annotation
Name | Type | Required | Description |
filterName | String | Optional | Name of the filter. |
value or urlPatterns | String[] | Required | Specify one or more URL patterns to which the filter applies. Either of attribute can be used, but not both. |
dispatcherTypes | DispatcherType[] | Optional | Specify types of dispatcher to which the filter applies. Default is javax.servlet.DispatcherType.REQUEST |
servletNames | String[] | Optional | Specify names of servlets to which the filter applies. |
displayName | String | Optional | Display name of the filter. |
description | String | Optional | Description of the filter. |
asyncSupported | boolean | Optional | Specify whether the filter supports asynchronous operation mode. Default is false. |
initParams | WebInitParam[] | Optional | Specify one or more initialization parameters of the filter. Each parameter is specified by @WebInitParam annotation type. |
smallIcon | String | Optional | Specify name of the small icon of the filter. |
largeIcon | String | Optional | Specify name of the large icon of the filter. |
3. Some @WebFilter Examples
The following example registers a filter for the URL pattern /admin:@WebFilter("/admin") public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { } @Override public void destroy() { } }Apply a filter for all URLs:
@WebFilter("/*") public class MyFilter implements Filter { // implements Filter's methods here... }Register a filter for a specific servlet:
@WebFilter(servletNames = "MyOwnServlet") public class MyFilter implements Filter { // implements Filter's methods here... }
@WebFilter(servletNames = {"MyOwnServlet", "UploadServlet"}) public class MyFilter implements Filter { // implements Filter's methods here... }Specify initialization parameters for the filter:
@WebFilter( urlPatterns = "/uploadFilter", initParams = @WebInitParam(name = "fileTypes", value = "doc;xls;zip;txt;jpg;png;gif") ) public class UploadFilter implements Filter { // implements Filter's methods here... }Specify additional information for the filter:
@WebFilter( urlPatterns = "/admin/*", filterName = "AdminFilter", description = "Filter all admin URLs" ) public class MyFilter implements Filter { // implements Filter's methods here... }Specify dispatcher types:
@WebFilter( urlPatterns = "/admin", dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD} ) public class MyFilter implements Filter { // implements Filter's methods here... }That's how to use the @WebFilter annotation. I recommend you to read the book Head First Servlet and JSP to fully learn about Java servlet programming.
References:
Related Java Filter Tutorials:
- @WebServlet annotation examples
- How to Create Java Servlet Filter
- How to modify HTTP response using Java Filter
- How to implement authentication filter for Java web application
Related Java Servlet Annotations:
- @WebServlet annotation
- @WebListener annotation
- @WebInitParam annotation
- @HandlesTypes annotation
- @MultipartConfig annotation
- @ServletSecurity, @HttpMethodContraint and @HttpConstraint annotations
Other Java Servlet Tutorials:
- Java Servlet Quick Start for beginners (XML)
- Java Servlet for beginners (annotations)
- Java Servlet and JSP Hello World Tutorial with Eclipse, Maven and Apache Tomcat
- Handling HTML form data with Java Servlet
- Java File Download Servlet Example
Comments
@WebFilter("/forfewUrls")
@interface MySecurityFilter{
}
public HelloWorldContrller{
@MySecurityFilter
@RequestMapping("/hello")
public String getMessage(){
return "";
}
}