Advertisement



< Prev
Next >



Filter Initalization Parameters



In this tutorial, we are going to understand how to associate initialization parameters with a filter, which is itself associated with a web resource. The web resource could be a Servlet, JSP or a static html webpage.

As you know, we can create a Filter class by implementing the Filter interface. With this Filter class, we can associate one or multiple initialization parameters by specifying these initialization parameters in the deployment descriptor(web.xml) file.

After specifying the initialization parameters of a Filter class. When a user make a request to this web resource, the Filter class associated with this resource is executed and its initialization parameters could be read and used before and after the web resource requested by the user is executed, hence providing filtering to the user request and response.




Methods of Filter interface


In order to create a filter class we must implement the three methods of javax.servlet.Filter interface.

Methods Description
void doInit(FilterConfig config)
This method initializes the Filter.

void doFilter(ServletRequest request, ServletResponse, response, FilterChain chain)
This method is called by the web container when a client requests for a web resource which could be a Servlet or a JSP page.

void destroy()
This method destroys the Filter instance.






Creating a webpage which calls the Servlet


We are creating a webpage which asks the user to enter his name and click the submit button, which when clicked, will call a Servlet, but before this Servlet is executed, the filter associated with it will be executed and the initialization parameters associated with this filter(specified in the deployment descriptor file(web.xml) are displayed.

Webpage1.jsp
<html>

<head>
<title> Filters Demo </title>
</head>


<body>

<b> Please enter your name : ? </b>
<br/>
<br/>
<br/>

<form action ="MyServ">
Name : <input type = "text"  name = "name" />
<input type = "submit"  name = "submit" />
</form>

</body>
</html>





Creating Filter



MyServlet1.java
import java.io.*;
import javax.servlet.*;

public class MyServlet1 extends GenericServlet
{


public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("The requested Servlet is being executed.");
}
}




Advertisement




Directory Structure of Servlet files




The diagram above depicts how to arrange the Servlet files in a specific directory structure, as per Java Servlet Specification-






Creating the Deployment Descriptor file


As per the Java Servlet specifications, every web application based on Servlet must have a Deployment Descriptor file(an XML file) named web.xml. So, let's create one and this time, associate our two filter classes with our Servlet class -


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">

  <display-name>Welcome tomcat</display-name>
  <description>
     Welcome tomcat
  </description>

  
<filter>
 	<filter-name>Filter1</filter-name>
	<filter-class>MyFilter1</filter-class>
	<init-param>
		<param-name>Subject</param-name>
		<param-value>Computer Programming</param-value>
	</init-param>
	<init-param>
		<param-name>Topic</param-name>
		<param-value>Filter Initialization Parameters</param-value>
	</init-param>
	<init-param>
		<param-name>Year</param-name>
		<param-value>2018</param-value>
	</init-param>
</filter>



<filter-mapping>
	<filter-name>Filter1</filter-name>
	<url-pattern>/MyServ</url-pattern>
</filter-mapping>




<servlet>
 	<servlet-name>Servlet</servlet-name>
	<servlet-class>MyServlet1</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>Servlet</servlet-name>
	<url-pattern>/MyServ</url-pattern>
</servlet-mapping>

</web-app>


In deployment descriptor file, The <filter> has two child tags <filter-name> and <filter-class>.




We have also specified three initialization parameters specific to our Filter using <init-param> tag. This tag has two child tags <param-name> and <param-value> :




<servlet> has two child tags <servlet-name> and <servlet-class> :








Note







Setting the classpath


Much of the support for developing the web applications based on the Java Servlet technology does not come with the core Java. Hence, in order to compile the Servlet programs, we have to set the classpath to a jar file named servlet-api.jar.

This jar file provides all the classes that are required for the Servlet programming and it comes within the lib Folder of Tomcat installation folder.

For example, in our case we have installed Tomcat Web Server within the C: Drive, hence the path to our lib folder containing the servlet-api.jar is - C:\apache-tomcat-9.0.2\lib

There are two ways to set the classpath -




Compiling the Servlet class







Executing the Servlet







Please share this article -




< Prev
Next >
< Filters Chain
User Authentication Filter >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement