This article was last updated on : November 4th, 2016 at 3:34 am

Generic Servlet class definition, methods, example, pros and cons, its uses

In this article we are publishing a quick guide of Generic Servlet class. We will demonstrate Generic Servlet class definition, methods, its advantages, disadvantages and one standalone example using eclipse and apache tomcat server.

Defining Generic Servlet Class :

As per its name Generic, it is the basic implementation of a servlet.

Servlet’s API servlet classes and interfaces are packaged in two packages.

1. javax.servlet

2. javax.servlet.http

GenricServlet class belongs to javax.servlet package. It implements Servlet, ServletConfig and Serializable interfaces.

GenericServlet class is an abstract class. Except service() method it provides the implementation of all the methods of Servlet, ServletConfig interfaces. Not like HttpServlet, GenericServlet class can handle any type of request so it is protocol-independent.

Design Pattern used to implement GenericServlet Class

GenericServlet Class uses Adapter pattern for its implementation.

Generic Servlet class example

In this example we will submit an form. After submitting this application form we will display Name, Age, Job Title of a person on next page.

This example will demonstrate

1. How web container takes a simple form submission request from browser.
2. How it generates response in HTML formate and handover it to browser.

By looking into the example you can easily guess what is the role of javax.servlet.GenericServlet class? This example will show you uses of each and every useful GenericServlet methods.

Steps to do example

1. Create one applicationForm.html page.
2. Create one Generic Servlet “DisplayFormValuesServlet.java” to display form values
3. Deploy your application to tomcat and run it.

applicationForm.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="DisplayFormValuesServlet">
<table>
<tr>
<td>Name :</td>
<td><input name="name" type="text" /></td>
</tr>
<tr>
<td>Age :</td>
<td><input name="age" type="text" /></td>
</tr>
<tr>
<td>Name :</td>
<td><input name="emailId" type="text" /></td>
</tr>
<tr>
<td><input name="submit" type="submit" /></td>
</tr>
</table>
</form>
</body>
</html>

DisplayFormValuesServlet.java

package com.sitenol.genericservlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class DisplayFormValuesServlet extends GenericServlet {
private static final long serialVersionUID = 1L;
public DisplayFormValuesServlet() {
super();
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Name entered is : " + request.getParameter("name")
+ "<br>");
out.println("Age entered is : " + request.getParameter("age") + "<br>");
out.println("Email Id : " + request.getParameter("emailId") + "<br>");
}
}

Deploy your application to tomcat and run it

You can deploy and run the web application. Please type this url in the browser.
http://localhost:8081/SimpleGenericServlet/applicationForm.html

 Why using GenericServlet class is not safe?

When you will hit enter after entering url the above mentioned url. You can see the key value pairs in the request url like

?name=Tarun&age=29

Complete Url : http://localhost:8081/SimpleGenericServlet/DisplayFormValuesServlet name=Tarun&age=29&emailId=tarunkumrs%40gmail.com&submit=Submit+Query

It proves that it can not be a safe approach. The data sent to browser is readable.

Methods in Generic Servlet class

We preparing a good note on methods of Generic Servlet. We will include it here may be in 3 days. Please keep visiting this blog.

How can you convert a Java class into GenericServlet?

One tricky point after reading above points “A simple java class can be converted to a GenericServlet by inheriting
the GenericServlet class and providing the implementation of the service method. right!”

Please let us know your feedback after reading this quick guide. In case you need any help. please feel free to contact our tutorial team. We will include methods, pros and cons soon. Please keep visiting our blog.

Thanks.

Share this post:

Recent Posts

3 Comments

  1. Avatar for Tarun Singh
    Sai Krishna says:

    Where should i place html pages ? I have done everything but getting some error.! Pls tell me this so i can go futher.! Thanking you

    • Avatar for Tarun Singh
      Tarun Singh says:

      Hi Sai! Please see this url: “localhost:8081/SimpleGenericServlet/applicationForm.html”
      Here “SimpleGenericServlet” is your web application name and applicationForm.html is the html page. All the html pages and WEB-INF folder will be inside “SimpleGenericServlet” folder.
      Application folders will look like :
      SimpleGenericServlet/applicationForm.html
      SimpleGenericServlet/WEB-INF/classes (All the compiled java files will go here).

  2. Avatar for Tarun Singh
    Nani says:

    Excellent read. I just passed this onto a buddy who was doing a litlte research on that. He just bought me lunch since I found it for him! Thus let me rephrase: Thank you for lunch!

Leave a Comment