Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

10/12/23, 10:45 PM Servlet Architecture - GeeksforGeeks

Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multithreading Java Exception Handling Java Programs Java Project Java C

Join Geeks Premier League


2023 and Earn Big!! Servlet Architecture
Write an Interview Experience Read Discuss Practice

Servlets are grouped under the Advanced Java tree that are used to
Share Your Campus
Experience create dynamic web applications. Servlets are robust in nature,
well scalable and are primarily used in developing server-side
Life Cycle of a Servlet applications. If we go a little back in time, we would be able to
witness that before the introduction of servlets, CGI (Common
The HttpSession Interface in
Servlet Gateway Interface) were used. Amid several indigenous tasks that
a servlet is capable of doing, dynamically performing client
Servlet Collaboration In Java requests and responses are most common. Other tasks that a
Using RequestDispatcher and
HttpServletResponse servlet can do effectively are:

Can easily manage/control the application flow.


ServletConfig in Servlet
Suitable to implement business logic.
Attributes in Servlets | Java Can effectively balance the load at the server-side.
Easily generate dynamic web content.
How to Upload Multiple Files
using Java Servlet? Talking about the types of servlets, there are primarily two types,
namely:
Hidden Form Field using
Annotation
We use| Java Servlet
cookies to ensure you have the 1.
bestGeneric Servletson our website. By using our site, you acknowledge that you have read and
browsing experience
Got It !
understood our Cookie Policy & Privacy Policy
2. HTTP Servlets
Introduction to Java Servlets
Skip to content
10/12/23, 10:45 PM Servlet Architecture - GeeksforGeeks

There are three potential ways in which we can employ to create a


servlet:

1. Implementing Servlet Interface


2. Extending Generic Servlet
3. Extending HTTP Servlet

Components of Servlet Architecture


Below is the high level architecture diagram of servlet. Let’s see in
brief, how does each component add to the working of a servlet.

Check out our Master Java Programming - Complete Beginner


to Advanced course and boost your knowledge 🚀

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
understood our Cookie Policy & Privacy Policy

Skip to content
10/12/23, 10:45 PM Servlet Architecture - GeeksforGeeks

Servlet Architecture

1. Client

The client shown in the architecture above is primarily working as a


medium who is sending out HTTP requests over to the web server
and again processing the response it gets back from the server. As
we can see in the diagram, our client here is the web browser.

2. Web Server

Primary job of a web server is to process the requests and


responses that a user sends over time and maintain how a web
user would be able to access the files that has been hosted over
the server. The server we are talking about here is a software
which manages access to a centralized resource or service in a
network.. There are precisely two types of webservers:

1. Static web server


2. Dynamic web server
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
understood our Cookie Policy & Privacy Policy

3. Web Container
Skip to content
10/12/23, 10:45 PM Servlet Architecture - GeeksforGeeks

Web container is another typical component in servlet architecture


which is responsible for communicating with the servlets. Two
prime tasks of a web container are:

Managing the servlet lifecycle


URL mapping

Web container sits at the server-side managing and handling all


the requests that are coming in either from the servlets or from
some JSP pages or potentially any other file system.

How does a Servlet Request flow?


Every servlet should override the following 3 methods namely:

1. init()
2. service()
3. destroy()

These methods are used to process the request from the user.

Following are the steps in which a request flows through a servlet


which can be observed in the architecture diagram:

The client sends over a request.


The request is accepted by the web server and forwarded to the
web container.
In order to obtain the servlet’s address, the web container traces
web.xml file corresponding to the request URL pattern.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
By understood
the timeour above
Cookie process takes
Policy & Privacy place, the servlet should have
Policy
been instantiated and initialized. If the servlet has not been
Skip to content
10/12/23, 10:45 PM Servlet Architecture - GeeksforGeeks

instantiated and initialized, init() method is invoked to serve the


purpose.
By passing ServletRequest and Response object, public
service() method is called by the container.
In the next step, the ServletRequest and ServletResponse
objects are type-casted to HttpServletRequest and
HttpServletResponse objects by the public service() method.
Now protected service() method is called by the public service()
method.
The protected service() method dispatches the request to the
correct handler method based on the type of request.
When servlet container shuts down, it unloads all the servlets
and calls destroy() method for each initialized servlets.

Advantages
Prime functionality of a servlet is that they are independent of
server configuration and they are pretty much compatible with
any of the web servers
Servlets are also protocol-independent supporting FTP, HTTP,
SMTP, etc. protocols to the fullest.
Until destroyed manually, servlets can be retained in the
memory helping process several requests over time. Also, once
a database connection is established, it can facilitate process
several requests for a database in the very same database
session.
We use cookies to ensure you have the bestServlets inherit on
browsing experience Java’s property
our website. ofour
By using portability and hence
site, you acknowledge are
that you have read and
understood our Cookie Policy & Privacy Policy
compatible with nearly any web server.
Skip to content
10/12/23, 10:45 PM Servlet Architecture - GeeksforGeeks

Servlets are first converted into byte codes and then executed,
which helps in increasing the processing time.

Disadvantages
Designing a servlet can be pretty laborious.
Exceptions need to be handled while designing a servlet since
they are not thread-safe.
Developers may need additional skills to program a servlet.

