Filter
It is a program which executes on the time of responding to a request to a specific URL. The filter resides in a webserver. Whenever the user makes the request
to the associated URL then the request goes to the filter. The filter can modify the request or perform any task before passing the request to the destination.
Creation process of a filter
- Create a class by inheriting
javax.servlet.filter interface.
- Override the methods of the filter interface. The
methods are as follows:
public void init()
public void doFilter(servletRequest req,servletResponse res,filterchain
chain)
public void destroy()
- Perform the required task inside doFilter()
- Transfer request and response to the
destination by using doFilter() of filterchain.
Storing, compilation and deployment of
filter
- Store the filter file inside classes'
folder of the context.
- Compile the filter file by using
servlet-api.jar in classpath.
- Deploy the filter by using the required
tags in web.xml file. The tags are as follows:
- <filter>
- <filter-name>This contains mapping name for the filter</filter-name>
- <filter-class> This contains class name for the filter </filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name> This contains mapping name for the filter </filter-name>
- <url-pattern>/ This contains the url for whom the filter will execute </url-pattern>
- </filter-mapping>
Example
access.html
- <html>
- <body>
- <h1><a href="./genserv">Visit Genserv</a></h1>
- </body>
- </html>
genserv.java file -
- import java.io.*;
- import javax.servlet.*;
- public class genserv extends GenericServlet
- {
- public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException
- {
- PrintWriter out = res.getWriter();
- res.setContentType("text/html");
- out.println("<html><body bgcolor='blue'>");
- out.println("<h1>First Servlet</h1>");
- out.println("</body></html>");
- }
- }
Filtercounter.java file
-
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.io.*;
-
- public class Filtercounter implements Filter
- {
- public void init(FilterConfig con) {}
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
- {
- try
- {
- FileWriter fw = new FileWriter("E:/filter/fil.txt", true);
-
- fw.write("page accessed");
- fw.close();
- chain.doFilter(request, response);
- }
- catch (Throwable t)
- {
- System.out.println(t);
- }
- }
- public void destroy() {}
- }
Web.xml settings
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version="2.5">
-
- <filter>
- <filter-name>Filtercounter</filter-name>
- <filter-class>Filtercounter</filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>Filtercounter</filter-name>
- <url-pattern>/genserv</url-pattern>
- </filter-mapping>
-
- <servlet>
- <servlet-name>genserv</servlet-name>
- <servlet-class>genserv</servlet-class>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>genserv</servlet-name>
- <url-pattern>/genserv</url-pattern>
- </servlet-mapping>
-
- </web-app>
Compile both the files as below
javac -cp servlet-api.jar genserv.java (for tomcat 6.0)
javac -cp servlet-api.jar Filtercounter.java (for tomcat 6.0)
Output
Run the tomcat then write the below line in the URL
Here the test is the Context path, which we mentioned in the server.xml file, which
is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf)
directory.
http://localhost:8081/test/
After giving the URL a set a listing will come, here only one appears
As access.html, click it
Note:- You will notice in E:/filter/fil.txt file how many times the user
will visit this Url(http://localhost:8081/test/genserv) that the number of times
page access will be written to that file.