2.1 SERVLETS INTRODUCTION
Servlets are the objects (Java Objects) whose responsibility is to handle the client requests. Servlets are also known as container managed objects because all the servlets life cycles are managed by a container called as Servlet Containers.
Servlets are the specification provided by Oracle(Sun Micro Systems) in the form of interfaces and all server vendors implements the Servlet interface. Before the servlets are invented there was another specification called as CGI(Common Gateway Interface) which were used to create a web applications.CGI has several shortcomings and to overcome that Servlet was designed.
To make servlet picture clear, let’s look at the below figure.
- Web Browser is a client which sends request to servlets. Corresponding Servlet invokes the business logic.
- Servlets responds back with the response to the client.
- Servlet container contains multiple servlets .It is the responsibility of Servlet Container to delegate the request to a particular Servlet.
2.2 SERVLET CONTAINER
Servlet container (also known as Servlet engine ) and is a part of any web server or Application Server and runs within a web or application server and interacts with Servlets. Some of the free or open source web servers are tomcat, Jetty, Glassfish etc.
There are specifications for a container , that needs to be fulfilled to become a servlet container like “it must respond to all HTTP requests”.
As we discussed earlier, container manages the life cycle of each and every servlet starting instantiation to destroy. We will discuss the life cycle of servlet in detail in upcoming chapters.
All web applications are deployed under web server and a servlet container and all servlets are a part of specific web application.
2.3 WHERE DOES SERVLET RESIDE ACTUALLY (COMPLETE PICTURE) -
A web application is an application accessible over web. A web application is composed of web components like Servlet, JSP, Filter etc. and other components such as HTML. The web components typically execute in Web Server and respond to HTTP request.
Let’s look at the below figure and its description. Below figure is the complete picture of all components in relation.
Web Server & Servlet Container
2.4 ILLUSTRATION /FLOW
- There are two clients (Client1 and Client2).
- One Web Server with Servlet Container
- There are two web application with name “Web Application 1” and “Web Application 2”
- Web application 1 consists of two servlets (Servlet 1 and Servlet 2)
- Web application 2 consists of two servlets (Servlet 3 and Servlet 4)
Any client (client 1) opens up a web browser and sends the request to web server by hitting the URL of web application like “http//localhost:8080/WebApplicaion1/Servlet1”. This URL says that server location is localhost (which means client and server are running on same machine) and web server is running in port 8080.
Web server scans the request and verifies that request is for Servlet1 of WebApplcation1. So servlet container instantiates the servlet1 if not already instantiated, creates a new thread and delegates the client request to the servlet to handle this request in that thread.
Now servlet can have any custom logic or it may invoke another java classes to execute some business logic. Once the processing is done , servlet sends back the HTTP response to the client.
Similarly another client can invoke another web application or the same web application which is invoked by client 1.
2.5 ADVANTAGES OF SERVLTS OVER CGI
- Servlets are platform independent. There are two reason for Servlets portability.
- As we all know Java is platform independent language and Servlets are written in Java which means servlets can run on any Operation System like Windows, Unix, Mac etc.
- Servlets are just specification and all vendor servers implement the interface so custom code written following specification can be ported easily from one server to another like Tomcat, JBoss etc.
- Servlets provides a much better performance as compared to traditional Common Gateway Interface scripts in terms of both memory and processing time.
As Servlets are web based which means any number of concurrent requests can come for a particular servlet. Servlet container handles this situation very well which results much better performance.
- With traditional CGI script , for each new HTTP request a separate process is created. Creating a process is very expensive operation. In case of servlets, all requests are handled in a separate thread (not separate process).
- Only one instance of a particular servlet is created and all the requests to that servlet are executed in a separate thread. This saves a lot of memory at run time as only one instance is created. This is very important concept so be clear that always one and only one instance of a particular servlets is created. We will discuss this more in Servlet Life Cycle.
- Servlets have an access to all other java classes and functionality like RMI, sockets.
- As Servlet’s life cycle is managed by Servlet container, they are more robust as developers need not to worry about most complex areas like garbage collections , memory management, memory leaks and security.
2.6 WHAT CAN A SERVLET DO?
- Servlets can be used to execute complex business logic, to interact with third party or any legacy systems or can make a database calls.
- Servlet can even create a web page by writing complete output to the response.
- Servlet can grab the user inputs and can process the request or user inputs.
2.7 NUMBER OF SERVLET INSTANCES -
Typically one and only one Servlet instance is created by a servlet container and for each HTTP request, container spawns a separate thread which handles the request.
This means multiple requests can be processed simultaneously in a separate thread and hence provides memory and performance gain.
2.8 CAN YOU THINK OF ANY PROBLEM WITH THIS MODEL?
If there is only one instance created and all requests are served in a separate thread which means all threads will share the member variables (instance variable). If servlets just performs read only operations on instance variable than we are good but if servlet writes or updates the value of an instance variable then we may end up with unpredictable behaviour.
To address this problem, there are options like
- Access the instance variable in synchronized blocks.
- Servlet specification provides a concept / feature of Single Thread Model. Single Thread Model is a marker interface and Implementing single thread model makes sure that only one thread executes at a time. This is a serious disadvantage as web multiple requests need to wait till one request completes.
Single Thread Model is deprecated now as its functionality can be achieved using synchronized blocks.