Servlet Tutorial

Servlet Introduction

A distributed application is one which always runs in the context of browser or www. The result of distributed application is always sharable across the globe. To develop distributed application one must follow the following:

Client-Server architecture:

  1. 2-tier architecture (Client program, Database program).
  2. 3-tier or MVC (Model [Database] View [JSP] Controller [Servlets]) architecture (Client program, Server program, Database program).
  3. n-tier architecture (Client program, Firewall program, Server program, Database program).

To exchange the data between client and server we use a protocol caller http which is a part of TCP/IP.

  1. A client is the program which always makes a request to get the service from server.
  2. A server is the program which always receives the request, process the request and gives response to 'n' number of clients concurrently.

A server is the third party software developed by third party vendors according to SUN micro systems specification. All servers in the industry are developed in java language only. The basic purpose of using server is that to get concurrent access to a server side program.

According to industry scenario, we have two types of servers; they are web server and application server.

A web server is one which always supports http protocol only.Any protocol can be supported.
Web server does not contain enough security to prevent unauthorized users.An application server always provides 100% security to the server side program.
Web server is not able to provide enough services to develop effective server side program.An application server provides effective services to develop server side program.
For examples Tomcat server, web logic server, etc.For examples web logic server, web sphere server, pramathi server, etc.

Note: Web logic server acts as both web server and as well as application server.

In the initial days of server side programming there is a concept called CGI and this was implemented in the languages called C and PERL. Because of this approach CGI has the following disadvantages.

  1. latform dependency.
  2. Not enough security is provided.
  3. Having lack of performance. Since, for each and every request a new and separate process is creating (for example, if we make hundreds of requests, in the server side hundreds of new and separate processes will be created)

To avoid the above problems SUN micro system has released a technology called Servlets.

A servlet is a simple platform independent, architectural neutral server independent java program which extends the functionality of either web server or application server by running in the context of www.

Advantages of SERVLETS over CGI:

  1. Servlets are always platform independent.
  2. Servlets provides 100% security.
  3. Irrespective of number of requests, a single process will be created at server side. Hence, Servlets are known as single instance multiple thread technology.

Servlets is the standard specification released by SUN micro systems and it is implemented by various server vendors such as BEA corporation (Web logic server), Apache Jakarta (Tomcat server).

In order to run any servlet one must have either application server or web server. In order to deal with servlet programming we must import the following packages:


Servlet Hierarchy:

  1. In the above hierarchy chart Servlet is an interface which contains three life cycle methods without definition.
  2. GenericServlet is an abstract class which implements Servlet interface for defining life cycle methods i.e., life cycle methods are defined in GenericServlet with null body.
  3. Using GenericServlet class we can develop protocol independent applications.
  4. HttpServlet is also an abstract class which extends GenericServlet and by using this class we can develop protocol dependent applications.
  5. To develop our own servlet we must choose a class that must extends either GenericServlet or HttpServlet.