Servlets are programs that run on a Web or Application server and act as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server.

A servlet is simply a class which responds to a particular type of network request – most commonly an HTTP request.

Basically, servlets are usually used to implement web applications – but there are also various frameworks which operate on top of servlets (e.g. Struts) to give a higher-level abstraction than the “here’s an HTTP request, write to this HTTP response” level which servlets provide.

Servlets run in a servlet container which handles the networking side (e.g. parsing an HTTP request, connection handling etc). One of the best-known open source servlet containers is Tomcat.

The container will take care of things like wrapping the whole thing in a HTTP response object and send it over to the client (say a browser).

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 and doPost, for handling HTTP-specific services.


Lifecycle

Servlets are now java classes whose lifecycle will be maintained by the container but their reaction to incoming HTTP requests will be decided by you.

You do that by writing what-you-want-to-do in the pre-defined methods like init(), doGet(), doPost() etc.

When a Servlet is requested for the first time or when the web app starts up, the servlet container will create an instance of it and keep it in memory during the web app’s lifetime.

The same instance will be reused for every incoming request whose URL matches the servlet’s URL pattern. You can access the requested data by HttpServletRequest and handle the response by HttpServletResponse. Both objects are available as method arguments inside of any of the overridden methods of HttpServlet, such as doGet() to preprocess a request and doPost() to post-process a request. See also How do servlets work? Instantiation, sessions, shared variables and multithreading.

Categories: Java

2 Comments

Dawinder Singh · December 3, 2016 at 3:49 am

Good job

Dawinder Singh · December 3, 2016 at 3:50 am

This comment has been removed by the author.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Apache Ant

Apache Ant basic tasks

In the previous post, we have learned what is Apache Ant, its features and an example to create simple build file. This post will guide you other basic ant tasks that are frequently used to create Read more…

Apache Ant

Apache Ant : Java Build Tool

Apache Ant is a Java library and command-line tool to automate the repetitive tasks and helps to build software. Ant is flexible and does not impose coding conventions. It supplies a number of built-in tasks allowing Read more…

Java

Advantages of spring interceptor over servlet filter

Even filters can do the same things that interceptors can but interceptors can do more easily and more elegantly. Interceptors are more coupled to the request/response cycle than filters. Can inject other beans in the Read more…