Open In App

Generic Servlet Class

Last Updated : 30 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

GenericServlet implements the Servlet interface and provides an implementation for all its method except the service() method hence it is abstract. GenericServlet class defines a protocol-independent(HTTP-less) servlet. However, while building a website or an online application, we may want to have HTTP protocol, in that case, we must extend HttpServlet instead of GenericServlet. Developing Servlet by extending GenericServlet is very easy because we have to provide implementation only for the service() method. GenericServlet class is in javax.servlet package (javax.servlet.GenericServlet).

service() method

The prototype of service( ) method is :

public void service (ServletRequest req, ServletResponse resp) throws ServletException,IOException

This method is automatically called by the server whenever a request for a GenericServlet arrives. The service() method accepts two parameters:

  1. A ServletRequestObject
  2. A SerlvetResponseObject

The ServletRequest object allows to read data provided by the client request and the ServletResponse object is used to send the response to the client

Servlet “Hello World” example by extending GenericServlet class

Java




import java.io.*;
import javax.servlet.*;
  
public class MyFirstServlet extends GenericServlet
  
{
    public void service(ServletRequest req,
                        ServletResponse resp)
        throws ServletException, IOException
    {
        resp.setContentType("text/html");
        PrintWriter pw = resp.getWriter();
        pw.println("<html>");
        pw.println("<head><title>My first Servlet</title></head>");
        pw.println("<body>");
        pw.println("<h2>Welcome To Servlet World!</h2>");
        pw.println("</body>");
        pw.println("</html>");
        pw.close();
    }
}


Compiling the Program

Before we can compile the program we need to set the servlet-api.jar file in our classpath. This is because the package javax.servlet is in this jar file. The file servlet-api.jar is available in the lib folder of Tomcat. So assuming Tomcat is in h:\ drive, our command to set classpath would be:

h:\Apache Software Foundation\Tomcat 8.0\lib\servlet-api.jar ;.;%classpath%

Now we can compile the code using the usual javac command

javac MyFirstServlet.java  

Doing this will generate the MyFirstServlet.class file which we will have to deploy in our web-server

Deploying the Servlet

  To deploy the servlet in our web server we need to do the following

  • Create an appropriate directory structure
  • Design the web.xml file
  • Place the .class file in your application’s classes sub-directory

Creating the Directory

For creating a web application we should follow the standard directory structure provided by Sun Microsystem which is server independent. According to this an application contain an application root folder with any name.

  1. Under the root folder, a subfolder is required with the name WEB-INF.
  2. Under WEB-INF two subfolders are required called classes and lib.
  3. All jar files placed inside lib folder.
  4. All .class files including servlet’s .class file are placed inside classes folder.
  5. All image, html, .js, jsp, etc files are placed inside the application root folder.
  6. Also under the WEB-INF folder, we need to place the web.xml file 

Creating the Directory

web.xml File

  • Java web applications use a deployment descriptor file to determine how URLs map to servlets.
  • This file is named web.xml, and resides in the app’s  WEB-INF/ directory and is a part of the servlet standard for web applications

Designing web.xml File

XML




<web-app>
  
    <servlet>
       <servlet-class>MyFirstServlet</servlet-class>
       <servlet-name>MyFirstServlet</servlet-name>
    </servlet>
  
    <servlet-mapping>
       <servlet-name>MyFirstServlet</servlet-name>
       <url-pattern>/MyFirstServlet</url-pattern>
    </servlet-mapping>
  
</web-app>


Configuring the Servlet

Once the directory structure is in place, then we can copy the MyFirstServlet.class file inside the classes folder of our application. Placing the .class file inside the classes folder is a compulsory requirement for our servlet.

Configuring The Servlet

Run the Application

After all the deployment has been done we need to test our servlet and for this, we need to do the following:

  • Start the Tomcat server by clicking on the Tomcat 8.5.file found inside the Tomcat’s bin folder.
  • Open the browser and type the following URL:

http://localhost:2021/sumit/MyFirstServlet

Output:

Output



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads