<s>
A	O
Jakarta	B-Application
Servlet	I-Application
(	O
formerly	O
Java	B-Application
Servlet	I-Application
)	O
is	O
a	O
Java	B-Language
software	O
component	O
that	O
extends	O
the	O
capabilities	O
of	O
a	O
server	B-Application
.	O
</s>
<s>
Although	O
servlets	B-Application
can	O
respond	O
to	O
many	O
types	O
of	O
requests	O
,	O
they	O
most	O
commonly	O
implement	O
web	B-Language
containers	I-Language
for	O
hosting	O
web	B-Application
applications	I-Application
on	O
web	B-Application
servers	I-Application
and	O
thus	O
qualify	O
as	O
a	O
server-side	O
servlet	B-Application
web	B-General_Concept
API	I-General_Concept
.	O
</s>
<s>
Such	O
web	O
servlets	B-Application
are	O
the	O
Java	B-Language
counterpart	O
to	O
other	O
dynamic	O
web	O
content	O
technologies	O
such	O
as	O
PHP	B-Application
and	O
ASP.NET	B-Application
.	O
</s>
<s>
A	O
Jakarta	B-Application
Servlet	I-Application
processes	O
or	O
stores	O
a	O
Java	B-Language
class	I-Language
in	O
Jakarta	B-Language
EE	I-Language
that	O
conforms	O
to	O
the	O
Jakarta	B-Application
Servlet	I-Application
API	B-Application
,	O
a	O
standard	O
for	O
implementing	O
Java	B-Language
classes	O
that	O
respond	O
to	O
requests	O
.	O
</s>
<s>
Servlets	B-Application
could	O
in	O
principle	O
communicate	O
over	O
any	O
client	B-Operating_System
–	I-Operating_System
server	I-Operating_System
protocol	O
,	O
but	O
they	O
are	O
most	O
often	O
used	O
with	O
HTTP	B-Protocol
.	I-Protocol
</s>
<s>
Thus	O
"	O
servlet	B-Application
"	O
is	O
often	O
used	O
as	O
shorthand	O
for	O
"	O
HTTP	B-Protocol
servlet	B-Application
"	O
.	O
</s>
<s>
Thus	O
,	O
a	O
software	B-Application
developer	I-Application
may	O
use	O
a	O
servlet	B-Application
to	O
add	O
dynamic	O
content	O
to	O
a	O
web	B-Application
server	I-Application
using	O
the	O
Java	B-Device
platform	I-Device
.	O
</s>
<s>
The	O
generated	O
content	O
is	O
commonly	O
HTML	B-Language
,	O
but	O
may	O
be	O
other	O
data	O
such	O
as	O
XML	B-Protocol
and	O
more	O
commonly	O
,	O
JSON	B-General_Concept
.	O
</s>
<s>
Servlets	B-Application
can	O
maintain	O
state	B-Application
in	O
session	B-Protocol
variables	O
across	O
many	O
server	B-Application
transactions	O
by	O
using	O
HTTP	B-Application
cookies	I-Application
,	O
or	O
URL	O
mapping	O
.	O
</s>
<s>
The	O
Jakarta	B-Application
Servlet	I-Application
API	B-Application
has	O
,	O
to	O
some	O
extent	O
,	O
been	O
superseded	O
by	O
two	O
standard	O
Java	B-Language
technologies	I-Language
for	O
web	O
services	O
:	O
</s>
<s>
the	O
Jakarta	B-Language
XML	I-Language
Web	I-Language
Services	I-Language
(	O
JAX-WS	B-Language
)	O
useful	O
for	O
SOAP	B-Application
Web	O
Services	O
.	O
</s>
<s>
To	O
deploy	O
and	O
run	O
a	O
servlet	B-Application
,	O
a	O
web	B-Language
container	I-Language
must	O
be	O
used	O
.	O
</s>
<s>
A	O
web	B-Language
container	I-Language
(	O
also	O
known	O
as	O
a	O
servlet	B-Language
container	I-Language
)	O
is	O
essentially	O
the	O
component	O
of	O
a	O
web	B-Application
server	I-Application
that	O
interacts	O
with	O
the	O
servlets	B-Application
.	O
</s>
<s>
The	O
web	B-Language
container	I-Language
is	O
responsible	O
for	O
managing	O
the	O
lifecycle	O
of	O
servlets	B-Application
,	O
mapping	O
a	O
URL	O
to	O
a	O
particular	O
servlet	B-Application
and	O
ensuring	O
that	O
the	O
URL	O
requester	O
has	O
the	O
correct	O
access	O
rights	O
.	O
</s>
<s>
The	O
Servlet	B-Application
API	B-Application
,	O
contained	O
in	O
the	O
Java	B-Language
package	I-Language
hierarchy	O
,	O
defines	O
the	O
expected	O
interactions	O
of	O
the	O
web	B-Language
container	I-Language
and	O
a	O
servlet	B-Application
.	O
</s>
<s>
The	O
basic	O
Servlet	B-Application
package	O
defines	O
Java	B-Language
objects	O
to	O
represent	O
servlet	B-Application
requests	O
and	O
responses	O
,	O
as	O
well	O
as	O
objects	O
to	O
reflect	O
the	O
servlet	B-Application
's	O
configuration	O
parameters	O
and	O
execution	O
environment	O
.	O
</s>
<s>
The	O
package	O
defines	O
HTTP-specific	O
subclasses	O
of	O
the	O
generic	O
servlet	B-Application
elements	O
,	O
including	O
session	B-Protocol
management	I-Protocol
objects	O
that	O
track	O
multiple	O
requests	O
and	O
responses	O
between	O
the	O
web	B-Application
server	I-Application
and	O
a	O
client	O
.	O
</s>
<s>
Servlets	B-Application
may	O
be	O
packaged	O
in	O
a	O
WAR	B-Language
file	I-Language
as	O
a	O
web	B-Application
application	I-Application
.	O
</s>
<s>
Servlets	B-Application
can	O
be	O
generated	O
automatically	O
from	O
Jakarta	B-Language
Server	I-Language
Pages	I-Language
(	O
JSP	B-Language
)	O
by	O
the	O
Jakarta	B-Language
Server	I-Language
Pages	I-Language
compiler	O
.	O
</s>
<s>
The	O
difference	O
between	O
servlets	B-Application
and	O
JSP	B-Language
is	O
that	O
servlets	B-Application
typically	O
embed	O
HTML	B-Language
inside	O
Java	B-Language
code	I-Language
,	O
while	O
JSPs	O
embed	O
Java	B-Language
code	I-Language
in	O
HTML	B-Language
.	O
</s>
<s>
While	O
the	O
direct	O
usage	O
of	O
servlets	B-Application
to	O
generate	O
HTML	B-Language
(	O
as	O
shown	O
in	O
the	O
example	O
below	O
)	O
has	O
become	O
rare	O
,	O
the	O
higher	O
level	O
MVC	O
web	O
framework	O
in	O
Jakarta	B-Language
EE	I-Language
(	O
JSF	B-Language
)	O
still	O
explicitly	O
uses	O
the	O
servlet	B-Application
technology	O
for	O
the	O
low	O
level	O
request/response	O
handling	O
via	O
the	O
.	O
</s>
<s>
A	O
somewhat	O
older	O
usage	O
is	O
to	O
use	O
servlets	B-Application
in	O
conjunction	O
with	O
JSPs	O
in	O
a	O
pattern	O
called	O
"	O
Model	B-Architecture
2	I-Architecture
"	O
,	O
which	O
is	O
a	O
flavor	O
of	O
the	O
model	O
–	O
view	O
–	O
controller	O
.	O
</s>
<s>
The	B-Application
Java	I-Application
Servlet	I-Application
API	I-Application
was	O
first	O
publicly	O
announced	O
at	O
the	O
inaugural	O
JavaOne	O
conference	O
in	O
May	O
1996	O
.	O
</s>
<s>
This	O
was	O
the	O
first	O
alpha	O
of	O
the	O
Java	B-Language
Web	B-Application
Server	I-Application
(	O
JWS	O
;	O
then	O
known	O
by	O
its	O
codename	O
Jeeves	O
)	O
which	O
would	O
eventually	O
be	O
shipped	O
as	O
a	O
product	O
on	O
June	O
5	O
,	O
1997	O
.	O
</s>
<s>
In	O
his	O
blog	O
on	O
java.net,	O
Sun	O
veteran	O
and	O
GlassFish	B-Language
lead	O
Jim	O
Driscoll	O
details	O
the	O
history	O
of	O
servlet	B-Application
technology	O
.	O
</s>
<s>
James	O
Gosling	O
first	O
thought	O
of	O
servlets	B-Application
in	O
the	O
early	O
days	O
of	O
Java	B-Language
,	O
but	O
the	O
concept	O
did	O
not	O
become	O
a	O
product	O
until	O
December	O
1996	O
when	O
Sun	O
shipped	O
JWS	O
.	O
</s>
<s>
This	O
was	O
before	O
what	O
is	O
now	O
the	O
Jakarta	B-Language
EE	I-Language
was	O
made	O
into	O
a	O
specification	O
.	O
</s>
<s>
Starting	O
with	O
version	O
2.2	O
,	O
the	O
specification	O
was	O
developed	O
under	O
the	O
Java	B-Language
Community	I-Language
Process	I-Language
.	O
</s>
<s>
Three	O
methods	O
are	O
central	O
to	O
the	O
life	O
cycle	O
of	O
a	O
servlet	B-Application
.	O
</s>
<s>
They	O
are	O
implemented	O
by	O
every	O
servlet	B-Application
and	O
are	O
invoked	O
at	O
specific	O
times	O
by	O
the	O
server	B-Application
.	O
</s>
<s>
During	O
initialization	O
stage	O
of	O
the	O
servlet	B-Application
life	O
cycle	O
,	O
the	O
web	B-Language
container	I-Language
initializes	O
the	O
servlet	B-Application
instance	O
by	O
calling	O
the	O
method	O
,	O
passing	O
an	O
object	O
implementing	O
the	O
interface	O
.	O
</s>
<s>
This	O
configuration	O
object	O
allows	O
the	O
servlet	B-Application
to	O
access	O
name-value	B-Application
initialization	O
parameters	O
from	O
the	O
web	B-Application
application	I-Application
.	O
</s>
<s>
After	O
initialization	O
,	O
the	O
servlet	B-Application
instance	O
can	O
service	O
client	O
requests	O
.	O
</s>
<s>
The	O
web	B-Language
container	I-Language
calls	O
the	O
service( )	O
method	O
of	O
the	O
servlet	B-Application
for	O
every	O
request	O
.	O
</s>
<s>
The	O
developer	B-Application
of	O
the	O
servlet	B-Application
must	O
provide	O
an	O
implementation	O
for	O
these	O
methods	O
.	O
</s>
<s>
If	O
a	O
request	O
is	O
made	O
for	O
a	O
method	O
that	O
is	O
not	O
implemented	O
by	O
the	O
servlet	B-Application
,	O
the	O
method	O
of	O
the	O
parent	O
class	O
is	O
called	O
,	O
typically	O
resulting	O
in	O
an	O
error	O
being	O
returned	O
to	O
the	O
requester	O
.	O
</s>
<s>
Finally	O
,	O
the	O
web	B-Language
container	I-Language
calls	O
the	O
destroy( )	O
method	O
that	O
takes	O
the	O
servlet	B-Application
out	O
of	O
service	O
.	O
</s>
<s>
The	O
destroy( )	O
method	O
,	O
like	O
init( )	O
,	O
is	O
called	O
only	O
once	O
in	O
the	O
lifecycle	O
of	O
a	O
servlet	B-Application
.	O
</s>
<s>
The	O
browser	O
then	O
generates	O
an	O
HTTP	B-Protocol
request	O
for	O
this	O
URL	O
.	O
</s>
<s>
This	O
request	O
is	O
then	O
sent	O
to	O
the	O
appropriate	O
server	B-Application
.	O
</s>
<s>
The	O
HTTP	B-Protocol
request	O
is	O
received	O
by	O
the	O
web	B-Application
server	I-Application
and	O
forwarded	O
to	O
the	O
servlet	B-Language
container	I-Language
.	O
</s>
<s>
The	O
container	O
maps	O
this	O
request	O
to	O
a	O
particular	O
servlet	B-Application
.	O
</s>
<s>
The	O
servlet	B-Application
is	O
dynamically	O
retrieved	O
and	O
loaded	O
into	O
the	O
address	O
space	O
of	O
the	O
container	O
.	O
</s>
<s>
The	O
container	O
invokes	O
the	O
init( )	O
method	O
of	O
the	O
servlet	B-Application
.	O
</s>
<s>
This	O
method	O
is	O
invoked	O
only	O
when	O
the	O
servlet	B-Application
is	O
first	O
loaded	O
into	O
memory	O
.	O
</s>
<s>
It	O
is	O
possible	O
to	O
pass	O
initialization	O
parameters	O
to	O
the	O
servlet	B-Application
so	O
that	O
it	O
may	O
configure	O
itself	O
.	O
</s>
<s>
The	O
container	O
invokes	O
the	O
service( )	O
method	O
of	O
the	O
servlet	B-Application
.	O
</s>
<s>
This	O
method	O
is	O
called	O
to	O
process	O
the	O
HTTP	B-Protocol
request	O
.	O
</s>
<s>
The	O
servlet	B-Application
may	O
read	O
data	O
that	O
has	O
been	O
provided	O
in	O
the	O
HTTP	B-Protocol
request	O
.	O
</s>
<s>
The	O
servlet	B-Application
may	O
also	O
formulate	O
an	O
HTTP	B-Protocol
response	O
for	O
the	O
client	O
.	O
</s>
<s>
The	O
servlet	B-Application
remains	O
in	O
the	O
container	O
's	O
address	O
space	O
and	O
is	O
available	O
to	O
process	O
any	O
other	O
HTTP	B-Protocol
requests	I-Protocol
received	O
from	O
clients	O
.	O
</s>
<s>
The	O
service( )	O
method	O
is	O
called	O
for	O
each	O
HTTP	B-Protocol
request	O
.	O
</s>
<s>
The	O
container	O
may	O
,	O
at	O
some	O
point	O
,	O
decide	O
to	O
unload	O
the	O
servlet	B-Application
from	O
its	O
memory	O
.	O
</s>
<s>
The	O
container	O
calls	O
the	O
servlet	B-Application
's	O
destroy( )	O
method	O
to	O
relinquish	O
any	O
resources	O
such	O
as	O
file	O
handles	O
that	O
are	O
allocated	O
for	O
the	O
servlet	B-Application
;	O
important	O
data	O
may	O
be	O
saved	O
to	O
a	O
persistent	O
store	O
.	O
</s>
<s>
The	O
memory	O
allocated	O
for	O
the	O
servlet	B-Application
and	O
its	O
objects	O
can	O
then	O
be	O
garbage	O
collected	O
.	O
</s>
<s>
The	O
following	O
example	O
servlet	B-Application
prints	O
how	O
many	O
times	O
its	O
service( )	O
method	O
was	O
called	O
.	O
</s>
<s>
Note	O
that	O
HttpServlet	O
is	O
a	O
subclass	O
of	O
GenericServlet	O
,	O
an	O
implementation	O
of	O
the	O
Servlet	B-Application
interface	O
.	O
</s>
<s>
The	O
service( )	O
method	O
of	O
HttpServlet	O
class	O
dispatches	O
requests	O
to	O
the	O
methods	O
doGet( )	O
,	O
doPost( )	O
,	O
doPut( )	O
,	O
doDelete( )	O
,	O
and	O
so	O
on	O
;	O
according	O
to	O
the	O
HTTP	B-Protocol
request	O
.	O
</s>
<s>
In	O
the	O
example	O
below	O
service( )	O
is	O
overridden	O
and	O
does	O
not	O
distinguish	O
which	O
HTTP	B-Protocol
request	O
method	O
it	O
serves	O
.	O
</s>
<s>
The	O
specification	O
for	O
Servlet	B-Application
technology	O
has	O
been	O
implemented	O
in	O
many	O
products	O
.	O
</s>
<s>
See	O
a	O
list	O
of	O
implementations	O
on	O
the	O
web	B-Language
container	I-Language
page	O
.	O
</s>
<s>
There	O
are	O
also	O
other	O
types	O
of	O
servlet	B-Language
containers	I-Language
such	O
as	O
those	O
for	O
SIP	O
servlets	B-Application
,	O
e.g.	O
,	O
SailFin	B-Language
.	O
</s>
