1) Create a MyFilter.java filter class in the src folder of the Web Application
2) Configure the filter in the web.xml deployment description in the WebContent/WEB-INF folder, as follows,
This filter will intercept requests to all URLs (of servlets, JSPs, etc.) in the Web Application
3) Test the filter by accessing any valid URL (servlet URL or JSP) of the Web Application, and check the server console log,
package info.icontraining.filters;
import java.io.IOException;
import javax.servlet.*;
public class MyFilter implements Filter {
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
public void destroy() {
System.out.println("Clean-up code in Filter");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("Filter processing request");
chain.doFilter(request, response);
System.out.println("Filter processing response");
}
}
2) Configure the filter in the web.xml deployment description in the WebContent/WEB-INF folder, as follows,
<filter>
<filter-name>myFilter</filter-name>
<filter-class>info.icontraining.filters.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
This filter will intercept requests to all URLs (of servlets, JSPs, etc.) in the Web Application
3) Test the filter by accessing any valid URL (servlet URL or JSP) of the Web Application, and check the server console log,
http://localhost:8080/WebAppName/anyValidURL
No comments:
Post a Comment