GenericServlet is an abstract class which implements Servlet and ServletConfig interface.
GenericServlet class can also be used to create a Servlet. GenericServlet class is part of the Servlet API and the full path to import this class is
javax.servlet.GenericServlet.
Some ways to create a Servlet
We could create a Servlet in various ways -
By implementing the Servlet interface.
By extendingGenericServlet abstract class.
By extendingHttpServlet class.
You've already seen how to create a Servlet by implementing the Servlet interace. Now
let's see how to create a Servlet by extending GenericServlet abstact class.
When to use GenericServlet to create a Servlet?
We could create a Servlet by implementing Servlet interface, even though there is a
slight bit of more work with it i.e.. when creating a Servlet by implementing a Servlet class,
we must implement all the methods of Servlet interface, even when these methods might not be of any use to our
particular Servlet program, these methods are -
init(ServletConfig)
service(ServletRequest, ServletResponse)
getServletConfig()
getServletInfo()
destroy()
We know, GenericServlet is an abstract class which implements Servlet and ServletConfig
interface. Hence, GenericServlet class has implemented all the methods of Servlet
interface except service(ServletRequest, ServletResponse) method of Servlet interface.
This is why, when creating a Servlet by extending GenericServlet, we only have to implement the
service(ServletRequest, ServletResponse) method of Servlet interface and makes the servlet programming easier.
This is why GenericServlet class is sometimes preferred over Servlet class when creating
a Servlet.
GenericServlet methods
GenericServlet abstract class implementsServlet and ServletConfig interface, hence it has implemented all the methods available in both Servlet
and ServletConfig interface except the service(ServletRequest, ServletResponse) of Servlet interface. Hence, this method is left abstract and we must implement it when
creating a Servlet by extending the GenericServlet class.
Let's take a look at the methods part of javax.servlet.GenericServlet class.
This method gets the Servlet associated information.
ServletConfiggetServletConfig()
This method gets the ServletConfig object.
StringgetInitParameter(String str)
This method returns the value of a parameter named str
EnumerationgetInitParameterNames()
This method returns all the parameter names associated with the Servlet.
ServletContextgetServletContext()
This method returns an object of ServletContext.
StringgetServletName()
This method returns the name of this Servlet object.
Advertisement
Creating the Servlet class by extending GenericServlet
We are creating a Servlet extending the GenericServlet abstract class. GenericServlet class implements Servlet and ServletConfig interface.
A Servlet class is just a regular Java class which ends with a .java extension, hence we have named this file MyServlet1.java.
MyServlet1.java
import java.io.*;
import javax.servlet.*;
public class MyServlet1 extends GenericServlet
{
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("Hello there!");
out.println("Hello from the Servlet made by extending GenericServlet abstract class.");
System.out.println("Putting the Servlet in service");
}
}
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-
Project Folder - We have created a new folder for this project named GenericServ within
the Tomcat webapps folder, which contains our Servlet class(.java) file.
WEB-INF - This folder should contain the deployment descriptor file(web.xml) of your Java Servlet program.
classes - This folder should contain the compiled(.class) form of your Java Servlet class.
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 -
In deployment descriptor file, <servlet> has two child tags <servlet-name> and <servlet-class> :
<servlet-name> tag is used to specify a unique name for our Servlet class, we have given it a unique name MyGenericServlet.
<servlet-class> tag is used to specify the full qualified name of the Servlet class and in our example our Servlet class is named MyServlet1.
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 -
You could set the classpath by entering this command at the Command Prompt, but this only temporarily sets the classpath to the path of servlet-api.jar file. If you restart your system, you will have to set the classpath again.
set classpath=C:\apache-tomcat-9.0.2\lib\servlet-api.jar
In order to set the classpath permanently, you need to set your classpath system variables. A simple tutorial on how to permanently set the classpath system variables, you may read
How to permanently set the classpath system variable
Compiling the Servlet class
After setting the classpath, you need to compile the Servlet class by entering the command at the folder where you've stored the Servlet class file.
javac -d WEB-INF/classes MyServlet1.java
Executing the Servlet
Next, we need to enter the URL of this Servlet in the address bar of our web browser.
2) Our the Servlet file is stored in the a new folder created by us named GenericServEx (within webapps folder), hence
we need to give the full path to the file(including
the project folder name which contains the Servlet class file)
For example, if we stored MyServlet1.java in the folder GenericServ then the URL to execute this file would be
http://localhost:8080/GenericServEx/MyServlet
Where, MyServlet is the specified in the url-pattern tag within web.xml file.
GenericServ is the name of the folder within webapps folder, in which you've stored the Servlet class.