JSP
Maven Tutorial for Beginners: What is, Architecture, Project Framework
What is Maven? Maven is an automation and management tool developed by Apache Software Foundation. It...
Filters are defined in web.xml, and they are a map to servlet or JSP.When JSP container starts with the web application, it creates the instance of each filter that have been declared in the deployment descriptor.
Following are the filter methods:
This is called everytime when a request/response is passed from every client when it is requested from a resource.
This is to indicate that filter is placed into service
This to indicate the filter has been taken out from service.
In this example, we have created filter and mapped in web.xml
Gurufilter.java
package demotest; import java.io.IOException; import java.util.Date; 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; public class GuruFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, Filterchain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest req = (HttpServletRequest) request; String ipAddress = req.getRemoteAddr(); System.out.println("IP Address "+ipAddress + ", Time is" + new Date().toString()); // pass the request along the filter chain chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { String guruparam = fConfig.getInitParameter("guru-param"); //Print the init parameter System.out.println("Test Param: " + guruparam); } }
Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name> test</display-name> <filter> <description> </description> <display-name> GuruFilter</display-name> <filter-name>GuruFilter</filter-name> <filter-class>demotest.GuruFilter</filter-class> <init-param> <param-name>guru-param</param-name> <param-value>This is guru paramter</param-value> </init-param> </filter> <filter-mapping> <filter-name>GuruFilter</filter-name> <url-pattern>/GuruFilter</url-pattern> </filter-mapping>
Explanation of the code:
Gurufilter.java
Code Line 17-32: Here we are using "doFilter" method where we are getting request object(in our example the request object is req(HttpServletRequest object)) and get the remote address of the client and printing on the console and also printing date and time on the console.
Code Line 33-37: Here we are using init method where we are taking the init parameter and printing init parameter in the console.
Web.xml
Code Line 10-11 – Mapping the GuruFilter with the class name GuruFilter.java where we have filter-name as GuruFilter and filter class which is directory path of GuruFilter class
Code Line 13-15 – Mapping the init parameter named guru-param and getting the value of it which is placed under filter tag so this init-param has been defined for gurufilter
Output:
When you execute the above code, you get the following output:
What is Maven? Maven is an automation and management tool developed by Apache Software Foundation. It...
JSP actions which use constructs in XML syntax to control the behavior of the servlet engine. We...
Download PDF 1) Explain what is Maven? How does it work? Maven is a project management tool. It...
What is Expression Language (EL)? Expression Language (EL) is mechanism that simplifies the...
What are JSP Directives? JSP directives are the messages to JSP container. They provide global...
In this tutorial, we will be learning the basic tags of JSP and how to add comments into JSP....