Pr.Pg Next Pg

What are Servlets?

  • A Servlet is a Java programming language class used to extend the capabilities of a server. Although servlets can respond to any types of requests, they are commonly used to extend the applications hosted by web servers.

  • Servlets are modules of Java code that run in a server application hence the name "Servlets", similar to "Applets" on the client side to answer client requests.

  • Servlets are not tied to a specific client-server protocol but they are most commonly used with HTTP and the word "Servlet" is often used in the meaning of "HTTP Servlet".

  • The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets.

  • All servlets must implement the Servlet interface, which defines life-cycle methods.

  • When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet anddoPost, for handling HTTP-specific services.

 

Different between Applets and Servlets

 

Sr No

Applets

Servlets

1

A program written in Java that can be embedded in a HTML page is called an applet.

A Java program that is used to improve/ extend the functionalities of a server is called a servlet.

2

When applet is running it takes the resource of client machine.

Servlets are server side component which are processed on server side.

3

Applets can use a user interface classes like AWT and Swing.

Servlets do not have user interface

4

Applet life cycle methods consist of: init(), stop(), paint(), start(), destroy()

Servlet life cycle methods consist of: init(), service() and destroy().

5

Applets are very heavy to handle as compare to Servlets.

Servlets are light weight as compare to Applets.

6

Applets are use for developing static web pages.

Servlets are use for developing dynamic web pages

 

Typical uses for HTTP Servlets include

  • Processing and/or storing data submitted by an HTML form.

  • Providing dynamic content, e.g. returning the results of a database query to the client.

  • Managing state information on top of the stateless HTTP, e.g. for an online shopping cart system which manages shopping carts for many concurrent customers and maps every request to the right customer.

Pr.Pg border                                              Next Pg