DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. What is a Servlet Container?

What is a Servlet Container?

Ryan Wang user avatar by
Ryan Wang
·
May. 01, 13 · Interview
Like (32)
Save
Tweet
Share
158.58K Views

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

web server

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.

web server & servlet container

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.

  1. 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.
  2. 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.
  3. 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?

  1. web server receives http request
  2. web server forwards the request to servlet container
  3. the servlet is dynamically retrieved and loaded into the address space of the container, if it is not in the container.
  4. the container invokes the init() method of the servlet for initialization(invoked once when the servlet is loaded first time)
  5. 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.
  6. web server return the dynamically generated results to the correct location

the six steps are marked on the following diagram:

servlet container - life cycle

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

Web container Web server Requests

Published at DZone with permission of Ryan Wang. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Rust vs Go: Which Is Better?
  • mTLS Everywere
  • Specification by Example Is Not a Test Framework
  • OpenVPN With Radius and Multi-Factor Authentication

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: