by Dinesh Thakur Category: Basic Servlet Examples

The GenericServlet class implements the Servlet and ServletConfig interfaces. Since service () method is declared as an abstract method in GenericServlet class, it is an abstract class. The class extending this class must implement the service () method. It is used to create servlets which are protocol independent.

To understand the creation of servlets using the GenericServlet class, lets us consider a simple program given here. For this, two files will be required; one .java file in which servlet is defined and another. html file containing code for web page.

A program to create servlet using GenericServlet class

import java.io.*;

import java.util.*;

import javax.servlet.*;

public class ServletExample extends GenericServlet

{

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

   {

      PrintWriter out = res.getWriter();

      String login= req.getParameter("loginid");

      String password= req.getParameter("password");

      out.print("You have successfully login :");

      out.println("Your login ID is: "+login);

      out.println("Your password is: " +password);

      out.close();

   }

}

The explanation of this program is as follow.

1. Firstly, the javax.servlet package is imported which contains interfaces and classes to create servlet.

2. Next, the class named ServletExample is defined which extends the GenericServlet class. This class provides methods for initializing ,invoking and destroying an instance of servlet. The init () and destroy () methods are inherited from the GenericServlet class. The ServletExample class must override the service () method as it is declared as abstract in GenericServlet class.

3. The two parameters passed to the service () method are req and res, the objects of ServletRequest and ServletResponse interfaces respectively. The req object allows to read data provided in the client request and the res object is used to develop response for the client request.

4. The getWriter () method returns PrintWriter stream. This stream is used to send data as part of the response. The println () method can be used to write HTML code forming part of the response.

5. The getParameter () method of ServletRequest interface is used to retrieve data sent To the server by the client .For example, the expression req. getParameter ("loginid") is used the retrieve value stored in parameter loginid provided as a part of the request.

This code for servlet will be executed as a response to some action performed on the webpage. The HTML code(.html) for the webpage is as follows.

<HTML>

   <BODY>

      <CENTER>

         <FORM NAME="Form1" METHOD="post" ACTION="http://localhost:8080/ServletExample">

              <B>Login ID</B> <INPUT TYPE="text" NAME="loginid" SIZE="30">

              <P>

              <B>Password</B> <INPUT TYPE="password" NAME="password" SIZE="30">

             </P>

             <P>

                  <INPUT TYPE=submit VALUE="Submit">

            </P>

   </BODY>

</HTML>

This HTML code creates a web page containing a form. This form includes two text fields, one submit button and one reset button. The action attribute of< FORM>tag is used to specify the URL for identifying the servlet to be processed for the POST request. Save this file as ServletExample.html in root directory.

To make servlet working, compile ServletExample.java file and store its .class file in the appropriate directory. Add its name in web.xml file using the following statements in the section which defines the servlets.

<servlet>

<servlet-name>ServletExample</servlet-name>

<servlet-class>ServletExample</servlet-class>

</servlet>

Also, add the following statements in the section which defines the section mappings in web.xml.

<servlet-mapping>

<servlet-name>ServletExample</servlet-name>

<url-pattem>ServletExample</url-pattern>

</servlet-mapping>

Start the Tomcat so that it is in the running mode in the background when web page is being processed. Open the Internet Explorer and enter the following URL in the address bar.

http://localhost:8080/ServletExample.html

      Web Page Displaying the Form

Enter the required data and press the submit button. The browser will display the response generated dynamically by the corresponding servlet.

               Output Generated by Servlet





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.