SlideShare a Scribd company logo
Web Component Development
with Servlet & JSP Technologies
(EE 6)
Module-4: The Servlet’s Environment
www.webstackacademy.com
Objectives
Upon completion of this module, you should be able
to:
● Describe the environment in which the servlet runs
● Describe HTTP headers and their function
● Use HTML forms to collect data from users and send
it to a servlet
● Understand how the web container transfers a
request to the servlet
● Understand and use HttpSession Object
www.webstackacademy.com
Relevance
Discussion – The following questions are relevant to
understanding what technologies are available for
developing web applications and the limitations of
those technologies:
● What environment does the servlet run in?
● What are HTTP headers, and what are they for?
● How can you get data from a browser to a server?
● How can a server recognize a browser that is making
successive calls?
www.webstackacademy.com
Http Get Method
One of the two most common HTTP methods is the GET
request. A GET method is used whenever the user
clicks a hyperlink in the HTML page currently being
viewed. A GET method is also used when the user
enters a URL into the Location field (for Netscape
NavigatorTM and FireFox) or the Address field (for
Microsoft Internet Explorer). While processing a web
page, the browser also issues GET requests for images,
applets, style sheet files, and other linked media.
www.webstackacademy.com
HTTP Request
The request stream acts as an envelope to the request
URL and message body of the HTTP client request. The
first line of the request stream is called the request line.
It includes the HTTP method (usually either GET or
POST), followed by a space character, followed by the
requested URL (usually a path to a static file), followed
by a space, and finally followed by the HTTP version
number. The request line is followed by any number of
request header lines.
www.webstackacademy.com
Http Request Stream
Example
www.webstackacademy.com
HTTP Request
Headers
Table illustrates some of the HTTP headers that the browser can
place in the request. The headers could then be used to determine
how the server processes the request.
www.webstackacademy.com
HTTP Response
The response stream acts as an envelope to the message
body of the HTTP server response. The first line of the
response stream is called the status line. The status line
includes the HTTP version number, followed by a space,
followed by the numeric status code of the response,
followed by a space, and finally followed by a short text
message represented by the status code.
www.webstackacademy.com
HTTP Response
Headers
Table illustrates some of the HTTP headers that the server can place in
the response. The headers could then be used to determine how the
browser processes the response.
www.webstackacademy.com
Input Types
<p>Year: <input type=’text’ name=’theYear’/></p>
www.webstackacademy.com
Drop-Down List
Component
www.webstackacademy.com
<select name=’season’>
Season:
<option value=’UNKNOWN’>select...</option>
<option value=’Spring’>Spring</option>
<option value=’Summer’>Summer</option>
<option value=’Fall’>Fall</option>
<option value=’Winter’>Winter</option>
</select> <br/><br/>
Drop-Down List
Component
www.webstackacademy.com
An Example HTML
Form
<form>
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
www.webstackacademy.com
How Form Data Are
Sent in an HTTP
Request
Syntax:
fieldName1=fieldValue1&fieldName2=fieldValue2
Example:
Username= Fred&password=C1r5z
www.webstackacademy.com
HTTP GET Method
Request
Form data are carried in the URL of the HTTP GET request:
GET /admin/add_league.do
year=2003&season=Winter&title=Westminster+Indoor
www.webstackacademy.com
HTTP POST Method
Request
Form data is contained in the body of the HTTP request:
POST /admin/add_league.do HTTP/1.1
www.webstackacademy.com
HTTP GET Methods
The HTTP GET method is used in these conditions:
● The processing of the request is idempotent.
● This means that the request does not have side-effects on the
server or that multiple invocations produce no more changes
than the first invocation.
● The amount of form data is small.
● You want to allow the request to be bookmarked along with its
data.
www.webstackacademy.com
HTTP Post Method
The HTTP POST method is used in these conditions:
● The processing of the request changes the state of the server, such
as storing data in a database.
● The amount of form data is large.
● The contents of the data should not be visible in the URL (for
example, passwords)
www.webstackacademy.com
Web Container
Architecture
www.webstackacademy.com
Request and Response
Process
Browser Connects to the Web Container:
The first step in this process is the web browser sending an HTTP
request
to the web container. To process a request, the browser makes a
TCP
socket connection to the web container.
Web Container Objectifies the Input/Output Streams:
Next, the web container creates an object that encapsulates the
data in therequest and response streams. These two objects
represent all of the
information available in the HTTP request and response streams.
www.webstackacademy.com
Request and
Response Process
Web Container Executes the Servlet:
The web container then executes the service method on the
requested
servlet. The request and response objects are passed as
arguments to this
method. The execution of the service method occurs in a separate
Thread.
Servlet Uses the Output Stream to Generate the Response:
Finally, the text of the response generated by the servlet is
packaged into
an HTTP response stream, which is returned to the web browser.
www.webstackacademy.com
Sequence Diagram of
an HTTP GET Request
www.webstackacademy.com
The HttpServlet API
www.webstackacademy.com
The HTTP request information is encapsulated by the
HttpServletRequest interface. The getHeaderNames
method returns an enumeration of strings composed
of the names of each header in the request stream.
To retrieve the value of a specific header, you can
use the getHeader method. This method returns a
String. Some header values might represent integer
or date information. There are two convenience
methods, getIntHeader and getDateHeader, that
perform the conversion for you.
HttpServletRequest
API
www.webstackacademy.com
HttpServletResponse
API
The HTTP response information is encapsulated by the
HttpServletResponse interface. You can set a response
header using the setHeader method. If the header
value you want to set is either an integer or a date,
then you can use the convenience methods
setIntHeader or setDateHeader.
www.webstackacademy.com
Handling Errors in
Servlet Processing
You throw an exception from a doXxx() servlet method,
you should wrap the exception in either a
ServletException, or an UnavailableException. The
ServletException is used to indicate that this particular
request has failed. UnavailableException indicates that
the problem is environmental, rather than specific to
this request.
www.webstackacademy.com
The HTTP Protocol
and Client Sessions
● HTTP is a stateless protocol. That means that every
request is, from the perspective of HTTP, entirely
separate from every other. There is no concept of an
ongoing conversation. This provides great potential
for scalability and redundancy in servers, because
any request can go to any one of a collection of
identically equipped servers.
● Web servers in a Java EE environment are required to
provide mechanisms for identifying clients and
associating each client with a map that can be used
to store client specific data. That map is implemented
using the HttpSession class.
www.webstackacademy.com
The HttpSession API
www.webstackacademy.com
Storing Session
Attributes
To store a value in a session object, use the
setAttribute method. For example, if a servlet has
prepared an object referred to by a variable called
league, this could be placed in the session using the
following code:
HttpSession session = request.getSession();
session.setAttribute(“league”, league);
www.webstackacademy.com
Retrieving Session
Attributes
HttpSession session = request.getSession();
League league =
(League)session.getAttribute(“league”);
www.webstackacademy.com
Closing the Session
When the web application has finished with a session,
such as if the user logs out, you should call the
invalidate method on the HTTPSession object. If you
fail to invalidate the session, you might clutter up the
memory of the server with lots of unnecessary objects.
Such memory clutter is detrimental to performance
and scalability.
Note – In a secured environment, the invalidate
method is likely to delete authentication information
forcing, the user to login again.
www.webstackacademy.com
Additional Session
Methods
SetMaxInactiveInterval method
session.setMaxInactiveInterval(30*60);
www.webstackacademy.com
Session
Configuration
<web-app ...>
<session-config>
<session-timeout>30</session-timeout>
<tracking-mode>SSL</tracking-mode>
</session-config>
</web-app>
www.webstackacademy.com
Using Cookies for
Client-Specific
Storage
Cookies allow a web server to store information on the
client machine. Data are stored as key-value pairs in
the browser and are specific to the individual client.
www.webstackacademy.com
● Cookies are sent in a response from the web server.
● Cookies are stored on the client’s computer.
● Cookies are stored in a partition assigned to the web
server’s domain name. Cookies can be further
partitioned by a path within the domain.
● All cookies for that domain (and path) are sent in
every request to that web server.
● Cookies have a life span and are flushed by the client
browser at the end of that life span.
Using Cookies for
Client-Specific
Storage
www.webstackacademy.com
Using Cookies
Example
In your servlet, you could use the following code to store that
cookie:
String name = request.getParameter("firstName");
Cookie c = new Cookie("yourname", name);
response.addCookie(c);
Later, when the visitor returns, your servlet can access the
yourname cookie using the following code:
Cookie[] allCookies = request.getCookies();
for ( int i=0; i < allCookies.length; i++ ) {
if ( allCookies[i].getName().equals(“yourname”) ) {
name = allCookies[i].getValue();
}
}
www.webstackacademy.com
Using URL-Rewriting for
Session Management
URL-rewriting is an alternative session management
strategy that the web container must support.
Typically, a web container attempts to use cookies to
store the session ID. If that fails (that is, the user has
cookies turned off in the browser), then the web
container tries to use URL-rewriting.
Web Stack Academy (P) Ltd
#83, Farah Towers,
1st floor,MG Road,
Bangalore – 560001
M: +91-80-4128 9576
T: +91-98862 69112
E: info@www.webstackacademy.com
www.webstackacademy.com

More Related Content

What's hot (20)

PDF
Http methods
maamir farooq
 
PPTX
HTTP fundamentals for developers
Mario Cardinal
 
PPTX
Http
NITT, KAMK
 
PPT
Http request&response by Vignesh 15 MAR 2014
Navaneethan Naveen
 
KEY
What's up with HTTP?
Mark Nottingham
 
PDF
21servers And Applets
Adil Jafri
 
PDF
21 HTTP Protocol #burningkeyboards
Denis Ristic
 
PPTX
HTTP
altaykarakus
 
PPTX
Session And Cookies In Servlets - Java
JainamParikh3
 
PPTX
Java Servlets
Emprovise
 
PPTX
Http
Maiyur Hossain
 
PPTX
Http protocol
Arpita Naik
 
PDF
Lec 7(HTTP Protocol)
maamir farooq
 
PPT
Jagmohancrawl
Jag Mohan Singh
 
PDF
Mule caching strategy with redis cache
Priyobroto Ghosh (Mule ESB Certified)
 
PPTX
SERVIET
sathish sak
 
PPTX
Http - All you need to know
Gökhan Şengün
 
PPTX
Hypertex transfer protocol
wanangwa234
 
PPT
HTTP protocol and Streams Security
Blueinfy Solutions
 
PPT
Web Technologies -- Servlets 4 unit slides
Sasidhar Kothuru
 
Http methods
maamir farooq
 
HTTP fundamentals for developers
Mario Cardinal
 
Http request&response by Vignesh 15 MAR 2014
Navaneethan Naveen
 
What's up with HTTP?
Mark Nottingham
 
21servers And Applets
Adil Jafri
 
21 HTTP Protocol #burningkeyboards
Denis Ristic
 
Session And Cookies In Servlets - Java
JainamParikh3
 
Java Servlets
Emprovise
 
Http protocol
Arpita Naik
 
Lec 7(HTTP Protocol)
maamir farooq
 
Jagmohancrawl
Jag Mohan Singh
 
Mule caching strategy with redis cache
Priyobroto Ghosh (Mule ESB Certified)
 
SERVIET
sathish sak
 
Http - All you need to know
Gökhan Şengün
 
Hypertex transfer protocol
wanangwa234
 
HTTP protocol and Streams Security
Blueinfy Solutions
 
Web Technologies -- Servlets 4 unit slides
Sasidhar Kothuru
 

Similar to Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4 - The Servlet’s Environment (20)

PDF
Sun certifiedwebcomponentdeveloperstudyguide
Alberto Romero Jiménez
 
PPT
Anintroductiontojavawebtechnology 090324184240-phpapp01
raviIITRoorkee
 
PPTX
java Servlet technology
Tanmoy Barman
 
