You are on page 1of 12

Servlet Tutorial

1. An Introduction to Servlets
Some Example Applications
Architecture Overview
Lifecycle
2. How to rite a servlet
Interactin! with "lients
Lifecycle #ethods
$rovidin! Information a%out the Servlet
&. How to use servletrunner to 'un a Servlet
(. Examples
). Learnin! more a%out servlets
Introduction to Servlets
Servlets are modules that run inside re*uest+response,oriented servers- such as .ava,ena%led we%
servers. /unctionally they operate in a very similar way to "0I scripts- however- %ein! .ava %ased they
are more platform independent.
Some Example Applications
A few of the many applications for servlets include-
$rocessin! data $OS1ed over H11$S usin! an H1#L form- includin! purchase order or credit
card data. A servlet li2e this could %e part of an order,entry and processin! system- wor2in!
with product and inventory data%ases- and perhaps an on,line payment system.
Allowin! colla%oration %etween people. A servlet can handle multiple re*uests concurrently3
they can synchroni4e re*uests to support systems such as on,line conferencin!.
/orwardin! re*uests. Servlets can forward re*uests to other servers and servlets. 1his allows
them to %e used to %alance load amon! several servers that mirror the same content. It also
allows them to %e used to partition a sin!le lo!ical service over several servers- accordin! to
tas2 type or or!ani4ational %oundaries.
Servlet Architecture Overview
1he central a%straction in the Servlet A$I is the Servlet interface. All servlets implement this
interface- either directly or- more commonly- %y extendin! a class that implements it such as
HttpServlet. 1he inheritance hierarchy loo2s as follows.
1he Servlet interface provides the followin! methods that mana!e the servlet and its
communications with clients.
destroy()
"leans up whatever resources are %ein! held and ma2es sure that any persistent state is
synchroni4ed with the servlet5s current in,memory state.
getServletConfig()
'eturns a servlet confi! o%6ect- which contains any initiali4ation parameters and startup
confi!uration for this servlet.
getServletInfo()
'eturns a strin! containin! information a%out the servlet- such as its author- version- and
copyri!ht.
init(ServletConfig)
Initiali4es the servlet. 'un once %efore any re*uests can %e serviced.
service(Servlete!uest" Servletesponse)
"arries out a sin!le re*uest from the client.
Servlet writers provide some or all of these methods when developin! a servlet.
hen a servlet accepts a service call from a client- it receives two o%6ects- ServletRequest and
ServletResponse. 1he ServletRequest class encapsulates the communication from the client
to the server- while the ServletResponse class encapsulates the communication from the servlet
%ac2 to the client.
1he ServletRequest interface allows the servlet access to information such as the names of the
parameters passed in %y the client- the protocol 7scheme8 %ein! used %y the client- and the names of the
remote host that made the re*uest and the server that received it. It also provides the servlet with access
to the input stream- ServletInputStream- throu!h which the servlet !ets data from clients that are
usin! application protocols such as the H11$ $OS1 and $91 methods. Su%classes of
ServletRequest allow the servlet to retrieve more protocol,specific data. /or example-
HttpServletRequest contains methods for accessin! H11$,specific header information.
1he ServletResponse interface !ives the servlet methods for replyin! to the client. It allows the
servlet to set the content len!th and mime type of the reply- and provides an output stream-
ServletOutputStream- and a riter throu!h which the servlet can send the reply data.
Su%classes of ServletResponse !ive the servlet more protocol,specific capa%ilities. /or example-
HttpServletResponse contains methods that allow the servlet to manipulate H11$,specific
header information.
1he classes and interfaces descri%ed a%ove ma2e up a %asic Servlet. H11$ servlets have some
additional o%6ects that provide session,trac2in! capa%ilities. 1he servlet writer can use these A$Is to
maintain state %etween the servlet and the client that persists across multiple connections durin! some
time period.
Servlet #ifecycle
Servers load and run servlets- which then accept 4ero or more re*uests from clients and return data to
them. 1hey can also remove servlets. 1hese are the steps of a servlets lifecycle. 1he next para!raphs
descri%e each step in more detail- concentratin! on concurrency issues.
hen a server loads a servlet- it runs the servlet5s init method. Even thou!h most servlets are run in
multi,threaded servers- there are no concurrency issues durin! servlet initiali4ation. 1his is %ecause the
server calls the init method once- when it loads the servlet- and will not call it a!ain unless it is
reloadin! the servlet. 1he server can not reload a servlet until after it has removed the servlet %y callin!
the destroy method. Initiali4ation is allowed to complete %efore client re*uests are handled 7that is-
%efore the service method is called8 or the servlet is destroyed.
After the server loads and initiali4es the servlet- the servlet is a%le to handle client re*uests. It processes
them in its service method. Each client5s re*uest has its call to the service method run in its own
servlet thread: the method receives the client5s re*uest- and sends the client its response.
Servlets can run multiple service methods at a time. It is important- therefore- that service
methods %e written in a thread,safe manner. If- for some reason- a server should not run multiple
service methods concurrently- the servlet should implement the SingleThreadModel interface.
1his interface !uarantees that no two threads will execute the servlet5s service methods
concurrently.
Servlets run until they are removed from the service. hen a server removes a servlet- it runs the
servlet5s destroy method. 1he method is run once3 the server will not run it a!ain until after it
reloads and reinitiali4es the servlet.
1he dia!ram %elow shows the %asic interactions %etween clients- a we% server- and a servlet re!istered
with the we% server.
hen the destroy method runs- however- other threads mi!ht %e runnin! service re*uests. If- in
cleanin! up- it is necessary to access shared resources- that access should %e synchroni4ed. ;urin! a
servlet5s lifecycle- it is important to write thread,safe code for destroyin! the servlet and- unless the
servlet implements the SingleThreadModel interface- servicin! client re*uests.
$riting the Servlet
Servlets implement the javax.servlet.Servlet interface. hile servlet writers can develop
servlets %y implementin! the interface directly- this is usually not re*uired. <ecause most servlets
extend we% servers that use the H11$ protocol to interact with clients- the most common way to
develop servlets is %y speciali4in! the javax.servlet.http.HttpServlet class. 1his tutorial
concentrates on descri%in! this method of writin! servlets.
1he HttpServlet class implements the Servlet interface %y extendin! the 0enericServlet %ase class-
and provides a framewor2 for handlin! the H11$ protocol. Its service method supports standard
H11$+1.1 re*uests %y dispatchin! each re*uest to a method desi!ned to handle it.
<y default- servlets written %y speciali4in! the HttpServlet class can have multiple threads
concurrently runnin! its service method. If- you would li2e to have only a sin!le thread runnin! a
sin!le service method at a time- then in addition to extendin! the HttpServlet- your servlet
should also implement the SingleThreadModel interface. 1his does not involve writin! any extra
methods- merely declarin! that the servlet implements the interface.
/or example-
pu%lic class SurveyServlet extends HttpServlet
implements Sin!le1hread#odel =
+> typical servlet code- with no threadin! concerns
> in the service method. ?o extra code for the
> Sin!le1hread#odel interface. >+
...
@
Interacting with Clients
Servlet writers who are developin! H11$ servlets that speciali4e the HttpServlet class should
override the method or methods desi!ned to handle the H11$ interactions that their servlet will handle.
1he candidate methods include-
doGet- for handlin! 0E1- conditional 0E1 and HEA; re*uests
doost- for handlin! $OS1 re*uests
dout- for handlin! $91 re*uests
do!elete- for handlin! ;ELE1E re*uests
<y default- these methods return a "#!$R%&'%ST ()**+ error. An example H11$ servlet that
handles 0E1 and HEA; re*uests follows3 it speciali4es the doGet method. 1he second example is
also provided. It handles $OS1 re*uests from a form %y speciali4in! the doost method.
1he HttpServlet5s service method- %y default- also calls the doOptions method when it
receives an O$1IO?S re*uest- and doTrace when it receives a 1'A"E re*uest. 1he default
implementation of doOptions automatically determines what H11$ options are supported and
returns that information. 1he default implementation of doTrace causes a response with a messa!e
containin! all of the headers sent in the trace re*uest. 1hese methods are not typically overridden.
hatever method you override- it will ta2e two ar!uments. 1he first encapsulates the data from the
client- and is an HttpServletRequest. 1he second encapsulates the response to the client- and is
an HttpServletResponse. 1he followin! para!raphs discuss their use.
An HttpServletRequest o%6ect provides access to H11$ header data- such as any coo2ies found
in the re*uest and the H11$ method with which the re*uest was made. It- of course- allows the you to
o%tain the ar!uments that the client sent as part of the re*uest. How you access the client data mi!ht
depend on the H11$ method of the re*uest.
/or any H11$ method- you can use the getarameter,alues method- which will return
the value of a named parameter. 71he method getarameter-ames provides the names of
the parameters.8 Aou can also manually parse the re*uest.
/or re*uests usin! the H11$ 0E1 method- the get&ueryString method will return a
String to %e parsed.
/or H11$ methods $OS1- $91- and ;ELE1E- you have the choice %etween two methods. If
you expect text data- then it can %e read usin! the "u..eredReader returned %y the
getReader method- if you expect %inary data- then it should %e read with the
ServletInputStream returned %y the getInputStream method.
?ote that you should use either the getarameter,alues method or one of the methods that allow
you to parse the data yourself. 1hey can not %e used to!ether in a sin!le re*uest.
/or respondin! to the client- an HttpServletResponse o%6ect provides two ways of returnin! the
response data to the user. Aou can use the writer returned %y the get/riter method or the output
stream returned %y the getOutputStream method. Aou should use get/riter to return text data
to the user- and getOutputStream for %inary data.
<efore accessin! the riter or OutputStream- H11$ header data should %e set. 1he
HttpServletResponse class provides methods to access the header data- such as the content type
and encodin!- and content len!th of the response. After you set the headers- you may o%tain the writer
or output stream and send the %ody of the response to the user. "losin! the writer or output stream after
sendin! the response to the client allows the server to 2now when the response is complete.
Example of an %TT& Servlet that handles the 'ET and %EA( methods
+>>
> 1his is a simple example of an H11$ Servlet. It responds to the 0E1
> and HEA; methods of the H11$ protocol.
>+
pu%lic class SimpleServlet extends HttpServlet =
pu)lic void do'et(%ttpServlete!uest re!" %ttpServletesponse res)
throws ServletException- IOException =
++ set header field first
res*setContentType(+text,html+)-
++ then !et the writer and write the response data
&rint$riter out . res*get$riter()-
out.println7BCHEA;DC1I1LED SimpleServlet OutputC+1I1LEDC+HEA;DC<O;ADB83
out.println7BCh1D SimpleServlet Output C+h1DB83
out.println7BC$D1his is output is from SimpleServlet.B83
out.println7BC+<O;ADB83
out*close()-
@
pu%lic Strin! !etServletInfo78 =
return BA simple servletB3
@
@
1he example a%ove shows the code for the entire servlet. 1he doGet method- %ecause it is returnin!
text to the client- uses the HttpServletResponse5s get/riter method. It sets the response
header field- content type- %efore writin! the %ody of the response- and closes the writer after writin!
the response.
In addition to doGet- there is a second method- getServletIn.o. #ore information on the
getServletIn.o method appears in later section. <ecause this servlet is an example shipped with
the release- it is already compiled. 1o try the servlet- run it in the servletrunner section.
Example of an %TT& Servlet that handles the &OST method
1he followin! example processes data $OS1ed %y a form. 1he form loo2s li2e this:
ChtmlD
CheadDCtitleD.dcSurveyC+titleDC+headD
C%odyD
Cform actionEhttp:++demo:FGFG+servlet+survey methodE$OS1D
Cinput typeEhidden nameEsurvey valueESurveyG1'esultsD
C<'DC<'DHow #any Employees in your "ompanyHC<'D
C<'D1,1GGCinput typeEradio nameEemployee valueE1,1GGD
C<'D1GG,2GGCinput typeEradio nameEemployee valueE1GG,2GGD
C<'D2GG,&GGCinput typeEradio nameEemployee valueE2GG,&GGD
C<'D&GG,(GGCinput typeEradio nameEemployee valueE&GG,(GGD
C<'D)GG,moreCinput typeEradio nameEemployee valueE)GG,moreD
C<'DC<'D0eneral "ommentsHC<'D
C<'DCinput typeEtext nameEcommentD
C<'DC<'Dhat I;Es do you useHC<'D
C<'D.avaor2ShopCinput typeEchec2%ox nameEide valueE.avaor2ShopD
C<'D.IICinput typeEchec2%ox nameEide valueE.IID
C<'D"afe5Cinput typeEchec2%ox nameEide valueE"afe5D
C<'DC<'DCinput typeEsu%mitDCinput typeEresetD
C+formD
C+%odyD
C+htmlD
1he servlet writes the form data to a file- and responds to the user with a than2 you messa!e. 1he
doost method of the servlet loo2s li2e this:
+>>
> rite survey results to output file in response to the $OS1ed
> form. rite a Bthan2 youB to the client.
>+
pu)lic void do&ost(%ttpServlete!uest re!" %ttpServletesponse res)
throws ServletException- IOException =
++ first- set the Bcontent typeB header of the response
res*setContentType(+text,html+)-
++0et the response5s $rintriter to return text to the client.
&rint$riter toClient . res*get$riter()-
try =
++Open the file for writin! the survey results.
String survey/ame . re!*get&arameter0alues(+survey+)123-
/ileriter results/ile E new /ileriter7results;ir
I System.!et$roperty7Bfile.separatorB8
I survey?ame I B.txtB- true83
$rintriter to/ile E new $rintriter7results/ile83
++ 0et client5s form data J store it in the file
to/ile.println7BC<E0I?DB83
Enumeration values . re!*get&arameter/ames()-
while7values.has#oreElements788 =
Strin! name E 7Strin!8values.nextElement783
String value . re!*get&arameter0alues(name)123-
if7name.compare1o7Bsu%mitB8 KE G8 =
to/ile.println7name I B: B I value83
@
@
to/ile.println7BCE?;DB83
++"lose the file.
results/ile.close783
++ 'espond to client with a than2 you
to"lient.println7BChtmlDB83
to"lient.println7BCtitleD1han2 youKC+titleDB83
to"lient.println7B1han2 you for participatin!B83
to"lient.println7BC+htmlDB83
@ catch7IOException e8 =
e.printStac21race783
to"lient.println7
BA pro%lem occured while recordin! your answers. B
I B$lease try a!ain.B83
@
++ "lose the writer3 the response is done.
toClient*close()-
@
1he doost method uses the getarameter-ames and getarameter,alues methods to !et
the form data. <ecause it returns text to the client- doost calls the get/riter method. It sets the
sets the response header field- content type- %efore writin! the %ody of the response- and closes the
writer when the response is complete.
#ifecycle 4ethods
Servlets that mana!e resources do so %y overridin! the lifecycle methods init and destroy. 1hese
servlets mi!ht need to %e !iven ar!uments at startup- in order to initiali4e correctly.
Overriding the Init 4ethod
;urin! initiali4ation- the servlet should prepare the resources it mana!es- to ready the servlet for
acceptin! service re*uests. It can do this without re!ard for multi,threadin! concerns- since there is
only a sin!le thread runnin! on %ehalf of the servlet durin! initiali4ation. As soon as the init method
returns- the servlet can receive client re*uests. If- for some reason- the servlet5s re*uired resources can
not %e made availa%le 7for example- a re*uired networ2 connection can not %e esta%lished8- or some
other initiali4ation error occurs that would ma2e it impossi%le for the servlet to handle re*uests- the
init method should throw an 'navaila0le%xception exception.
1he init method ta2es a Servlet1on.ig o%6ect as a parameter. 1he method should save this
o%6ect- so that it can %e returned %y the getServlet1on.ig method. 1he simplest way to do this is
to have the new init method call super.init. If you do not do this- you should store the
Servlet"onfi! o%6ect yourself- and override the getServlet1on.ig method so that it can o%tain the
o%6ect from its new location.
An example init method follows. It is the init method from the Survey Servlet- which accepts
input from a form and stores it in a file. In order store the survey information- it needs a directory. It
receives the directory as an initiali4ation parameter3 initiali4ation parameters are discussed in the next
section.
pu%lic void init(ServletConfig config)
throws ServletException
=
super*init(config)-
++Store the directory that will hold the survey,results files
results;ir E !etInit$arameter7Bresults;irB83
++If no directory was provided- can5t handle clients
if 7results;ir EE null8 =
throw new 5navaila)leException (this"
+/ot given a directory to write survey results6+)-
@
@
As you can see- this init method calls the super.init method to mana!e the Servlet"onfi!
o%6ect. 1he init method also sets a field- results!ir- with the directory name that is provided as
an initiali4ation parameter. If no directory name was provided- the servlet throws an unavaila%le
exception. If the init method completes successfully- the servlet can then handle client re*uests.
Initiali7ation &arameters
1he specification of initiali4ation parameters is server,specific. /or example- they are specified with a
property when a servlet is run with the servlet runner. 1his tutorial contains a !eneral explanation of
properties- and how to create them.
However the initiali4ation parameters are specified- they are always o%tained the same way: with the
getInitarameter method. 1his method ta2es the parameter name as an ar!ument. 1he example
init method calls getInitarameter. If- for some reason- you need to !et the parameter names-
you can !et them with the getarameter-ames method.
Overriding the Destroy 4ethod
hen a server unloads a servlet- it calls the servlet5s destroy method. 1he destroy method should
undo any initiali4ation wor2 and synchroni4e persistent state with the current in,memory state. 1his
section %e!ins with a description of how to write a simple destroy method- then descri%es how to
structure a servlet if threads runnin! its service method mi!ht still %e runnin! when the destroy
method is called.
1hou!h it is often the case that a servlet that overrides the init method must also override the
destroy method to undo that initiali4ation- this is not re*uired. <ecause initiali4ation involves
readin! a file and usin! its contents to initiali4e a shared data structure- there is no wor2 to undo when
the server is finished with the servlet.
/or many servlets- however- there is initiali4ation wor2 that must %e undone. /or example- assume
there is a servlet that opens a data%ase connection durin! initiali4ation. Its destroy method- shown as
an example %elow- would close that connection.
+>>
> "leans up data%ase connection
>+
pu%lic void destroy78 =
try =
con*close()-
@ catch 7SLLException e8 =
while7e KE null8 =
lo!7BSLLException: B I e.!etSLLState78 I 5Mt5 I
e.!et#essa!e78 I 5Mt5 I
e.!etError"ode78 I 5Mt583
e E e.!et?extException783
@
@ catch 7Exception e8 =
e.printStac21race783
@
@
Coping with Service Threads at Servlet Termination
hen a server removes a servlet- it typically calls destroy after all service calls have %een completed-
or a server,specific num%er of seconds have passed- whichever comes first. If your servlet has
operations that ta2e a lon! time to run 7that is- they may run lon!er than the server5s !race period8- then
threads could still %e runnin! when destroy is called. 1he servlet writer is responsi%le for ma2in! sure
that any threads still handlin! client re*uests complete. 1he remainder of this section descri%es a
techni*ue for doin! this.
A servlet with potentially lon!,runnin! service re*uests should 2eep trac2 of how many service
methods are currently runnin!. Its lon!,runnin! methods should periodically poll to ma2e sure that they
should continue to run. If the servlet is %ein! destroyed- then the lon!,runnin! method should stop
wor2in!- clean up if necessary- and return.
/or example- the instance varia%le that counts the num%er of service methods runnin! could %e
called service1ounter- and the indicator of whether the servlet is %ein! destroyed could %e an
instance varia%le called shutting!o2n. Each varia%le should have its own set of access methods:
pu%lic ShutdownExample extends HttpServlet =
private int service"ounter E G3
private <oolean shuttin!;own3
...
++Access methods for service"ounter
protected synchroni4ed void enterin!Service#ethod78 =
service"ounterII3
@
protected synchroni4ed void leavin!Service#ethod78 =
service"ounter,,3
@
protected synchroni4ed int numServices78 =
return service"ounter3
@
++Access methods for shuttin!;own
protected setShuttin!;own7<oolean fla!8 =
shuttin!;own E fla!3
@
protected <oolean isShuttin!;own78 =
return shuttin!;own3
@
@
1he service method should increment the service counter each time it is entered and decrement it
each time it returns:
protected void service7HttpServlet'e*uest re*- HttpServlet'esponse resp8
throws ServletException- IOException
=
enterin!Service#ethod783
try =
super.service7re*- resp83
@ finally =
leavin!Service#ethod783
@
@
1he destroy method should chec2 the service1ounter- and if there are any lon!,runnin!
methods- set the shutting!o2n varia%le. 1his varia%le will let the threads still handlin! client
re*uests 2now that it is time to shut down. 1he destroy method should then wait for the service
methods to complete- in order to provide a clean shutdown.
pu%lic void destroy78 =
+> "hec2 to see whether there are still service methods runnin!-
> and if there are- tell them to stop. >+
if 7numServices78 D G8 =
setShuttin!;own7true83
@
+> ait for the service methods to stop. >+
while7numService78 D G8 =
try =
this1hread.sleep7interval83
@ catch 7InterruptedException e8 =
@
@
@
&roviding Information a)out the Servlet
Some applets and applications- for example- the .ava e% Server Administration 1ool- display
information a%out a servlet. 1his information can include a short description of the purpose of the
servlet- its author- and perhaps its version num%er. 1he Servlet A$I provides a method to return this
information- getServletIn.o. <y default- this method returns null. hile servlet writers are not
re*uired to override this method- it is stron!ly recommended. 1he simple servlet- shown as an example
earlier- overrides this method:
+>>
> 1his is a simple example of an H11$ Servlet. It responds to the 0E1
> and HEA; methods of the H11$ protocol.
>+
pu%lic class SimpleServlet extends HttpServlet =
...
pu)lic String getServletInfo() =
return BA simple servletB3
@
@
%ow to use servletrunner to un a Servlet
Once you have written your servlet- you can run it in many we% servers- or in the servletrunner.
here ever you decide to run your servlet- there are certain pieces of data that you mi!ht want or need
to specify. hen you are usin! servletrunner you do this with properties. 1he next section
descri%es a servlet5s properties- and how to store them. /ollowin! that- there is a section on how to run
servlets in servletrunner.
&roperties
$roperties are 2ey,value pairs- used for the confi!uration- creation- and initiali4ation of a servlet. /or
example- servlet.#ssignment.code3#ssignmentServlet is a property whose 2ey is
servlet.#ssignment.code and whose value is #ssignmentServlet.
1here are two properties for servlets. One is servlet.name.code- whose value is the servlet5s
class name. 1he other property is servlet.name.initargs- whose value holds the initiali4ation
parameters for the servlet. <oth properties- servlet. name .code and
servlet. name .initargs- are discussed in more detail %elow.
5sing the code &roperty
1he servlet.name.code property names your servlet %y associatin! its name with its class. If
your servlet uses initiali4ation parameters- this property is re*uired. It allows the server to associate the
servlet o%6ect with its initiali4ation ar!uments: they %oth have the same name. Even if your servlet does
not use initiali4ation parameters- it is recommended that it have this property- so that clients can access
the servlet usin! its name.
Syntax of the Initargs &roperty
1he value of the servlet.name.initAr!s property holds the servlet5s initiali4ation parameters. 1he syntax
of a sin!le parameter is parameterNameEparameterValue. 1he entire property 7the entire 2ey,value
pair8 must %e a sin!le lo!ical line. /or reada%ility- you can use the %ac2*uote syntax to allow the
property to span multiple lines in the file. /or example- the ar!ument to the Assi!nment servlet loo2s
li2e this:
servlet.Assi!nment.initAr!sEM
Assi!nment$arameterE$arameterNalue
The &roperty 8ile
$roperties are stored in a file that is- %y default- called Bservlet.propertiesB- thou!h you can specify
another name when servletrunner is started. 1he file should hold the properties for all the servlets
that servletrunner will run. It should %e plain text. Aou can create it in an editor.
5sing Servlet unner
If you would li2e to run your servlet in a we% server- please see that server5s documentation for
instructions. 1his section explains how to run the servlet in the servletrunner utility that comes
with this release.
1he servletrunner is a small utility- intended for testin!. It is multithreaded- so it can run more
than one servlet. It can %e used- therefore- to run multiple servlets simultaneously- or to test one servlet
that calls other servlets in order to satisfy client re*uests. 9nli2e some we% servers- it does not
automatically reload servlets when they are updated. <ecause it is small- however- there is very little
overhead associated with stoppin! and restartin! it in order to use a new version of a servlet.
1he servletrunner is in the 45!6780in directory. Invo2in! it with the ,help fla! shows a usa!e
messa!e without runnin! it:
O .+%in+servletrunner ,help
9sa!e: servletrunner PoptionsQ
Options:
,p port the port num%er to listen on
,% %ac2lo! the listen %ac2lo!
,m max maximum num%er of connection handlers
,t timeout connection timeout in milliseconds
,d dir servlet directory
,r root document root directory
,s filename servlet property file name
,v ver%ose output
O
In order to see the default values of these options- you can call servletrunner with the ,v switch.
1his will- however- start the servlet runner. .ust stop it after you have o%tained the information- if you
are not ready to run it yet- or want it to run with somethin! other than the default values.
Suppose we choose port )G)G. 1hen we can start servletrunner as follows.
.+%in+servletrunner ,p )G)G ,d . ,r . J
Once the servletrunner is executin!- you run servlets %y callin! them directly in your %rowser- or as the
forms example shows- %y usin! a form that calls a servlet to process its data. 1he 9'L for a servlet has
the followin! !eneral form:
http:++machine-name:port+servlet+servlet-name
where servlet-name corresponds to the name you have !iven your servlet. /or example- to run the
Assi!nment Servlet- which has the property
servlet.#ssignment.code3#ssignmentServlet- you would use the followin! 9'L. 7It
assumes that servletrunner is runnin! on machine7info*cse*iit)*ac*in8- at port )G)G- and that the
Assi!nment servlet is located in the servlet directory provided to servletrunner at startup:
http:++info.cse.iit%.ac.in:)G)G+servlet+Assi!nment
Examples
1. Example1 source
2. Example2 source
&. Example Session Servlet source
#earning more a)out servlets
'ead more a%out servlet features ;etailed Servlet 1utorial and "reatin! sessions with servlets
eferences
P1Q http:++www.cs.%ham.ac.u2+Rtmw+servletStutorial+
P2Q http:++scitec.uwichill.edu.%%+cmp+online+"S&TL+wee2T+servletsSnext.htm
P&Q http:++www.apl.6hu.edu+Rhall+6ava+Servlet,1utorial+index.html
P(Q http:++6ava.sun.com+docs+%oo2s+tutorial+servlets+1O".html

You might also like