Home » Servlets » Servlet Overview

Servlet Overview

What are Servlets ?

A servlet is a Java program that runs on a Web server. It is similar to an applet, but is processed on the server rather than a client's machine. Servlets are often run when the user clicks a link, submits a form, or performs another type of action on a website.

Using Servlets, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically.

Advantage of Servlet :

  • Less response time because each request run in a seperate thread.
  • Servlet are scalable.
  • Servlet are robust and object oriented.
  • Servlet are platform independent.

Servlet Architecture :

The javax.servlet package provides interfaces and classes for writing servlets. The architecture of the package is described below.

Servlet Architecture

The Servlet Interface :

The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more commonly, by extending a class that implements it such as HttpServlet.

  • destroy()

    Cleans up whatever resources are being held and makes sure that any persistent state is synchronized with the servlet's current in-memory state.

  • getServletConfig()

    Returns a servlet config object, which contains any initialization parameters and startup configuration for this servlet.

  • getServletInfo()

    Returns a string containing information about the servlet, such as its author, version, and copyright.

  • init(ServletConfig)

    Initializes the servlet. Run once before any requests can be serviced.

  • service(ServletRequest, ServletResponse)

    Carries out a single request from the client.

Client Interaction :

When a servlet accepts a service call from a client, it receives two objects, ServletRequest and ServletResponse. The ServletRequest class encapsulates the communication from the client to the server, while the ServletResponse class encapsulates the communication from the servlet back to the client.

The ServletRequest interface allows the servlet access to information such as the names of the parameters passed in by the client, the protocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it. It also provides the servlet with access to the input stream, ServletInputStream, through which the servlet gets data from clients that are using application protocols such as the HTTP POST and PUT methods. Subclasses of ServletRequest allow the servlet to retrieve more protocol-specific data. For example, HttpServletRequest contains methods for accessing HTTP-specific header information.

The ServletResponse interface gives the servlet methods for replying to the client. It allows the servlet to set the content length and mime type of the reply, and provides an output stream, ServletOutputStream, and a Writer through which the servlet can send the reply data. Subclasses of ServletResponse give the servlet more protocol-specific capabilities. For example, HttpServletResponse contains methods that allow the servlet to manipulate HTTP-specific header information.

The classes and interfaces described above make up a basic Servlet. HTTP servlets have some additional objects that provide session-tracking capabilities. The servlet writer can use these APIs to maintain state between the servlet and the client that persists across multiple connections during some time period.

Servlets Tasks :

Servlets perform the following major tasks.

  • Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program.
  • Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types and compression schemes the browser understands, and so forth.
  • Process the data and generate the results. This process may require talking to a database, executing an RMI or CORBA call, invoking a Web service, or computing the response directly.
  • Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent in a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.
  • Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or other clients what type of document is being returned (e.g., HTML), setting cookies and caching parameters, and other such tasks.

In the next tutorial we will discuss about important concepts of Servlet(Server, container, http request, get and post, content type etc.)

Next Article

comments powered by Disqus