PPTX
SCWCD : The servlet model : CHAP : 2
Ben Abdallah Helmi
 
PPTX
SCWCD : The servlet model CHAP : 2
Ben Abdallah Helmi
 
PPT
Web Tech Java Servlet Update1
vikram singh
 
PPT
An Introduction To Java Web Technology
vikram singh
 
PPTX
J2EE : Java servlet and its types, environment
joearunraja2
 
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1...
WebStackAcademy
 
PPT
Servlet.ppt
MouDhara1
 
PPT
Servlet.ppt
kstalin2
 
PPT
Servlet1.ppt
KhushalChoudhary14
 
PPT
Servlet123jkhuiyhkjkljioyudfrtsdrestfhgb
shubhangimalas1
 
PPTX
Servlets
Geethu Mohan
 
PDF
restapitest-anil-200517181251.pdf
mrle7
 
PPTX
Rest API Testing
upadhyay_25
 
PDF
JEE Programming - 04 Java Servlets
Danairat Thanabodithammachari
 
PPTX
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
ssuser4f7d71
 
PPT
Servlets
Manav Prasad
 
Sun certifiedwebcomponentdeveloperstudyguide
Alberto Romero Jiménez
 
Anintroductiontojavawebtechnology 090324184240-phpapp01
raviIITRoorkee
 
java Servlet technology
Tanmoy Barman
 
SCWCD : The servlet model : CHAP : 2
Ben Abdallah Helmi
 
SCWCD : The servlet model CHAP : 2
Ben Abdallah Helmi
 
Web Tech Java Servlet Update1
vikram singh
 
An Introduction To Java Web Technology
vikram singh
 
J2EE : Java servlet and its types, environment
joearunraja2
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1...
WebStackAcademy
 
Servlet.ppt
MouDhara1
 
Servlet.ppt
kstalin2
 
Servlet1.ppt
KhushalChoudhary14
 
Servlet123jkhuiyhkjkljioyudfrtsdrestfhgb
shubhangimalas1
 
Servlets
Geethu Mohan
 
restapitest-anil-200517181251.pdf
mrle7
 
Rest API Testing
upadhyay_25
 
JEE Programming - 04 Java Servlets
Danairat Thanabodithammachari
 
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
ssuser4f7d71
 
Servlets
Manav Prasad
 
Ad

More from WebStackAcademy (20)

PDF
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
PDF
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
PDF
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
PDF
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
PDF
Webstack Academy - Internship Kick Off
WebStackAcademy
 
PDF
Building Your Online Portfolio
WebStackAcademy
 
PDF
Front-End Developer's Career Roadmap
WebStackAcademy
 
PDF
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
PDF
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
PDF
Angular - Chapter 5 - Directives
WebStackAcademy
 
PDF
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
PDF
Angular - Chapter 3 - Components
WebStackAcademy
 
PDF
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
PDF
Angular - Chapter 1 - Introduction
WebStackAcademy
 
PDF
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
PDF
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
PDF
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
PDF
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
Webstack Academy - Internship Kick Off
WebStackAcademy
 
Building Your Online Portfolio
WebStackAcademy
 
Front-End Developer's Career Roadmap
WebStackAcademy
 
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
Angular - Chapter 5 - Directives
WebStackAcademy
 
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
Angular - Chapter 3 - Components
WebStackAcademy
 
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
Angular - Chapter 1 - Introduction
WebStackAcademy
 
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Ad

Recently uploaded (20)

PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Digital Circuits, important subject in CS
contactparinay1
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 

Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4 - The Servlet’s Environment