< Prev
Next >


In this article, we are going to understand how to use Wrappers in a web application. Wrappers are placed between the Servlet and the Servlet Container, due to which the interaction with Servlet is done using wrappers. There are two kinds of wrappers -
package decodejava;

import javax.servlet.*;

public class MyServlet1 extends GenericServlet

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
String name = request.getParameter("username");

PrintWriter out = response.getWriter();



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 filter class with our Servlet class -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""

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






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

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


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

After setting the classpath, you need to compile the filter and the Servlet class by entering the command at the folder where you've stored the Servlet class file.

javac -d WEB-INF/classes

javac -cp ./WEB-INF/classes/;C:/apache-tomcat-9.0.2/lib/servlet-api.jar -d WEB-INF/classes

javac -d WEB-INF/classes

Executing the Servlet

Please share this article -

< Prev
Next >
< ServletHttpSessionListener
ServletResponseWrapper >


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition