What is a Servlet Container?
Join the DZone community and get the full member experience.Join For Free
in this post, i write a little bit about the basic ideas of web server , servlet container and its relation with jvm . i want to show that servlet container is nothing more than a java program.
1. what is a web server?
to know what is a servlet container, we need to know what is a web server first
a web server uses http protocol to transfer data. in a simple situation, a user type in a url (e.g. www.programcreek.com/static.html) in browser (a client), and get a web page to read. so what the server does is sending a web page to the client. the transformation is in http protocol which specifies the format of request and response message.
2. what is a servlet container?
as we see here, the user/client can only request static webpage from the server. this is not good enough, if the user wants to read the web page based on his input. the basic idea of servlet container is using java to dynamically generate the web page on the server side. so servlet container is essentially a part of a web server that interacts with the servlets.
servlet container is the container for servlets.
3. what is a servlet?
servlet is an interface defined in javax.servlet package. it declares three essential methods for the life cycle of a servlet – init(), service(), and destroy(). they are implemented by every servlet(defined in sdk or self-defined) and are invoked at specific times by the server.
- the init() method is invoked during initialization stage of the servlet life cycle. it is passed an object implementing the javax.servlet.servletconfig interface, which allows the servlet to access initialization parameters from the web application.
- the service() method is invoked upon each request after its initialization. each request is serviced in its own separate thread. the web container calls the service() method of the servlet for every request. the service() method determines the kind of request being made and dispatches it to an appropriate method to handle the request.
- the destroy() method is invoked when the servlet object should be destroyed. it releases the resources being held.
from the life cycle of a servlet object, we can see that servlet classes are loaded to container by class loader dynamically. each request is in its own thread, and a servlet object can serve multiple threads at the same time(thread not safe). when it is no longer being used, it should be garbage collected by jvm.
like any java program, the servlet runs within a jvm. to handle the complexity of http requests, the servlet container comes in. the servlet container is responsible for servlets’ creation, execution and destruction.
4. how servlet container and web server process a request?
- web server receives http request
- web server forwards the request to servlet container
- the servlet is dynamically retrieved and loaded into the address space of the container, if it is not in the container.
- the container invokes the init() method of the servlet for initialization(invoked once when the servlet is loaded first time)
- the container invokes the service() method of the servlet to process the http request, i.e., read data in the request and formulate a response. the servlet remains in the container’s address space and can process other http requests.
- web server return the dynamically generated results to the correct location
the six steps are marked on the following diagram:
5. the role of jvm
using servlets allows the jvm to handle each request within a separate java thread, and this is one of the key advantage of servlet container. each servlet is a java class with special elements responding to http requests. the main function of servlet contain is to forward requests to correct servlet for processing, and return the dynamically generated results to the correct location after the jvm has processed them. in most cases servlet container runs in a single jvm, but there are solutions when container need multiple jvms.
from program creek
Published at DZone with permission of Ryan Wang. See the original article here.
Opinions expressed by DZone contributors are their own.