As we already know Servlets are portable (platform/server


independent) in nature and hence are a better option if we talk in
terms of other scripting languages. They process the requests and
responses dynamically. Whenever we are developing a web
application where we need to coordinate with different existing
protocols, servlets are preferred over other means because of its
capability to support various protocols. At last we can descend to a
conclusion that employing a servlet could potentially be most
suitable while developing a web application.

Last Updated : 02 Jan, 2021 23

Similar Reads
Javax.servlet.http.Co Servlet
class in Java Collaboration In
Java Using…
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
understood our Cookie Policy & Privacy Policy

Skip to content
10/12/23, 10:45 PM Servlet Architecture - GeeksforGeeks

Starting with first The HttpSession


Servlet Application Interface in Servlet
Life Cycle of a Difference between
Servlet Servlet and JSP

Servlet - Filter URL Rewriting using


Java Servlet

Hidden Form Field Java Servlet and


using Annotation | JDBC Example |
Java Servlet Insert data in MySQL

Related Tutorials
Java AWT Tutorial Spring MVC Tutorial

Spring Tutorial Spring Boot Tutorial

Java 8 Features -
Complete Tutorial

Previous Next

How to Save Data to the Java Program to Illustrate


Firebase Realtime Database the usage of Octal Integer
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
in Android?
understood our Cookie Policy & Privacy Policy

Skip to content
10/12/23, 10:45 PM Servlet Architecture - GeeksforGeeks

Article Contributed By :
shahzaibanwarkhan
S shahzaibanwarkhan

Follow

Vote for difficulty


Current difficulty : Easy

Easy Normal Medium Hard Expert

Article Tags : java-servlet , Java


Practice Tags : Java

Improve Article Report Issue

Master Java
Programming - Complete
Beginner to Advanced
Kick-start your JAVA programming
journey with this self-paced course
40+the
and master hours of
language from...163k+
content views

Mentorship byLEARN
Experts MORE
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
understood our Cookie Policy & Privacy Policy

Skip to content
10/12/23, 10:45 PM Servlet Architecture - GeeksforGeeks

Company Explore Languages DSA DSA Web


A-143, 9th Floor, Sovereign Corporate About Us Job-A-Thon Python Concepts Roadmaps Development
Tower, Sector-136, Noida, Uttar Pradesh - Hiring Challenge Data Structures DSA for HTML
201305 Legal Java
Careers Hack-A-Thon C++ Arrays Beginners CSS
feedback@geeksforgeeks.org
In Media GfG Weekly PHP Strings Basic DSA JavaScript
Contest Coding
Contact Us GoLang Linked List Bootstrap
Offline Classes Problems
Advertise with SQL Algorithms ReactJS
(Delhi/NCR) DSA Roadmap
us R Language Searching AngularJS
DSA in JAVA/C++ by Sandeep Jain
Campus Android Tutorial Sorting NodeJS
Master System DSA with
Training
Design Mathematical JavaScript Express.js
Program
Master CP Dynamic Top 100 DSA Lodash
Programming Interview
Problems
All Cheat Sheets

Computer Python Data Science DevOps Competitive System


Science Python & ML Git Programming Design
GATE CS Notes Programming Data Science AWS Top DSA for CP What is System
Examples With Python Design
Operating Docker Top 50 Tree
Systems Django Tutorial Data Science For Problems Monolithic and
Kubernetes
Computer Python Projects Beginner Top 50 Graph Distributed SD
Azure
Network Python Tkinter Machine Problems Scalability in SD
GCP
Databaseon our website.
We use cookies to ensure you have the best browsing experience OpenCV
By using our site, youLearning
Python acknowledge that you have read and Top 50 Array Databases in SD
understood our Cookie Policy & Privacy Policy
Management Tutorial Tutorial Problems
System Skip to content
10/12/23, 10:45 PM Servlet Architecture - GeeksforGeeks

Software Python Maths For Top 50 String High Level


Engineering Interview Machine Problems Design or HLD
Digital Logic Question Learning Top 50 DP Low Level
Design Pandas Tutorial Problems Design or LLD
Engineering NumPy Tutorial Top 15 Websites Top SD
Maths NLP Tutorial for CP Interview
Questions
Deep Learning
Tutorial

Interview GfG School Commerce UPSC SSC/ Write & Earn


Corner CBSE Notes for Accountancy Polity Notes BANKING Write an Article
Company Wise Class 8 Business Geography SSC CGL Improve an
Preparation CBSE Notes for Studies Notes Syllabus Article
Preparation for Class 9 Economics History Notes SBI PO Syllabus Pick Topics to
SDE CBSE Notes for Human Science and SBI Clerk Write
Experienced Class 10 Resource Technology Syllabus Write Interview
Interviews CBSE Notes for Management Notes IBPS PO Experience
Internship Class 11 (HRM) Economics Syllabus Internships
Interviews CBSE Notes for Management Notes IBPS Clerk
Competitive Class 12 Income Tax Important Syllabus
Programming English Finance Topics in Ethics Aptitude
Aptitude Grammar UPSC Previous Questions
Statistics for
Preparation Economics Year Papers SSC CGL
Practice Papers
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
understood our Cookie Policy & Privacy Policy
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

You might also like