• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Servlet » Examples » Servlet using GenericServlet class Example
Next →
← Prev

Servlet using GenericServlet class Example

By Dinesh Thakur

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=”https://ecomputernotes.com: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.

https://ecomputernotes.com: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

You’ll also like:

  1. Can we place only parameterized constructor in our Servlet class?
  2. Write A C++ Program To Make A Call Class Constructor Or Not During Class Array Declaration.
  3. Write A C++ Program For Using Class Pointer And Class Array Together.
  4. What is the Servlet container raises the life cycle events in servlet program?
  5. What is Virtual Class and Friend Class
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Servlet Tutorials

Servlet Tutorials

  • Servlet - Home
  • Servlet - Types
  • Servlet - Advantages
  • Servlet - Container
  • Servlet - API
  • Servlet - Chaining
  • Servlet - Life Cycle
  • Servlet - Developement Way
  • Servlet - Servlet Vs CGI
  • Servlet - Server Side
  • Servlet - HttpServletRequest
  • Servlet - Cookies Advantages
  • Servlet - JDBC Architecture
  • Servlet - HttpServlet
  • Servlet - HttpServletResponse
  • Servlet - Web Technology
  • Servlet - Applet Communication
  • Servlet - Html Communication
  • Servlet - HTTP POST Request
  • Servlet - HTTP GET Request

Other Links

  • Servlet - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW