how to install apache tomcat 8

2
How to Install Apache Tomcat 8 (on Windows, Mac OS X, Ubuntu) and et Sta!ted with  "a#a Se!# le t $!o%!amm in %   This p!act ical can be c ompleted in a &'hou! ses sion  This installation and con%u!ation %uide is applicabl e to Tomcat * and 8, and possibl+ the ea!lie! #e!sions  T ae note that T omcat 8 !e-ui! es "./ 0* It will 1OT wo! with "./ 02 I3 +ou! "./ is below 0*, up%!ade it (See "./ How'To) 4ou can chec +ou! "./ #e!sion #ia command 5  6a#ac '#e!sion5 1. Introduction 1.1 Web Application (Webapp) A web application (o! webapp), unlie standalone application, !uns o#e! the Inte!net 7amples o3 webapps a!e %oo%le, ama9on, eba+, 3aceboo and twitte! A webapp is t+picall+ a 3-tier  (o! multi-tier ) client-server database application !un o#e! the Inte!net as illust!ated in the dia%!am below It comp!ises #e components: 0 HTTP Server : 7%, Apache HTT$ Se!#e!, Apache Tomcat Se!#e!, Mic!oso3t Inte!net In3o!mation Se!#e! (IIS), n%in, oo%le Web Se!#e! (WS), and othe!s ; HTTP Client (or Web Browser) : 7%, Inte!net 7plo!e! (MSI7), <i!e<o, =h!ome, Sa3a!i, and othe!s & Database : 7 % , Op en'sou!ce M+ S>?, Ap ac he .e !b +, mS >?, S>?it e, $ost% !eS>?, OpenO@ces BaseC =omme!cial O!acle, IBM .B;, SA$ S+Base, MS S>? Se!#e!, MS AccessC and othe!s D Client Side Pro!ra"s: co ul d be w! it ten in HT M? <o!m, "a #aSc !i pt, EBSc!ipt, <lash, and othe!s F Server Side Pro!ra"s : could be w!itten in "a#a Se!#letG"S$, AS$, $H$, $e!l , $+thon, =I, and othe!s

Upload: sachin-kuchhal

Post on 06-Jul-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 1/52

How to Install Apache Tomcat8 (on Windows, Mac OS X,

Ubuntu) and et Sta!ted with "a#a Se!#let $!o%!ammin% 

 This p!actical can be completed in a &'hou! session

 This installation and con%u!ation %uide is applicable to Tomcat * and 8, and possibl+

the ea!lie! #e!sions

 Tae note that Tomcat 8 !e-ui!es "./ 0* It will 1OT wo! with "./ 02 I3 +ou! "./ is

below 0*, up%!ade it (See "./ How'To) 4ou can chec +ou! "./ #e!sion #ia command

5 6a#ac '#e!sion5

1. Introduction

1.1 Web Application (Webapp)

A web application (o! webapp), unlie standalone application, !uns o#e! the Inte!net

7amples o3 webapps a!e %oo%le, ama9on, eba+, 3aceboo and twitte!

A webapp is t+picall+ a 3-tier  (o! multi-tier ) client-server database application !un o#e!

the Inte!net as illust!ated in the dia%!am below It comp!ises #e components:0 HTTP Server: 7%, Apache HTT$ Se!#e!, Apache Tomcat Se!#e!, Mic!oso3t

Inte!net In3o!mation Se!#e! (IIS), n%in, oo%le Web Se!#e! (WS), and othe!s

; HTTP Client (or Web Browser) : 7%, Inte!net 7plo!e! (MSI7), <i!e<o,

=h!ome, Sa3a!i, and othe!s

& Database: 7%, Open'sou!ce M+S>?, Apache .e!b+, mS>?, S>?ite,

$ost%!eS>?, OpenO@ces BaseC =omme!cial O!acle, IBM .B;, SA$ S+Base, MS S>?

Se!#e!, MS AccessC and othe!s

D ClientSide Pro!ra"s: could be w!itten in HTM? <o!m, "a#aSc!ipt,

EBSc!ipt, <lash, and othe!s

F ServerSide Pro!ra"s: could be w!itten in "a#a Se!#letG"S$, AS$, $H$, $e!l,

$+thon, =I, and othe!s

Page 2: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 2/52

 The t+pical use'case is:

0 A use!, #ia a web b!owse! (HTT$ client), issues a U? !e-uest to an HTT$ se!#e!

to sta!t a webapp

; A client'side p!o%!am (such as an HTM? 3o!m) is loaded into clients b!owse!

& The use! lls up the -ue!+ c!ite!ia in the 3o!m

D The client'side p!o%!am sends the -ue!+ pa!amete!s to a se!#e!'side p!o%!am

F The se!#e!'side p!o%!am !ecei#es the -ue!+ pa!amete!s, -ue!ies the database

and !etu!ns the -ue!+ !esult to the client

2 The client'side p!o%!am displa+s the -ue!+ !esult on the b!owse!

* The p!ocess !epeats

1.# H$perte%t Trans&er Protocol (HTTP)

• HTT$ is an application layer  p!otocol !uns o#e! T=$GI$ The I$ p!o#ides suppo!t 3o!

!outin% and add!essin% (#ia an uni-ue I$ add!ess 3o! machines on the Inte!net)C

while T=$ suppo!ts multiplein% #ia 2D/ po!ts 3!om po!t numbe! to 2FF&F The

de3ault po!t numbe! assi%ned to HTT$ is T=$ po!t 8

• HTT$ is an asynchronous request-response application-layer protocol A client

sends a !e-uest messa%e to the se!#e! The se!#e! then !etu!ns a !esponse

messa%e to the client

Page 3: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 3/52

• HTT$ is a pull p!otocol, a client pulls a pa%e 3!om the se!#e! (instead o3 se!#e!

pushes pa%es to the clients)

•  The s+nta o3 the messa%e is dened in the HTT$ specication

1.' Apace To"cat HTTP Server

 Apache Tomcat  is a "a#a'capable HTT$ se!#e!, which could eecute special "a#ap!o%!ams nown as "a#a Se!#let and "a#a Se!#e! $a%es ("S$) It is the o@cial Reference

Implementation (RI) 3o! "a#a Se!#lets and "a#aSe!#e! $a%es ("S$) technolo%ies Tomcat is

an open-source p!o6ect, unde! the 5Apache So3twa!e <oundation5 (which also p!o#ides

the most use, open'sou!ce, indust!ial'st!en%th Apache HTT$ Se!#e!) The mothe! site 3o!

 Tomcat is http:GGtomcatapacheo!% Alte!nati#el+, +ou can nd tomcat #ia the Apache

mothe! site J http:GGwwwapacheo!%

 Tomcat was o!i%inall+ w!itten b+ "ames .uncan .a#ison (then wo!in% in Sun), in 0KK8,

based on an ea!lie! Suns se!#e! called "a#a Web Se!#e! ("WS) It be%an at #e!sion &

a3te! "SW./ ;0 it !eplaced Sun subse-uentl+ made Tomcat open'sou!ce and %a#e it to

Apache

 The #a!ious Tomcat !eleases a!e:

0 Tomcat & (0KKK): I 3o! Se!#let ;; and "S$ 00

; Tomcat D (;0): I 3o! Se!#let ;& and "S$ 0;

& Tomcat F (;;): I 3o! Se!#let ;D and "S$ ;

Page 4: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 4/52

D Tomcat 2 (;2): I 3o! Se!#let ;F and "S$ ;0

F Tomcat * (;0): I 3o! Se!#let &, "S$ ;; and 7? ;;

2 Tomcat 8 (;0&): I 3o! Se!#let &0, "S$ ;&, 7? & and "a#a WebSocet 0

 Tomcat is an HTT$ application !uns o#e! T=$GI$ In othe! wo!ds, the Tomcat se!#e! !uns

on a specic T=$ po!t in a specic I$ add!ess The de3ault T=$ po!t numbe! 3o! HTT$

p!otocol is 8, which is used 3o! the production HTT$ se!#e! <o! test  HTT$ se!#e!, +ou

can choose an+ unused po!t numbe! between 0;D and 2FF&F

#. How to Install To"cat and *et Startedwit +ava Servlet Pro!ra""in!

#.1 ST,P 1- Download and Install To"cat

1OT7: At the time o3 w!itin%, Tomcat K is at the alpha sta%e, not stable !elease We shall

install Tomcat 8&

or Windows

0 oto http:GGtomcatapacheo!%  L .ownloads L Tomcat 8 L 58 xx N5

(whe!e  xx N is the latest up%!ade numbe!) L Bina!+ .ist!ibutions L =o!e L

5/IP5 paca%e (e%, 5apache'tomcat'8 xx N0ip5, about 8 MB)

; =!eate +ou! p!o6ect di!ecto!+, sa+ 5d:m+$!o6ect5 o! 5c:m+$!o6ect5 U1PI$ the

downloaded le into +ou! p!o6ect di!ecto!+ Tomcat will be un9ipped into

di!ecto!+ 5d:m+$!o6ectapache'tomcat'8 xx N5

& <o! ease o& use, we shall sho!ten and !ename this di!ecto!+ to

5d:m+$!o6ecttomcat5

Tae note o& 2our To"cat Installed Director$ He!ea3te!, I shall !e3e! to the Tomcatinstalled di!ecto!+ as QTOM=ATRHOM7

or 3ac 4S 5

0 oto http:GGtomcatapacheo!%  L .ownload L Tomcat 8 L 58 xx N5

(whe!e  xx N denotes the latest up%!ade numbe!) L Bina!+ dist!ibution L

=o!e L 5tar.!05 paca%e (e%, 5apache'tomcat'8 xx Ntar.!05, about 8 MB)

; To install Tomcat:

0 oto 5G.ownloads5, double'clic the downloaded ta!ball (e%,

5apache'tomcat'8 xx Nta!%95) to epand it into a 3olde! (e%,

5apache'tomcat'8 xx N5); Mo#e the et!acted 3olde! (e%, 5apache'tomcat'8 xx N5) to

5GApplications5

& <o! ease o& use, we shall sho!ten and !ename this 3olde! to 5tomcat5

Tae note o& 2our To"cat Installed Director$ He!ea3te!, I shall !e3e! to the Tomcat

installed di!ecto!+ as QTOM=ATRHOM7

or 6buntu

Page 5: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 5/52

ead 5How to Install Tomcat 8 on Ubuntu5 4ou need to switch between these two

a!ticles

<o! academic lea!nin%, I !ecommend 59ip5 (o! 5ta!%95) #e!sion, as +ou could simpl+

delete the enti!e di!ecto!+ when Tomcat is no lon%e! needed (without !unnin% an+ un'

installe!) 4ou a!e 3!ee to mo#e o! !ename the Tomcats installed di!ecto!+ 4ou can

install (un9ip) multiple copies o3 Tomcat in the same machine <o! p!oduction, it is easie!

to use the installe! to p!ope!l+ con%u!e the TomcatTo"cat7s Directories

 Tae a -uic loo at the Tomcat installed di!ecto!+ It contains the 3ollowin% sub'

di!ecto!ies:

• bin: contains the bina!iesC and sta!tup sc!ipt (sta!tupbat 3o! Windows

and sta!tupsh 3o! Unies and Mac OS X), shutdown sc!ipt (shutdownbat 3o! Windows

and shutdownsh 3o! Uni and Mac OS X), and othe! bina!ies and sc!ipts

• con& : contains the s+stem'wide con%u!ation les, such

as se!#e!ml, webml, contetml, and tomcat'use!sml

• lib : contains the Tomcats s+stem'wide "A les, accessible b+ all webapps 4ou

could also place ete!nal "A le (such as M+S>? ".B= .!i#e!) he!e

• lo!s: contains Tomcats lo% les 4ou ma+ need to chec 3o! e!!o! messa%es

he!e

• webapps: contains the webapps to be deplo+ed 4ou can also place the WA

(Webapp A!chi#e) le 3o! deplo+ment he!e

• wor : Tomcats wo!in% di!ecto!+ used b+ "S$, 3o! "S$'to'Se!#let con#e!sion

• te"p: Tempo!a!+ les

#.# ST,P #- Create an ,nviron"ent8ariable +A8A9H43,

(or Windows)

 4ou need to c!eate an environment variable called 5 "AEARHOM75 and set it to +ou! "./ 

installed di!ecto!+

0 <i!st, nd +ou! "./ installed di!ecto!+ The de3ault is 5c:$!o%!am

<iles"a#a6d08R xx N5, whe!e  xx N is the up%!ade numbe! Tae note o3 

+ou! "./ installed di!ecto!+

; To set the en#i!onment #a!iable "AEARHOM7 in Windows *G8G0: Sta!t 5=ont!ol

$anel5 L S+stem L Ad#anced s+stem settin%s L Switch to 5Ad#anced5 tab L

7n#i!onment Ea!iables L S+stem Ea!iables L 51ew5 L In 5Ea!iable 1ame5,

ente! 5 "AEARHOM75 L In 5Ea!iable Ealue5, ente! +ou! "./ installed di!ecto!+ as

noted in Step 0

& To #e!i3+, :,STA:T a =M. shell (!esta!t needed to !e3!esh the en#i!onment)

and issue:

D S,T +A8A9H43,

Page 6: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 6/52

 "AEARHOM7c:$!o%!am <iles"a#a6d08R xx N Q Ee!i3+ that this is 4OU

 "./ installed di!ecto!+

(or 3ac 4S 5)

Sip this step 1o need to do an+thin%

#.' ST,P '- Con;!ure To"cat Server

 The Tomcat con%u!ation les a!e located in the 5con3 5 sub'di!ecto!+ o3 +ou! Tomcat

installed di!ecto!+, e% 5d:m+$!o6ecttomcatcon3 5 (3o! Windows) o!

5GApplicationsGtomcatGcon3 5 (3o! Mac OS X) The!e a!e D con%u!ation XM? les:

0 se!#e!ml

; webml

& contetml

D tomcat'use!sml

Mae a BA=/U$ o3 the con%u!ation les be3o!e +ou p!oceed

Step '(a) <con&=server.%"l< Set te TCP Port >u"ber

Use a p!o%!ammin% tet edito! (e%, 1ote$adVV, Tet$ad 3o! WindowsC o! %7dit, 67dit

3o! Mac OS X) to open the con%u!ation le 5se!#e!ml5, unde! the 5con3 5 sub'di!ecto!+

o3 Tomcat installed di!ecto!+

 The de3ault T=$ po!t numbe! con%u!ed in Tomcat is 88, +ou ma+ choose an+ numbe!

between 0;D and 2FF&F, which is not used b+ an eistin% application We shall choose

KKKK in this a!ticle (<o! p!oduction se!#e!, +ou should use po!t 8, which is p!e'

assi%ned to HTT$ se!#e! as the de3ault po!t numbe!)

?ocate the 3ollowin% lines (a!ound ?ine 2K) that dene the HTT$ connecto!, andchan%e po!t5885 to po!t5KKKK5

Q'' A 5=onnecto!5 !ep!esents an endpoint b+ which !e-uests a!e !ecei#ed

  and !esponses a!e !etu!ned .ocumentation at :

  "a#a HTT$ =onnecto!: GdocsGcon%Ghttphtml (blocin% non'blocin%)

  "a#a A"$ =onnecto!: GdocsGcon%Ga6phtml

  A$ (HTT$GA"$) =onnecto!: GdocsGap!html

  De;ne a nonSS? [email protected] Connector on port

''

Q=onnecto! po!t5KKKK5 p!otocol5HTT$G005

  connectionTimeout5;5

  !edi!ect$o!t58DD&5 GStep '(b) <con&=web.%"l< ,nablin! Director$ ?istin!

A%ain, use a p!o%!ammin% tet edito! to open the con%u!ation le 5 webml5, unde! the

5con3 5 sub'di!ecto!+ o3 Tomcat installed di!ecto!+

We shall enable di!ecto!+ listin% b+ chan%in% 5 listin%s5 3!om 53alse5 to 5t!ue5 3o! the

5de3ault5 se!#let This is hand+ 3o! test s+stem, but not 3o! p!oduction s+stem 3o!

secu!it+ !easons

Page 7: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 7/52

?ocate the 3ollowin% lines (a!ound ?ine 0&) that dene the 5de3ault5 se!#letC and

chan%e the 5listin%s5 3!om 53alse5 to 5t!ue5

Q'' The de3ault se!#let 3o! all web applications, that se!#es static ''

Q'' !esou!ces It p!ocesses all !e-uests that a!e not mapped to othe! ''

Q'' se!#lets with se!#let mappin%s ''

Qse!#let  Qse!#let'namede&aultQGse!#let'name

  Qse!#let'classo!%apachecatalinase!#lets.e3aultSe!#letQGse!#let'class

  Qinit'pa!am

  Qpa!am'namedebu%QGpa!am'name

  Qpa!am'#alueQGpa!am'#alue

  QGinit'pa!am

  Qinit'pa!am

  Qpa!am'namelistin!sQGpa!am'name

  Qpa!am'#aluetrueQGpa!am'#alue

  QGinit'pa!am

  Qload'on'sta!tup0QGload'on'sta!tupQGse!#let

Step '(c) <con&=conte%t.%"l< ,nablin! Auto"atic :eload

We shall add the att!ibute !eloadable5t!ue5 to the Q=ontet element to enable

automatic !eload a3te! code chan%es A%ain, this is hand+ 3o! test s+stem but not 3o!

p!oduction, due to the o#e!head o3 detectin% chan%es

?ocate the Q=ontet sta!t element (a!ound ?ine 0K), and chan%e it to Q=ontet

!eloadable5t!ue5

Q=ontet reloadable<true<

 

  QG=ontet

Step '(d) (4ptional) <con&=to"catusers.%"l<

7nable the Tomcats mana%e! b+ addin% the hi%hli%hted lines, inside the Qtomcat'

use!s elements:

Qtomcat'use!s

  Q!ole !olename5mana%e!'%ui5G

  Quse! use!name5mana%e!5 passwo!d55 !oles5mana%e!'%ui5G

QGtomcat'use!s

 This enables the mana%e! UI app 3o! mana%in% Tomcat se!#e!

#. ST,P - Start To"cat Server

 The Tomcats eecutable p!o%!ams and sc!ipts a!e ept in the 5bin5 sub'di!ecto!+ o3 the

 Tomcat installed di!ecto!+, e%, 5d:m+$!o6ecttomcatbin5 (3o! Windows) o!

5GApplicationsGtomcatGbin5 (3o! Mac OS X)Step (a) Start Server

Page 8: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 8/52

or Windows

?aunch a =M. shell Set the cu!!ent di!ecto!+ to 5QTOM=ATRHOM7bin5, and !un

5sta!tupbat5 as 3ollows:

GG =han%e the cu!!ent di!ecto!+ to Tomcats 5bin5

GG Assume that Tomcat is installed in 5d:m+$!o6ecttomcat5d-  GG =han%e the cu!!ent d!i#e

cd ="$Proect=to"cat=bin  GG =han%e .i!ecto!+ to 4OU Tomcats 5bin5 di!ecto!+

 

GG Sta!t Tomcat Se!#e!

startup

or 3ac 4S 5

I assume that Tomcat is installed in 5GApplicationsGtomcat5 To sta!t the Tomcat se!#e!,

open a new 5Te!minal5 and issue:

GG =han%e cu!!ent di!ecto!+ to Tomcats bina!+ di!ecto!+

cd @Applications@to"cat@bin 

GG Sta!t tomcat se!#e!

[email protected] run

A new Tomcat console window appea!s Stud+ the messa%es on the console ?oo out

3o! the Tomcats po!t numbe! (double chec that Tomcat is !unnin% on po!t KKKK) <utu!e

e!!o! messa%es will be send to this console S+stemoutp!intln() issued b+ +ou! "a#a

se!#lets will also be sent to this console

, :: o!%apacheco+oteAbst!act$!otocol sta!t

I1<O: Sta!tin% $!otocolHandle! Y<ttpbioEEEE<Z

, :: o!%apacheco+oteAbst!act$!otocol sta!t

I1<O: Sta!tin% $!otocolHandle! Y5a6p'bio'8K5Z

, :: o!%apachecatalinasta!tup=atalina sta!t

I1<O: Server startup in ;D** ms

(Sip 6nless ...) Cannot Start To"cat-  ead 5How to .ebu%5

Step (b) Start a Client to Access te Server

Sta!t a b!owse! (as HTT$ client) Issue U? 5http:GGlocalhost:KKKK5 to access the Tomcat

se!#e!s welcome pa%e The hostname 5localhost5 (with I$ add!ess o3 0;*0) is meant

3o! local loop'bac testin% inside the same machine <o! use!s on the othe! machines

o#e! the net, the+ ha#e to use the se!#e!s I$ add!ess o! .1S domain name o!

hostname in the 3o!mat o3 5http:GGserverHostnameOrIAddress:KKKK5

Page 9: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 9/52

 T!+ issuin% U? http:GGlocalhost:KKKKGeamples to #iew the se!#let and "S$ eamples T!+!unnin% some o3 the se!#let eamples

(Optional) T!+ issuin% U? http:GGlocalhost:KKKKGmana%e!Ghtml to !un the Tomcat Web

Mana%e! 7nte! the use!name and passwo!d con%u!ed ea!lie! in tomcat'use!smlStep (c) Sutdown Server

or Windows

 4ou can shutdown the tomcat se!#e! b+ eithe!:

0 $!ess =t!l'= on the Tomcat consoleC O

; un 5QTOM=ATRHOM7binsutdown.bat5 sc!ipt Open a new 5cmd5 and

issue:

& GG =han%e the cu!!ent di!ecto!+ to Tomcats 5bin5

D d-  GG =han%e the cu!!ent d!i#e

F cd ="$Proect=to"cat=bin  GG =han%e .i!ecto!+ to 4OU Tomcats 5bin5

di!ecto!+

2

* GG Shutdown the se!#e!

8 sutdown

or 3ac 4S 5

 To shutdown the Tomcat se!#e!:0 $!ess =ont!ol'= (1OT =ommand'=) on the Tomcat consoleC O

; un the 5QTOM=ATRHOM7GbinGshutdownsh5 sc!ipt Open a new 5Te!minal5

and issue:

& GG =han%e cu!!ent di!ecto!+ to Tomcats bin di!ecto!+

D cd @Applications@to"cat@bin

F

2 GG Shutdown the se!#e!

[email protected]

WA:>I>*: 4ou MUST p!ope!l+ shutdown the Tomcat .O 1OT ill the cat b+ pushin%the windows 5=?OS75 button

#.F ST,P F- Develop and Deplo$ a WebAppStep F(a) Create te Director$ Structure &or $our WebApp

Page 10: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 10/52

<i!st o3 all, choose a name 3o! +ou! webapp ?ets call it 5hello5 oto Tomcats 5webapps5

sub'di!ecto!+ =!eate the 3ollowin% di!ecto!+ st!uctu!e 3o! +ou webapp 5hello5 (as

illust!ated):

0 Unde! Tomcats 5webapps5, c!eate +ou! webapp root  di!ecto!+ 5hello5 (ie,

5QTOM=ATRHOM7webappshello 5)

; Unde! 5hello5, c!eate a sub'di!ecto!+ 5W7B'I1<5 (case sensiti#e, a 5dash5 not anunde!sco!e) (ie, 5QTOM=ATRHOM7webappshelloW7B'I1<5)

& Unde! 5W7B'I1<5, c!eate a sub'sub'di!ecto!+ 5classes5 (case sensiti#e, plu!al) (ie,

5QTOM=ATRHOM7webappshelloW7B'I1<classes5)

 4ou need to eep +ou! web !esou!ces (e%, HTM?s, =SSs, ima%es, sc!ipts, se!#lets, "S$s)

in the p!ope! di!ecto!ies:

• 5hello5: The is called the context root  (o! document base directory ) o3 +ou!

webapp 4ou should eep all +ou! HTM? les and !esou!ces #isible to the web use!s

(e%, HTM?s, =SSs, ima%es, sc!ipts, "S$s) unde! this context root 

• 5helloGW7B'I1<5: This di!ecto!+, althou%h unde! the contet !oot, is not visible tothe web use!s This is whe!e +ou eep +ou! applications web desc!ipto! le

5webml5

• 5helloGW7B'I1<Gclasses5: This is whe!e +ou eep all the "a#a classes such as

se!#let class'les

 4ou should 7'STAT +ou! Tomcat se!#e! to pic up the hello webapp =hec the

 Tomcats console to con!m that 5hello5 application has been p!ope!l+ depol+ed:

I1<O: .eplo+in% web application di!ecto!+ .:m+$!o6ecttomcatwebappsello

 4ou can issue the 3ollowin% U? to access the web application 5hello5:

http:GGlocalhost:KKKK@ello

 4ou should see the di!ecto!+ listin% o3 the di!ecto!+ 5QTOM=ATRHOM7webappshello 5,

which shall be empt+ (p!o#ided +ou ha#e enabled di!ecto!+ listin% in webml ea!lie!)Step F(b) Write a Welco"e Pa!e

Page 11: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 11/52

=!eate the 3ollowin% HTM? pa%e and sa#e as 5HelloHomehtml5 in +ou! applications !oot

di!ecto!+ 5hello5

0

;

&

D

F

2

Qhtml

  QheadQtitleM+ Home $a%eQGtitleQGhead

  Qbod+

  Qh0M+ 1ame is so and so This is m+ HOM7QGh0

  QGbod+

QGhtml

 4ou can b!owse this pa%e b+ issuin% this U?:

http:GGlocalhost:KKKKGhelloGHelloHo"e.t"l

Alte!nati#el+, +ou can issue an U? to +ou! web application !oot 5hello5:

http:GGlocalhost:KKKKGhello

 The se!#e! will !etu!n the di!ecto!+ listin% o3 +ou! base di!ecto!+ 4ou can then clic on

5HelloHomehtml5

ename 5HelloHomehtml5 to 5indehtml5, and issue a di!ecto!+ !e-uest a%ain:

http:GGlocalhost:KKKKGhello

1ow, the se!#e! will !edi!ect the di!ecto!+ !e-uest to 5 indehtml5, i3 the !oot di!ecto!+

contains an 5indehtml5, instead o3 se!#in% the di!ecto!+ listin%

 4ou can chec out the home pa%e o3 +ou! pee!s b+ issuin%:

http:GG!oureerHostnameOrIAddress:KKKKGhello

http:GG!oureerHostnameOrIAddress:KKKKGhelloGHelloHomehtml

http:GG!oureerHostnameOrIAddress:KKKKGhelloGindehtml

Page 12: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 12/52

with a #alid 5!oureerHostnameOrIAddress5, p!o#ided that +ou! pee! has sta!ted his

tomcat se!#e! and his !ewall does not bloc +ou! access 4ou can use command such

as 5ipcon%5 (Windows), 5i3con%5 (Mac OS X and Uni) to nd +ou! I$ add!ess

(Sip 6nless...) The liel+ e!!o!s a!e 5Unable to =onnect5, 5Inte!net 7plo!e!

cannot displa+ the web pa%e5, and 5DD <ile 1ot <ound5 ead 5How to .ebu%5 section

#.G ST,P G- Write a <Helloworld< +ava Servlet

A servlet  is "a#a p!o%!am that !uns inside a "a#a'capable HTT$ Se!#e!, such as Apache

 Tomcat A web use! in#oes a se!#let b+ issuin% an app!op!iate U? 3!om a web b!owse!

(HTT$ client)

Be3o!e +ou p!oceed, I shall assume that +ou a!e 3amilia! with "a#a $!o%!ammin% and

ha#e installed the 3ollowin%s:

0 "./ (ead 5How to install "./ and et Sta!ted5)

; A p!o%!ammin% tet edito!, such as Tet$ad o! 1otepadVV (3o! Windows)C 67dit o!%7dit (3o! Mac OS X) (ead 5$!o%!ammin% Tet 7dito!5)

Step G(a) Write a <Helloworld< +ava Servlet

A "a#a se!#let is a "a#a p!o%!am that !uns inside a HTT$ se!#e! A web use! in#oes a

se!#let b+ issuin% a U? 3!om a b!owse! (o! HTT$ client)

In this eample, we a!e %oin% to w!ite a "a#a se!#let called HelloSe!#let, which sa+s

5Hello, wo!ld5 We will then w!ite a con%u!ation such that web use!s can in#oe this

se!#let b+ issuin% U?http:GGhostname: port GhelloGsa+hello 3!om thei! b!owse!, as

illust!ated:

W!ite the 3ollowin% sou!ce codes called 5HelloSe!#let6a#a5 and sa#e it unde! +ou!

application 5classes5 di!ecto!+ (ie, 5QTOM=ATRHOM7webappshello W,B

Page 13: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 13/52

I>=classesHelloSe!#let6a#a5) This se!#let sa+s 5Hello5, echos some !e-uest

in3o!mation, and p!ints a !andom numbe! upon each !e-uest

0

;

&

D

F

2

*

8

K

0

00

0;

0&

0D0F

02

0*

08

0K

;

;0

;;

;&

;D

;F;2

;*

;8

;K

&

&0

&;

&&

&D

GG To sa#e as 5QTOM=ATRHOM7webappshelloW7B'I1<classesHelloSe!#let6a#a 5

impo!t 6a#aio[C

impo!t 6a#ase!#let[C

impo!t 6a#ase!#lethttp[C

 

public class HelloServlet etends HttpSe!#let

  JO#e!!ide

  public #oid do*et(HttpSe!#lete-uest reuest, HttpSe!#letesponse response)

  th!ows IO7ception, Se!#let7ception

 

GG Set the !esponse MIM7 t+pe o3 the !esponse messa%e

  response.setContentT$pe(<te%t@t"l<)

  GG Allocate a output w!ite! to w!ite the !esponse messa%e into the netwo! socet

  PrintWriter out response.!etWriter() 

GG W!ite the !esponse messa%e, in an HTM? pa%e

  t!+

  outp!intln(5Qhtml5)C

  outp!intln(5QheadQtitleHello, Wo!ldQGtitleQGhead5)C

  outp!intln(5Qbod+5)C

  outp!intln(5Qh0Hello, wo!ldQGh05)C GG sa+s Hello

  GG 7cho clients !e-uest in3o!mation

  outp!intln(5Qpe-uest UI: 5 V reuest.!et:euest6:I() V 5QGp5)C

  outp!intln(5Qp$!otocol: 5 V reuest.!etProtocol() V 5QGp5)C

  outp!intln(5Qp$athIn3o: 5 V reuest.!etPatIn&o()  V 5QGp5)C  outp!intln(5Qpemote Add!ess: 5 V reuest.!et:e"oteAddr() V 5QGp5)C

  GG ene!ate a !andom numbe! upon each !e-uest

  outp!intln(5QpA andom 1umbe!: Qst!on%5 V 3at.rando"() V 5QGst!on%QGp

  outp!intln(5QGbod+QGhtml5)C

  N nall+

  outclose()C GG Alwa+s close the output w!ite!

  N

  N

N

Step G(b) Co"pilin! te Servlet

We need the Se!#let A$I to compile the se!#let Se!#let A$I is 1OT pa!t o3 "./

1onetheless, Tomcat p!o#ides a cop+ in QTOM=ATRHOM7GlibGse!#let'api6a! We need to

include this "A le in the compilation #ia the 'cp (classpath) option

(or Windows)

GG Assume that Tomcat is installed in d:m+$!o6ecttomcat

GG =han%e di!ecto!+ to the sou!ce le

Page 14: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 14/52

d-

cd ="$Proect=to"cat=webapps=ello=W,BI>=classes

GG =ompile

 avac cp .d-="$Proect=to"cat=lib=servletapi.ar HelloServlet.ava

  GG 1ote: 4ou need to enclose the 6a! le in double -uotes i3 the path contains blan  GG e%, 6a#ac 'cp C5d:$ath Totomcatlibse!#let'api6a!5 HelloSe!#let6a#a

(or 3ac 4S 5)

GG Assume that Tomcat is installed in GApplicationsGtomcat

GG =han%e di!ecto!+ to the sou!ce le

cd @Applications@to"cat@webapps@ello@W,BI>@classes

GG =ompile

 avac cp .-@Applications@to"cat@[email protected] HelloServlet.ava

 The output o3 the compilation is 5HelloSe!#letclass5 B!owse the 5classes5 3olde! to mae

su!e that it is c!eated

(Sip 6nless...) ead 5=ommon 7!!o!s in =ompilin% "a#a Se!#let5

Step G(c) Con;!ure Servlet7s :euest 6:? in<webapps=ello=W,BI>=web.%"l<

A web use! in#oes a se!#let, which is ept in the web se!#e!, b+ issuin% a request 

"R# 3!om the b!owse! We need to con%u!e this !e-uest U? 3o! ou! HelloSe!#let

=!eate the 3ollowin% con%u!ation le called 5web.%"l5, and sa#e it unde!

5webapps=ello=W,BI>5 (ie, 5QTOM=ATRHOM7webappshelloW7B'I1<webml5)

0

;&

D

F

2

*

8

K

0

00

0;

0&

0D

0F

02

0*

08

0K

Q\ml #e!sion505 encodin%5ISO'88FK'05\

Qweb'app #e!sion5&5 mlns5http:GG6a#asuncomGmlGnsG6a#aee5

  mlns:si5http:GGwwww&o!%G;0GXM?Schema'instance5

  si:schema?ocation5http:GG6a#asuncomGmlGnsG6a#aee http:GG6a#asuncomGmlGnsG6a#aee

 

JK To save as <ello=W,BI>=web.%"l< L

 

Qse!#let

  Qse!#let'nameHelloWorldQGse!#let'name

  Qse!#let'classHelloServletQGse!#let'class

  QGse!#let

 

JK >ote- All JservletL ele"ents 36ST be !rouped to!eter and

  placed I> :4>T o& te Jservlet"appin!L ele"ents L

 

Qse!#let'mappin%

  Qse!#let'nameHelloWorldQGse!#let'name

  Qu!l'patte!n@sa$elloQGu!l'patte!n

Page 15: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 15/52

;

;0

  QGse!#let'mappin%

QGweb'app

In the abo#e con%u!ation, a se!#let ha#in% a class le 5HelloSe!#letclass5 is mapped to

!e-uest U? 5Gsa+hello5 (#ia an arbitrary  se!#let'name 5HelloWo!ld5), unde! this web

application 5hello5 In othe! wo!ds, the complete !e-uest U? 3o! this se!#let is5http:GGhostname: port @ello@sa$ello5

 This con%u!ation le, sa#ed unde! +ou! webapp 5hello5, is applicable onl+ to this

pa!ticula! webapp 5hello5

7STAT +ou! Tomcat se!#e! to !e3!esh the 5webml5 le

I3P4:TA>T- <o! 7A=H se!#let, +ou need to w!ite a pai! o3 Qse!#let and Qse!#let'

mappin% elements with a common but a!bit!a!+ Qse!#let'name Tae note that all

the Qse!#let elements MUST be %!ouped to%ethe! and placed I1 <O1T o3 the Qse!#let'

mappin% elementsStep G(d) Invoe te Servlet

 To !un this se!#let, sta!t a b!owse!, and issue the !e-uest U? con%u!ed ea!lie!:

http:GGlocalhost:KKKKGello@sa$ello

 4ou shall see the output o3 the se!#let displa+ed in +ou! web b!owse!

e3!esh the b!owse!, +ou shall see a new !andom numbe! upon each !e3!esh In othe!

wo!d, the doet() method o3 the se!#let !uns once pe! !e-uest

 T!+ 5Eiew Sou!ce5 to loo at the output !ecei#ed b+ the web use!s Tae note that the

web use!s !ecei#e onl+ the output o3 the se!#let (%ene!ated #ia

the outp!intln() statements) The+ ha#e no access to the se!#let p!o%!ams (which ma+

contain condential in3o!mation)

(<o! Mac OS Xs Sa3a!i b!owse!) 4ou need to enable 5.e#elope! Menu5 unde! the

5$!e3e!ences5 to enable the 5Eiew Sou!ce5 menu

Qhtml

QheadQtitleHello, Wo!ldQGtitleQGhead

Qbod+

Qh0Hello, wo!ldQGh0

Qpe-uest UI: GhelloGsa+helloQGp

Qp$!otocol: HTT$G00QGp

Qp$athIn3o: nullQGp

Qpemote Add!ess: 0;*0QGp

QpA andom 1umbe!: Qst!on%&F;&28;&;F*DKDK&QGst!on%QGp

QGbod+

Page 16: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 16/52

QGhtml

(Sip 6nless...) The liel+ e!!o!s a!e 5DD <ile 1ot <ound5 and 5F Inte!nal Se!#e!

7!!o!5 ead 5How to debu%5 Section

#.M ST,P M- Write a Database Servlet This section assumes that +ou a!e 3amilia! with 5"a#a database p!o%!ammin%5 and

5M+S>? database se!#e!5 Othe!wise, !ead 5 "a#a .atabase $!o%!am5 and 5How to Install

M+S>? F and et Sta!ted5Step M(a) Setup a Database on 3$SN?

Sta!t +ou! M+S>? se!#e! Tae note o3 the se!#e!s po!t numbe! I shall assume that the

M+S>? se!#e! is !unnin% on po!t &&2 (whe!eas the Tomcat is !unnin% on po!t KKKK)

GG <o! Windows

cd O path-to-mysql-bin  GG =hec +ou! M+S>? installed di!ecto!+

"$sld console

 

GG <o! Mac OS X

GG Use %!aphical cont!ol at 5S+stem $!e3e!ences5 ' M+S>?

Sta!t a M+S>? client I shall assume that the!e is a use! called 5m+use!5 with passwo!d

55

GG <o! Windows

cd O path-to-mysql-bin  GG =hec +ou! M+S>? installed di!ecto!+

"$sl u myuser  p

 

GG <o! Mac OS X

cd @usr@local@"$sl@bin.@"$sl u myuser  p

un the 3ollowin% S>? statements to c!eate a database called 5ebooshop5, with a table

called 5boos5 with F columns: id, title, autho!, p!ice, -t+

c!eate database i3 not eists eboosopC

use ebooshopC

d!op table i3 eists boosC

c!eate table boos (

  id  int,  title  #a!cha!(F),

  autor #a!cha!(F),

  price  ]oat,

  t$  int,

  p!ima!+ e+ (id))C

inse!t into boos #alues (00, "a#a 3o! dummies, Tan Ah Tec, 0000, 00)C

Page 17: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 17/52

inse!t into boos #alues (0;, Mo!e "a#a 3o! dummies, Tan Ah Tec, ;;;;, ;;)C

inse!t into boos #alues (0&, Mo!e "a#a 3o! mo!e dummies, Mohammad Ali, &&&&, &&)C

inse!t into boos #alues (0D, A =up o3 "a#a, /uma!, FFFF, FF)C

inse!t into boos #alues (0F, A Teaspoon o3 "a#a, /e#in "ones, 2222, 22)C

select [ 3!om boosCStep M(b) Install 3$SN? +DBC Driver

 4ou need to download M+S>? ".B= d!i#e! i3 +ou ha#e not done so ead 5 Installin% the

M+S>? ".B= .!i#e!5

(or Advanced 6sers 4nl$) 4ou could also place the M+S>? d!i#e! 6a!'le

5m+s-l'connecto!'6a#a'F0 xx N'bin6a!5 in Tomcats 5lib5 di!ecto!+Step M(c) Write a Clientside HT3? or"

?ets w!ite an HTM? sc!ipt to c!eate a query form with & checboes and a submit

button, as illust!ated below Sa#e the HTM? le as ^-ue!+boohtml_ in +ou! application

!oot di!ecto!+ ^QTOM=ATRHOM7webappshello_

0

;

&

D

F

2

*

8

K

0

00

0;

0&

0D

0F

Qhtml

Qhead

  Qtitle4et Anothe! BooshopQGtitle

QGhead

Qbod+

  Qh;4et Anothe! BooshopQGh;

  Q3o!m method5%et5 action<ttp-@@localost-EEEE@ello@uer$<

  Qb=hoose an autho!:QGb

  Qinput t+pe5checbo5 na"e<autor< value<Tan A Tec<Ah Tec

  Qinput t+pe5checbo5 na"e<autor< value<3oa""ad Ali<Ali

  Qinput t+pe5checbo5 na"e<autor< value<Qu"ar</uma!

  Qinput t+pe5submit5 #alue5Sea!ch5

  QG3o!m

QGbod+

QGhtml

 4ou can b!owse the HTM? pa%e b+ issuin% the 3ollowin% U?:

http:GGlocalhost:KKKKGello@uer$boo.t"l

=hec a bo (e%, 5Tan Ah Tec5) and clic the 5Sea!ch5 button An HTT$ 7T !e-uest

will be issued to the U? specied in the Q3o!ms 5action5 att!ibute Obse!#e the U?

o3 the HTT$ 7T !e-uest:

http:GGlocalhost:KKKKGhelloG-ue!+RautorTanATec 

Page 18: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 18/52

 The !e-uest consists o3 two pa!t: a U? co!!espondin% to the 5action5 att!ibute o3 

the Q3o!m ta%, and the 5name#alue5 pai! et!acted 3!om the Qinput ta%, sepa!ated

b+ a \ Tae note that blans a!e !eplaced b+ V (o! `;), because blans a!e not

allowed in the U?

I3 +ou chec two boes (e%, 5Tan Ah Tec5 and 5Mohammad Ali5), +ou will %et this U?,

which has two 5name#alue5 pai!s sepa!ated b+ an

http:GGlocalhost:KKKKGhelloG-ue!+RautorTanATec autor3oa""adAli

 4ou a!e epected to %et an e!!o! 5DD <ile 1ot <ound5, as +ou ha#e +et to w!ite the

se!#e!'side p!o%!am

Step M(d) Write te Serverside Database Nuer$ Servlet

 The net step is to w!ite a "a#a se!#let, which !esponses to the clients !e-uest b+

-ue!+in% the database and !etu!ns the -ue!+ !esults

0;

&

D

F

2

*

8

K

0

00

0;0&

0D

0F

02

0*

08

0K

;

;0

;;

;&

;D

;F

;2

;*

;8

;K

GG To sa#e as 5QTOM=ATRHOM7webappshelloW7B'I1<classes=Nuer$Servlet.ava5impo!t 6a#aio[C

impo!t 6a#as-l[C

impo!t 6a#ase!#let[C

impo!t 6a#ase!#lethttp[C

 

public class Nuer$Servlet etends HttpSe!#let GG "./ 2 and abo#e onl+

 

GG The doet() !uns once pe! HTT$ 7T !e-uest to this se!#let

  JO#e!!ide

  public #oid doet(HttpSe!#lete-uest !e-uest, HttpSe!#letesponse !esponse)

  th!ows Se!#let7ception, IO7ception   GG Set the MIM7 t+pe 3o! the !esponse messa%e

  !esponseset=ontentT+pe(5tetGhtml5)C

  GG et a output w!ite! to w!ite the !esponse messa%e into the netwo! socet

  $!intW!ite! out !esponse%etW!ite!()C

 

=onnection conn nullC

  Statement stmt nullC

  t!+

  GG Step 0: Allocate a database =onnection ob6ect

  conn Driver3ana!er.!etConnection(

  <dbc-"$sl-@@localost-''G@eboosopRuseSS?&alse<U <"$user<U <%%%%<)

  GG database'U?(hostname, po!t, de3ault database), use!name, passwo!d

 

GG Step ;: Allocate a Statement ob6ect within the =onnection

  st"t conn.createState"ent()

 

GG Step &: 7ecute a S>? S7?7=T -ue!+

Page 19: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 19/52

&

&0

&;

&&

&D

&F

&2

&*

&8

&K

D

D0

D;

D&

DD

DF

D2

D*

D8

DK

F

F0

F;

F&

FD

FF

F2F*

F8

FK

2

20

2;

2&

2D

  Strin! slStr <select V &ro" boos were autor <

  <7< reuest.!etPara"eter(<autor<) <7<

  < and t$ L order b$ price desc<

 

GG $!int an HTM? pa%e as the output o3 the -ue!+

  outp!intln(5QhtmlQheadQtitle>ue!+ esponseQGtitleQGheadQbod+5)C

  outp!intln(5Qh&Than +ou 3o! +ou! -ue!+QGh&5)C

  outp!intln(5Qp4ou -ue!+ is: 5 V s-lSt! V 5QGp5)C GG 7cho 3o! debu%%in%

  :esultSet rset st"t.e%ecuteNuer$(slStr)  GG Send the -ue!+ to the se!#e!

 

GG Step D: $!ocess the -ue!+ !esult set

  int count C

  while(rset.ne%t())

  GG $!int a pa!a%!aph QpQGp 3o! each !eco!d

  outp!intln(5Qp5 V rset.!etStrin!(<autor<)

  V 5, 5 V rset.!etStrin!(<title<)

  V 5, 5 V rset.!etDouble(<price<) V 5QGp5)C

  countVVC

  N

  outp!intln(5Qp 5 V count V 5 !eco!ds 3ound QGp5)C

  outp!intln(5QGbod+QGhtml5)C

  N catch (S>?7ception e)

  ep!intStacT!ace()C

  N nall+

  outclose()C GG =lose the output w!ite!

  t!+

  GG Step F: =lose the !esou!ces  i3 (stmt null) st"t.close()C

  i3 (conn null) conn.close()C

  N catch (S>?7ception e)

  ep!intStacT!ace()C

  N

  N

  N

N

=ompile the sou!ce 5>ue!+Se!#let6a#a5 into 5>ue!+Se!#letclass5 See the p!e#ious

eample in Step 2(b) on how to compile a se!#letStep M(e) Con;!ure te :euest 6:? &or te Servlet

Open the con%u!ation le 5webml5 o3 +ou! application 5hello5 that +ou ha#e c!eated

ea!lie! 3o! the HelloSe!#let, ie, 5QTOM=ATRHOM7webappshelloW7B'I1<webml5 Add

the lines that a!e shown in !ed at the ?O=ATIO1S I1.I=AT7.

0

;

Q\ml #e!sion505 encodin%5ISO'88FK'05\

Qweb'app #e!sion5&5

Page 20: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 20/52

&

D

F

2

*

8

K

0

00

0;

0&

0D

0F

02

0*

08

0K

;

;0

;;

;&

;D

;F

;2

;*

;8

;K&

&0

 mlns5http:GG6a#asuncomGmlGnsG6a#aee5

  mlns:si5http:GGwwww&o!%G;0GXM?Schema'instance5

  si:schema?ocation5http:GG6a#asuncomGmlGnsG6a#aee http:GG6a#asuncomGmlGnsG6a#aee

 

Q'' To sa#e as 5helloW7B'I1<webml5 ''

 

Qse!#let

  Qse!#let'nameHelloWo!ldQGse!#let'name

  Qse!#let'classHelloSe!#letQGse!#let'class

  QGse!#let

 

Qse!#let

  Qse!#let'name6serNuer$QGse!#let'name

  Qse!#let'classNuer$ServletQGse!#let'class

  QGse!#let

 

JK >ote- All JservletL ele"ents 36ST be !rouped to!eter and

  placed I> :4>T o& te Jservlet"appin!L ele"ents L

 

Qse!#let'mappin%

  Qse!#let'nameHelloWo!ldQGse!#let'name

  Qu!l'patte!nGsa+helloQGu!l'patte!n

  QGse!#let'mappin%

 

Qse!#let'mappin%

  Qse!#let'name6serNuer$QGse!#let'name

  Qu!l'patte!n@uer$QGu!l'patte!n  QGse!#let'mappin%

QGweb'app

 The abo#e lines con%u!e the 3ollowin% U? to in#oe >ue!+Se!#let:

http:GGlocalhost:KKKKGhello@uer$

Step M(&) Invoe te Servlet &ro" te ClientSide or"

Issue the 3ollowin% U? to b!owse the HMT? 3o!m 5-ue!+boohtml5 that +ou ha#e

c!eated ea!lie!:

http:GGlocalhost:KKKKGhelloGuer$boo.t"l

Select an autho! (e%, 5Tan Ah Tec5) and clic the submit button, which acti#ates the

3ollowin% U? coded in the Q3o!ms 5action5 att!ibute, to%ethe! with the name#alue

pai!:

http:GGlocalhost:KKKKGhelloGuer$RautorTanATec 

 This U? 5G-ue!+5 t!i%%e!s >ue!+Se!#let The >ue!+Se!#let !et!ie#es the name#alue pai!

o3 5autho!TanVAhVTec5 Inside the >ue!+Se!#let, the

Page 21: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 21/52

method !e-uest%et$a!amete!(5autho!5) !etu!ns 5 Tan Ah Tec5, which is inse!ted into the

S>? S7?7=T command to -ue!+ the database The p!ocessed -ue!+ !esult is then w!itten

to the client as an HTM? document

(Sip 6nless...) The liel+ e!!o!s a!e 5DD <ile 1ot <ound5 and 5F Inte!nal Se!#e!

7!!o!5 ead 5How to debu%5 Section

#. (Advanced) Deplo$in! Servletusin! WebServlet (Servlet '. on To"cat M)

Se!#let &, which is suppo!ted b+ Tomcat *, int!oduces the JWebSe!#let annotation,

which %!eatl+ simplies the deplo+ment o3 se!#lets 4ou no lon%e! need to w!ite the

deplo+ment desc!ipto! in 5webml5 Instead, +ou can use the JWebSe!#let annotation

to speci3+ the u!l mappin%

<o! eample, let us w!ite a new se!#let called Anothe!HelloSe!#let6a#a, b+ modi3+in%

the HelloSe!#let6a#a w!itten ea!lie!, with u!l mappin% o3 5sa+hi5

0;

&

D

F

2

*

8

K

0

00

0;

0&

0D

0F

02

0*

08

0K

;

;0

;;;&

;D

;F

;2

;*

;8

;K

GG To sa#e as 5QTOM=ATRHOM7webappshelloW7B'I1<classesAnoterHelloServlet6a#impo!t 6a#aio[C

impo!t 6a#ase!#let[C

impo!t 6a#ase!#lethttp[C

i"port ava%.servlet.annotation.V

 

JWebSe!#let(5Gsa+hi5)

public class Anothe!HelloSe!#let etends HttpSe!#let

  JO#e!!ide

  public #oid doet(HttpSe!#lete-uest !e-uest, HttpSe!#letesponse !esponse)

  th!ows IO7ception, Se!#let7ception

 

GG Set the !esponse MIM7 t+pe

  !esponseset=ontentT+pe(5tetGhtmlCcha!setUT<'85)C

  GG Allocate a output w!ite! to w!ite the !esponse messa%e into the netwo! socet

  $!intW!ite! out !esponse%etW!ite!()C

 

GG W!ite the !esponse messa%e, in an HTM? pa%e

  t!+

  outp!intln(5Qhtml5)C

  outp!intln(5QheadQtitleHello, Wo!ldQGtitleQGhead5)C

  outp!intln(5Qbod+5)C  outp!intln(5Qh0Hello wo!ld, a%ainQGh05)C GG sa+s Hello

  GG 7cho clients !e-uest in3o!mation

  outp!intln(5Qpe-uest UI: 5 V !e-uest%ete-uestUI() V 5QGp5)C

  outp!intln(5Qp$!otocol: 5 V !e-uest%et$!otocol() V 5QGp5)C

  outp!intln(5Qp$athIn3o: 5 V !e-uest%et$athIn3o() V 5QGp5)C

  outp!intln(5Qpemote Add!ess: 5 V !e-uest%etemoteAdd!() V 5QGp5)C

  GG ene!ate a !andom numbe! upon each !e-uest

Page 22: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 22/52

&

&0

&;

&&

&D

&F

&2

  outp!intln(5QpA andom 1umbe!: Qst!on%5 V Math!andom() V 5QGst!on%QGp5

  outp!intln(5QGbod+QGhtml5)C

  N nall+

  outclose()C GG Alwa+s close the output w!ite!

  N

  N

N

In ?ine *, the annotation JWebSe!#let(5Gsa+hi5)  is used to decla!e the U? mappin% 3o!

this se!#let, ie, http:GGlocalhost:KKKKGhelloGsa+hi The!e is no need to p!o#ide an+ mo!e

con%u!ation in 5webml5

'. How to Debu!R

57#e!+thin% that can possibl+ %o w!on% will %o w!on%5 The most impo!tant thin% to do is

to nd the ,::4: 3,SSA*,Alwa$s...

0 e3!esh +ou! b!owse! usin% CntlF (instead o3 !e3!esh button o! simpl+ <F) to

%et a 3!esh cop+, instead o3 3!om the cache

; 4ou ma+ !e'sta!t +ou! Tomcat se!#e! 4ou ma+ also !e'sta!t +ou! b!owse! to clea!

the cache

& =hec +ou! spellin% Alwa+s assume that all p!o%!ams a!e case'sensiti#e .ont

t+pe, cop+ and paste i3 possible

D and MOST IM$OTA1T?4 ' <ind the ,::4: 3,SSA*,

0 =hec the 7!!o! Messa%es on Tomcats =onsole Most o3 the e!!o!

messa%es ha#e a 3ew sc!eens o3 lines 4ou need to sc!oll up slowl+ 3!om the

last line to loo &or te I:ST ?I>, o& te error "essa!es

; =hec the Tomcats lo% les, located at 5QTOM=ATRHOM7lo%s5 The

5catalina++++'mm'ddlo%5 shows the Tomcats sta!tup messa%es Also chec

the 5localhost++++'mm'ddlo%5

b I3 thin%s we!e !unnin% ne until the li%htnin% st!ies, as +ou!sel3 5What ha#e I

chan%ed\5

Cannot Start To"cat To"cat7s Console lases and Disappears

0 T!+ !unnin% the sc!ipt 5con%testbat5 (3o! Windows) o! 5Gcon%testsh5 (3o! Mac

OS XG?inu) to chec +ou! con%u!ation les

; =hec the Tomcats lo% les 3o! e!!o! messa%es The lo% les a!e located at

5QTOM=ATRHOM7lo%s5 The 5catalina++++'mm'ddNlo%5 shows the Tomcats

sta!tup messa%es Also chec the 5localhost++++'mm'ddNlo%5

& I3 the e!!o! messa%es indicate that anothe! Tomcat instance is !unnin%

( 6a#anetBind7ception: Add!ess al!ead+ in use: "EMRBind), ill the Tomcat p!ocess

(see below)C o! t!+ !unnin% the 5shutdown5 sc!ipt at Tomcats bin (<o! Windows,

Page 23: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 23/52

simpl+ double'clic the 5shutdownbat5 o! issue 5shutdown5 3!om =M. <o! Mac OS

X, issue 5Gshutdownsh5 3!om Te!minal)

D I3 the e!!o! messa%es indicate that anothe! application is !unnin% on the Tomcats

po!t numbe!s, then +ou need to chan%e the Tomcats po!t numbe! in se!#e!ml

 4ou can issue command 5netstat 'an5 to chec the status o3 all the po!ts

F Sta!t the tomcat in the debu%%in% mode b+ !unnin% 5catalina debu%5

(o! Gcatalinash debu%) and t+pe 5!un5 in the 5 6db5 p!ompt ?oo 3o! the e!!o!

messa%es?ocatin!@Qillin! To"cat7s Process

• In windows, sta!t 5Tas Mana%e!5, Tomcat !un as a 5p!ocess5 named 5 6a#aee5

 4ou ma+ need to ill the p!ocess

• In Mac OS X, sta!t 5Acti#it+ Monito!5 Select 5All $!ocesses5 and loo 3o!

5 6a#aee5

• In ?inuGMac OS X, +ou ma+ issue 5ps au %!ep tomcat5 to locate the Tomcat

p!ocess 1ote down the p!ocess I. (pid) 4ou can ill the Tomcat p!ocess #ia 5ill'K  pid5

(ire&o%) 6nable to Connect(I,) Internet ,%plorer cannot displa$ te webpa!e(Cro"e) 4opsK *oo!le Cro"e could not connect to ...(Sa&ari) Sa&ari can7t connect to te server

=ause: 4ou a!e simpl+ not connectin% to +ou! Tomcat

Solution:

0 =hec i3 +ou! Tomcat se!#e! has been sta!ted\

; =hec the hostname and po!t numbe!, sepa!ated b+ a colon :, o3 +ou! U?(http:GGlocalhost:KKKKG)

,rror ile >ot ound

=ause: 4ou ha#e connected to +ou! Tomcat But Tomcat se!#e! cannot nd the HTM? le

o! Se!#let that +ou! !e-uested

Solution:

0 =hec +ou! spellin% The path is case'sensiti#e

; <o! HTM? le with U? http:GGlocalhost:KKKKG xxxx G$lenamehtml:

0 Open Tomcats 5webapps5 di!ecto!+, chec i3 sub'di!ecto!+ 5 xxxx 5 eists It

is case'sensiti#e

; Open the 5 xxxx 5 di!ecto!+, chec i3 5$lenamehtml5 eists

b <o! Se!#let with U? http:GGlocalhost:KKKKG xxxx Gservlet"R#:

0 =hec the Tomcats console 3o! e!!o! messa%e 4ou! application cannot be

deplo+ed i3 +ou mae a mistae in editin% 5webml5, which t!i%%e!ed man+

e!!o! messa%es

Page 24: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 24/52

; =hec the Tomcat console to mae su!e that +ou! application has been

deplo+ed

& Open Tomcats 5webapps5 di!ecto!+, chec i3 sub'di!ecto!+ 5 xxxx 5 eists

D Open the 5 xxxx 5 di!ecto!+, chec i3 sub'sub'di!ecto!+ 5W7B'I1<5

(uppe!case with a dash) eistsF Open the 5W7B'I1<5, chec i3 sub'sub'sub di!ecto!+ 5classes5 (lowe!case,

plu!al) eists

2 Open the con%u!ation le 5W7B'I1<webml5:

0 =hec that servlet"R#  is dened in a Qse!#let'mappin% ta% Tae

note o3 the name in Qse!#let'name ta%

; Based on the name noted, loo 3o! the matchin% Qse!#let'

class ta% Tae note o3 the %ervlet&lassname

& Open 5W7B'I1<classes5, chec i3 5%ervlet&lassnameclass5 that +ou

noted eists (1ote: It is 5class5, and 1OT 56a#a5 4ou need to compile

the 56a#a5 to %et the 5class5),rror F Internal Server ,rror

7!!o! F should ha#e t!i%%e!ed man+ e!!o! messa%e in the Tomcats console o to the

 Tomcats console, nd the e!!o! messa%e The e!!o! messa%e spans tens o3 lines 4ou

need to sc!oll up slowl+ to loo 3o! the $rst line o3 the e!!o! messa%e The e!!o! messa%e

should tell +ou the cuase o3 this e!!o!, e% S>? s+nta e!!o!, w!on% use!Gpasswo!d, etc

<o! database se!#let, +ou ma+ chec the e!!o! messa%es at 5=ommon 7!!o!s in ".B=

$!o%!ammin%5

• <o! 51o suitable d!i#e! 3ound5 (Windows) o! 1ull$ointe!7ception  (Mac OS X and

?inu): ead Step *(b) a%ain, a%ain, and a%ain

Apache Tomcat *

Mo!e about the =at This a!ticle is meant 3o! ad#anced p!o%!amme!s who is inte!ested to now mo!e about

 TomcatC o! usin% Tomcat 3o! p!oduction <o! no#ices, !ead 5How to Install and et

Sta!ted with Tomcat5

 The authoritative sou!ce o3 in3o!mation on Tomcat is the Tomcats documentation,

a#ailable unde! Tomcats 5webappsdocs5 di!ecto!+ 4ou ma+ also !e3e! to the "a#a

Page 25: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 25/52

Se!#let, "S$ and "S< specications, as Tomcat is the Reference Implementation 3o! these

technolo%ies

I shall assume that Tomcat is installed in d:m+p!o6ecttomcat, and shall denote this

di!ecto!+ as QTOM=ATRHOM7 o! Q=ATA?I1ARHOM7 ' 5=atalina5 is the codename 3o!

 Tomcat F and abo#e

1. To"cat Arcitecture and Con;!uration

1.1 To"cat7s Installed Director$ Structure

 Tomcat installation p!o#ides these di!ecto!ies:

• bin: 3o! Tomcats bina!ies and sta!tup sc!ipts

• con& : %lobal con%u!ation applicable to all the webapps The de3ault installation

p!o#ides:

o One $olic+ <ile: catalinapolic+ 3o! speci3+in% secu!it+ polic+

o  Two $!ope!ties <iles: catalinap!ope!ties and lo%%in%p!ope!ties,

o <ou! =on%u!ation XM? <iles: se!#e!ml (Tomcat main con%u!ation

le), webml (%lobal web application deplo+ment

desc!ipto!s), contetml (%lobal Tomcat'specic con%u!ation options)

and tomcat'use!sml (a database o3 use!, passwo!d and !ole 3o! authentication

and access cont!ol)

 The con3  also contain a sub'di!ecto!+ 3o! each en%ine, e%, =atalina, which in tu!n

contains a sub'sub'di!ecto!+ 3o! each o3 its hosts, e%, localhost 4ou can place the

host'specic contet in3o!mation (simila! to contetml, but named

as webappml 3o! each webapp unde! the host)

• lib : /eeps the "A'le that a!e a#ailable to all webapps The de3ault installation

include se!#let'api6a! (Se!#let), 6aspe!6a! ("S$) and 6aspe!'el6a! (7?) 4ou ma+ also

eep the "A les o3 ete!nal paca%e he!e, such as M+S>? ".B= d!i#e! ( m+s-l'

connecto!'6a#a'F0N'bin6a!) and "ST? ( 6stl6a! and standa!d6a!)

• lo!s: contains the en%ine lo%le =atalina++++'mm'ddNlo%, host

lo%le localhost++++'mm'ddNlo%, and othe! application lo%les such

as man%e! and host'mana%e! The access lo% (c!eated b+ the Access?o%Eal#e) is also

ept he!e

• webapps: the de3ault appBase ' web applications base di!ecto!+ o3 the

host localhost

wor : contains the t!anslated se!#let sou!ce les and classes o3 "S$G"S<O!%ani9ed in hie!a!ch+ o3 en%ine name (=atalina), host name (localhost), webapp

name, 3ollowed b+ the "a#a classes paca%e st!uctu!e

• te"p: tempo!a!+ les

1.# To"cat Arcitecture

Page 26: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 26/52

 Tomcat is an HTT server  Tomcat is also a servlet container  that can eecute "a#a

Se!#let, and con#e!tin% "a#aSe!#e! $a%es ("S$) and "a#aSe!#e!<aces ("S<) to "a#a Se!#let

 Tomcat emplo+s a hie!a!chical and modula! a!chitectu!e as illust!ated:

1.' 3ain Con;!uration ile <server.%"l<

 Tomcats main con%u!ation le is the 5se!#e!ml5, ept unde!

the Q=ATA?I1ARHOM7con3  di!ecto!+ The de3ault 5se!#e!ml5 is !ep!oduced as 3ollows

(a3te! !emo#in% the comments and mino! touch'ups):

0

;&

D

F

2

*

8

K

0

00

0;

0&0D

0F

02

0*

08

0K

Q\ml #e!sion0 encodin%ut3'8\

QServer po!t58F5 shutdown5SHUT.OW15  Q?istene! class1ame5o!%apachecatalinaco!e"aspe!?istene!5 G

  Q?istene! class1ame5o!%apachecatalinaco!eAp!?i3ec+cle?istene!5 SS?7n%ine5on5 G

  Q?istene! class1ame5o!%apachecatalinaco!e"!eMemo!+?ea$!e#ention?istene!5 G

  Q?istene! class1ame5o!%apachecatalinambeanslobalesou!ces?i3ec+cle?istene!5 G

  Q?istene! class1ame5o!%apachecatalinaco!eTh!ead?ocal?ea$!e#ention?istene!5 G

 

Qlobal1amin%esou!ces

  Qesou!ce name5Use!.atabase5 auth5=ontaine!5

  t+pe5o!%apachecatalinaUse!.atabase5

  desc!iption5Use! database that can be updated and sa#ed5

  3acto!+5o!%apachecatalinause!sMemo!+Use!.atabase<acto!+5  pathname5con3Gtomcat'use!sml5 G

  QGlobal1amin%esou!ces

 

QService name5=atalina5

  QConnector po!t5885 p!otocol5HTT$G005

  connectionTimeout5;5

Page 27: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 27/52

;

;0

;;

;&

;D

;F

;2

;*

;8

;K

&

&0

&;

&&

&D

&F

&2

&*

&8

  !edi!ect$o!t58DD&5 G

  QConnector po!t58K5 p!otocol5A"$G0&5 !edi!ect$o!t58DD&5 G

 

Q,n!ine name5=atalina5 de3aultHost5localhost5

 

Qealm class1ame5o!%apachecatalina!ealm?ocOutealm5

  Qealm class1ame5o!%apachecatalina!ealmUse!.atabaseealm5

  !esou!ce1ame5Use!.atabase5G

  QGealm

 

QHost name5localhost5 appBase5webapps5

  unpacWAs5t!ue5 auto.eplo+5t!ue5

  QEal#e class1ame5o!%apachecatalina#al#esAccess?o%Eal#e5 di!ecto!+5lo%s5

  p!e5localhostRaccessRlo%5 su@5tt5

  patte!n5`h `l `u `t -uotC`!-uotC `s `b5 G

  QGHost

  QG7n%ine

  QGSe!#ice

QGSe!#e!

Server

Se!#e! (?ine ;) is top component, !ep!esentin% an instance o3 TomcatIt can contains one

o! mo!e Se!#ices, each with its own 7n%ines and =onnecto!s

QServer po!t58F5 shutdown5SHUT.OW15 QGSe!#e!

?isteners

 The Se!#e! contains se#e!al ?istene!s (?ines &'*) A ?istene! listens and !esponses to

specic e#ents•  The "aspe!?istene! enables the "aspe! "S$ en%ine, and is !esponsible 3o! !e'

compilin% the "S$ pa%es that ha#e been updated

Q?istener class1ame5o!%apachecatalinaco!e"aspe!?istene!5 G

•  The lobalesou!ces?i3ec+cle?istene!  enables the %lobal !esou!ces, and maes

possible the use o3 "1.I 3o! accessin% !esou!ces such as databases

Q?istener

class1ame5o!%apachecatalinambeanslobalesou!ces?i3ec+cle?istene!5 G

*lobal >a"in! :esources

 The Qlobal1amin%esou!ces element (?ine K'0F) denes the "1.I ("a#a 1amin% and

.i!ecto!+ Inte!3ace) !esou!ces, that allows "a#a so3twa!e clients to disco#e! and loo up

data and ob6ects #ia a name

 The de3ault con%u!ation denes a "1.I name called Use!.atabase #ia

the Qesou!ce element (?ine 0'0D), which is a memo!+'based database 3o! use!

authentication loaded 3!om 5con3Gtomcat'use!sml5

Page 28: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 28/52

Q*lobal>a"in!:esources

  Q:esource name56serDatabase5 auth5=ontaine!5

  t+pe5o!%apachecatalinaUse!.atabase5

  desc!iption5Use! database that can be updated and sa#ed5

  3acto!+5o!%apachecatalinause!sMemo!+Use!.atabase<acto!+5

  pathname5con&@to"catusers.%"l5 GQGlobal1amin%esou!ces

 4ou can dene othe! %lobal !esou!ce "1.I such as M+S>? database to implement

connection poolin%

Services

A Se!#ice associates one o! mo!e =onnecto!s to a 7n%ine The de3ault con%u!ation

denes a Se!#ice called 5=atalina5, and associates two =onnecto!s: HTT$ and A"$ to

the 7n%ine

QService name5=atalina5 QGSe!#ice

Connectors

A =onnecto! is associated with a T=$ po!t to handle communications between

the Se!#ice and the clients The de3ault con%u!ation denes two =onnecto!s:

• HTT$G00: Handle HTT$ communication and enable Tomcat to be an HTT$ se!#e!

=lients can issue HTT$ !e-uests to the se!#e! #ia this =onnecto!, and !ecei#e the

HTT$ !esponse messa%es

QConnector  po!t5885 p!otocol5HTT$G005 connectionTimeout5;5

!edi!ect$o!t58DD&5 G

 The de3ault chooses T=$ po!t 88 to !un the Tomcat HTT$ se!#e!, which is die!ent

3!om the de3ault po!t numbe! o3 8 3o! HTT$ p!oduction se!#e! 4ou can choose an+numbe! between 0;D to 2FF&F, which is not used b+ an+ application, to !un +ou!

 Tomcat se!#e!

 The connectionTimeout att!ibute dene the numbe! o3 milliseconds

this connecto! will wait, a3te! acceptin% a connection, 3o! the !e-uest UI line

(!e-uest messa%e) to be p!esented The de3ault is ; seconds

 The !edi!ect att!ibute !e'di!ects the SS? !e-uests to T=$ po!t 8DD&

• A"$G0&: Apache "Se!# $!otocol connecto! to handle communication between

 Tomcat se!#e! and Apache HTT$ se!#e!

QConnector po!t58K5 p!otocol5A"$G0&5 !edi!ect$o!t58DD&5 G

 4ou could !un Tomcat and Apache HTT$ se!#e!s to%ethe!, and let the Apache HTT$

se!#e! handles static !e-uests and $H$C while Tomcat se!#e! handles the "a#a

Se!#letG"S$ ead 5How To =on%u!e Tomcat to wo! with Apache5Containers

Page 29: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 29/52

 Tomcat !e3e!s to 7n%ine, Host, =ontet, and =luste!, as container  The hi%hest'le#el

is 7n%ineC while the lowest'le#el is =ontet =e!tain components, such

as ealm and Eal#e, can be placed in a containe!,n!ine

A 7n%ine is the hi%hest'le#el o3 a container  It can contains one o! mo!e Hosts 4ou could

con%u!e a Tomcat se!#e! to !un on se#e!al hostnames, nown as virtual host 

Q,n!ine name5=atalina5 de3aultHost5localhost5

 The =atalina 7n%ine !ecei#es HTT$ !e-uests 3!om the HTT$ connecto!, and di!ect them to

the co!!ect host based on the hostnameGI$ add!ess in the !e-uest heade!:eal"

A ealm is a database o3 use!, passwo!d, and !ole 3o! authentication (ie, access

cont!ol) 4ou can dene ealm 3o! an+ containe!, such as 7n%ine, Host, and =ontet,

and =luste!

Q:eal" class1ame5o!%apachecatalina!ealm?ocOutealm5

  Qealm class1ame5o!%apachecatalina!ealmUse!.atabaseealm5!esou!ce1ame56serDatabase5G

QGealm

 The de3ault con%u!ation denes a ealm (Use!.atabaseealm) 3o! the =atalina 7n%ine,

to pe!3o!m use! authentication 3o! accessin% this en%ine It uses the "1.I

name Use!.atabase dened in thelobal1amin%esou!ces

Besides the Use!.atabaseealm, the!e a!e: ".B=ealm (3o! authenticatin% use!s to

connect to a !elational database #ia the ".B= d!i#e!)C .ataSou!ceealm (to connect to

a .ataSou!ce #ia "1.IC "1.Iealm(to connect to an ?.A$ di!ecto!+)C and Memo!+ealm (to

load an XM? le in memo!+)

HostsA Host denes a #i!tual host unde! the 7n%ine, which can in tu!n suppo!t

man+ =ontets (webapps)

QHost name5localhost5 appBase5webapps5 unpacWAs5t!ue5 auto.eplo+5t!ue5

 The de3ault con%u!ation dene one host called localhost The appBase att!ibute denes

the base di!ecto!+ o3 all the webapps, in this case, Q=ATA?I1ARHOM7webapps B+

de3ault, each webapps U? is the same as its directory name <o! eample, the de3ault

 Tomcat installation p!o#ides 3ou! webapps: docs, eamples, host'

mana%e! and mana%e! unde! the webapps di!ecto!+ The onl+ eception is OOT, which

is identied b+ an empt+ st!in% That is, its U? is http:GGlocalhost:88G The unpacWAs species whethe! WA'le d!opped into the webapps di!ecto!+ shall be

un9ipped <o! unpacWAs53alse5, Tomcat will !un the application 3!om the WA'le

di!ectl+, without unpacin%, which could mean slowe! eecution

 The auto.eplo+ att!ibute species whethe! to deplo+ application d!opped into

the webapps di!ecto!+ automaticall+Cluster

Page 30: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 30/52

 Tomcat suppo!ts se!#e! cluste!in% It can !eplicate sessions and contet att!ibutes

ac!oss the cluste!ed se!#e! It can also deplo+ a WA'le on all the cluste!

8alve

A Eal#e can inte!cept HTT$ !e-uests be3o!e 3o!wa!din% them to the applications, 3o! p!e'

p!ocessin% the !e-uests A Eal#e can be dened 3o! an+ containe!, such as 7n%ine, Host,and =ontet, and=luste!

In the de3ault con%u!ation, the Access?o%Eal#e inte!cepts an HTT$ !e-uest and c!eates

a lo% ent!+ in the lo% le, as 3ollows:

QEal#e class1ame5o!%apachecatalina#al#esAccess?o%Eal#e5 di!ecto!+5lo%s5

  p!e5localhostRaccessRlo%5 su@5tt5

  patte!n5`h `l `u `t -uotC`!-uotC `s `b5 G

Othe! #al#es include:

•emoteAdd!Eal#e: which blocs !e-uests 3!om ce!tain I$ add!esses,

• emoteHostEal#e: which blocs !e-uest based on hostnames,

• e-uest.umpe!Eal#e: which lo%s details o3 the !e-uests,

• Sin%leSi%nOn Eal#e: when placed unde! a Qhost, allows sin%le si%n'on to access

all the webapp unde! the host

1. 4ter Con;!urationiles- web.%"lU conte%t.%"lU to"catusers.%"l

YTO.OZ

 The+ a!e so man+ thin%s that +ou can con%u!ed in Tomcat I desc!ibe some o3 the

con%u!ations that I 3ound use3ul in this section

,nablin! Director$ ?istin!

When the !e-uest U? !e3e!s to a di!ecto!+ instead o3 a le, e%, http:GGhost : port GhelloG,

+ou can con%u!e Tomcat to se!#e the di!ecto!+ listin%, o! a welcome $le, o! issue e!!o!

5DD $a%e 1ot <ound5 7nablin% di!ecto!+ listin% is hand+ 3o! test se!#e! but 1OT desi!e

3o! p!oduction se!#e! (as it !e#eal the di!ecto!+ st!uctu!e and epose the enti!e di!ecto!+

hie!a!ch+),nablin! Director$ ?istin! &or A?? Webapps

 To enable di!ecto!+ listin% 3o! all the web applications, +ou could modi3+

the Q=ATA?I1ARHOM7con3webml , b+ chan%in% 5listin%s5 3!om 53alse5 to 5t!ue5 3o! the

5de3ault5 se!#let, as 3ollows:

Q'' The de3ault se!#let 3o! all web applications, that se!#es static ''

Q'' !esou!ces It p!ocesses all !e-uests that a!e not mapped to othe! ''

Q'' se!#lets with se!#let mappin%s ''

Qse!#let

  Qse!#let'namede&aultQGse!#let'name

 Qse!#let'classo!%apachecatalinase!#letsDe&aultServletQGse!#let'class

Page 31: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 31/52

  Qinit'pa!am

  Qpa!am'namedebu%QGpa!am'name

  Qpa!am'#alueQGpa!am'#alue

  QGinit'pa!am

  Qinit'pa!am

  Qpa!am'namelistin!sQGpa!am'name  Qpa!am'#aluetrueQGpa!am'#alue

  QGinit'pa!am

  Qload'on'sta!tup0QGload'on'sta!tup

QGse!#let

 

Q'' The mappin% 3o! the de3ault se!#let ''

Qse!#let'mappin%

  Qse!#let'namede&aultQGse!#let'name

  Qu!l'patte!n@QGu!l'patte!n

QGse!#let'mappin%

 

Q'' .e3ault Welcome <ile ?ist ''

Q'' When a !e-uest UI !e3e!s to a di!ecto!+, the de3ault se!#let loos ''

Q'' 3o! a 5welcome le5 within that di!ecto!+ and, i3 p!esent, ''

Q'' to the co!!espondin% !esou!ce UI 3o! displa+ I3 no welcome le ''

Q'' is p!esent, the de3ault se!#let eithe! se!#es a di!ecto!+ listin%, ''

Q'' o! !etu!ns a DD status, dependin% on how it is con%u!ed ''

Qwelcome'le'list

  Qwelcome'leinde%.t"lQGwelcome'le

  Qwelcome'leinde%.t"QGwelcome'le

  Qwelcome'leinde%.spQGwelcome'le

QGwelcome'le'list The abo#e con%u!ation maps U? 55 (!oot di!ecto!+ o3 the web contet) (in Qu!l'

patte!n) to "a#a class .e3aultSe!#let (in Qse!#let'class) #ia the common se!#let name

o3 de3ault (in Qse!#let'name) We enable di!ecto!+ listin% b+ chan%in% the se!#lets

initiali9ation pa!amete! listin%s to t!ue

I3 a use! !e-uests 3o! a di!ecto!+, and the di!ecto!+ listin% is enabled and it contains one

o3 the les in the Qwelcome'le list, the welcome le will be se!#edC othe!wise, the

di!ecto!+ listin% will be se!#ed On the othe! hand, i3 a di!ecto!+ !e-uest is !ecei#ed and

the di!ecto!+ listin% is not enabled, the se!#e! !etu!ns an e!!o! 5DD $a%e 1ot <ound5,nablin! Director$ ?istin! &or a particular Webapp

I3 +ou wish to allow di!ecto!+ listin% o3 a pa!ticula! web application onl+, +ou could

disable the di!ecto!+ listin% in 5Q=ATA?I1ARHOM7con3webml5 %loball+, and dene the

3ollowin% Qse!#let andQse!#let'mappin%  in +ou! application'specic W7B'I1<webml,

as 3ollows 4ou need to use anothe! Qse!#let'name in place o3 .e3aultSe!#let

Qse!#let

  Qse!#let'nameDirector$?istin!QGse!#let'name

Page 32: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 32/52

  Qse!#let'classo!%apachecatalinase!#lets.e3aultSe!#letQGse!#let'class

  Qinit'pa!am

  Qpa!am'namedebu%QGpa!am'name

  Qpa!am'#alueQGpa!am'#alue

  QGinit'pa!am

  Qinit'pa!am  Qpa!am'namelistin!sQGpa!am'name

  Qpa!am'#aluetrueQGpa!am'#alue

  QGinit'pa!am

QGse!#let

 

Qse!#let'mappin%

  Qse!#let'nameDirector$?istin!QGse!#let'name

  Qu!l'patte!n@QGu!l'patte!n

QGse!#let'mappin%

Auto"atic Servlet :eload

 To enable automatic se!#let !eload (whene#e! a se!#let is !e'compiled), +ou need tospeci3+ Q=ontet reloadable<true<QG=ontet, in

5Q=ATA?I1ARHOM7con3contetml5 3o! all web applications, o! the Q=ontet element

in 5Q=ATA?I1ARHOM7con3se!#e!ml5 3o! a pa!ticula! web application

 The 3ollowin% messa%es appea! on the Tomcats console i3 +ou !e'compile a se!#let:

XXX X, XXXX XX:XX:XX XX o!%apachecatalinaco!eStanda!d=ontet !eload

I1<O: :eloadin! Conte%t wit pat X@elloY as started

XXX X, XXXX XX:XX:XX XX o!%apachecatalinaco!eStanda!d=ontet !eload

I1<O: :eloadin! Conte%t wit pat X@elloY is co"pleted

7nablin% automatic se!#let !eload is hand+ du!in% application de#elopment, but it

!e-ui!es si%nicant !untime o#e!head to listen to the chan%es, and is not !ecommended

3o! p!oduction s+stems Instead, +ou could use the 5mana%e!5 to t!i%%e! !eloads on

demandSettin! te Conte%t :oot Director$ and :euest 6:? o& aWebapp

A se!#e! could !un man+ web applications A webapp is also called a web context 

 The context root  (o! document base directory ) !e3e!s to the base di!ecto!+ o3 a webapp

 The+ a!e a 3ew wa+s to con%u!e a contet !oot and its !e-uest U? o3 a webapp:

0 (7=OMM71.7.) =!eate a di!ecto!+ unde! Q=ATA?I1ARHOM7webapps 3o! +ou!

webapp A contet will be c!eated with !e-uest U? set to the name o3 the

di!ecto!+ <o! eample, i3 +ou c!eate a di!ecto!+ called 5hello5 unde! Tomcats

5webapps5 This application can be accessed b+ web use!s #ia

U? http:GGhost : port Ghello

 To chan%e the !e-uest U? o3 the webapp, c!eate a 5contetml5 con%u!ation le,

as 3ollows, and place it unde! 5&ontextRoot M7TA'I1<5:

Page 33: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 33/52

Q=ontet path5G your"R#ath5 G

; Alte!nati#el+, +ou can w!ite a Q=ontet element

in Q=ATA?I1ARHOM7con3se!#e!ml, unde! the QHost element 4ou can speci3+

both the U? and the base di!ecto!+ <o! eample,

&

D

F Q=ontet path5Gws5 docBase5d:Gwo!shop5 !eloadable5t!ue5

2 QG=ontet

* QGHost

8 QG7n%ine

K QGSe!#ice

QGSe!#e!

In the abo#e eample, we dene a web contet with U? 5 Gws5, with contet !oot

(docBase o! document base di!ecto!+) at 5d:wo!shop5 This application can be

accessed #ia U?http:GGhost : port @ws

 Tae note that:

o  The con%u!ation c!eates a mappin' 3!om the 5U? $ath5 issued b+ the

web use!s to the 5document base di!ecto!+5 in the se!#e!s le s+stem, whe!e

+ou sto!e +ou! webapp !esou!ces

o $lace the Q=ontet element be3o!e the endin% ta% o3  

the QHost element

o Use Uni'st+le 3o!wa!d slash G as the directory separator  in the

con%u!ation le, instead o3 Window'st+le bac slash o  The att!ibute !eloadable5t!ue5 ass Tomcat to monito! +ou! se!#lets 3o!

chan%es, and automaticall+ !eload the se!#lets i3 chan%es is detected This is

hand+ 3o! a de#elopment s+stem, but ine@cient in a p!oduction s+stem

0 W!ite a con%u!ation le with a Q=ontet element and place it unde! Tomcats

5con3=atalinalocalhost5 <o! eample, suppose we wish to c!eate a webapp with

U? 5hello5 in base di!ecto!+ 5d:m+p!o6ectm+Hello5, c!eate the 3ollowin% le

5helloml5:

00 Q\ml #e!sion505 encodin%5UT<'85\

Q=ontet docBase5.:m+p!o6ectm+Hello5 path5Ghello5 G

Can!in! te De&ault <webapps< Director$

 The default  di!ecto!+ 3o! deplo+in% web applications is Q=ATA?I1ARHOM7webapps 4ou

could chan%e the de3ault b+ modi3+in% the con%u!ation

le 5con3se!#e!ml5 QHost elements 5appBase5 att!ibute as 3ollows:

QHost name5localhost5 appBase<webapps<

Page 34: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 34/52

  unpacWAs5t!ue5 auto.eplo+5t!ue5

  mlEalidation53alse5 ml1amespaceAwa!e53alse5

 

QGhost

#. Deplo$in! WebappsA web context  is a sin%le web application (webapp) It is the lowest'le#el containe!, that

+ou can dene components such as ealm and Eal#e B+ de3ault, all webapps a!e ept

unde! theQ=ATA?I1ARHOM7webapps di!ecto!+ (as con%u!ed in

the Qhost element appBase att!ibute

A "a#a webapp ma+ contain man+ t+pes o3 les, such as HTM?, =SS, Sc!ipts, ima%es,

 "S$, se!#let, utilit+ classes, ete!nal lib!a!+ 6a!'les A "a#a webapp must 3ollow a

st!ict directory structure as depicted in the Se!#letG"S$ specications This enables

deplo+ment in a "a#a'capable web se!#e! (such as Apache Tomcat and lasssh) The

!esou!ces must be ept in the co!!ect di!ecto!ies and sub'di!ecto!ies

 The U? o3 a webapp, b+ de3ault, is the same as the base directory name (o! context 

root ) o3 the webapp

#.1 Webapp7s Director$ Structure

 The di!ecto!+ st!uctu!e o3 a webapp is as 3ollows:

• 5&ontextRoot 5: contains the !esou!ces that a!e #isible and accessible b+ the web

clients, such as HTM?, =SS, Sc!ipts and ima%es These !esou!ces will be deli#e!ed to

Page 35: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 35/52

the clients as it is 4ou could c!eate sub'di!ecto!ies such as ima%es, css and sc!ipts,

to 3u!the! cate%o!ies the #a!ious !esou!ces

• 5&ontextRoot W7B'I1<5: This di!ecto!+, althou%h unde! the contet !oot, is 1OT

#isible to the web use!s In othe! wo!ds, it is 1OT accessible b+ the clients di!ectl+

(3o! secu!it+ !eason) This is whe!e +ou eep +ou! application'specic con%u!ation

les such as 5webml5 Its sub'di!ecto!ies contain p!o%!am classes, sou!ce les,

and lib!a!ies

• 5&ontextRoot W7B'I1<s!c5: /eeps the "a#a p!o%!am sou!ce les It is optional but

a %ood p!actice to sepa!ate the sou!ce les and classes to 3acilitate deplo+ment

• 5&ontextRoot W7B'I1<classes5: /eeps the "a#a classes (compiled 3!om the sou!ce

codes) =lasses dened in paca%es must be ept acco!din% to the "a#a paca%e

di!ecto!+ st!uctu!e

• 5&ontextRoot W7B'I1<lib5: /eeps the lib!a!ies (6a!'les), which a!e p!o#ided b+

othe! paca%es, specic and a#ailable to this webapp onl+

5&ontextRoot M7TA'I1<5: This is also a hidden di!ecto!+, to eep !esou!ces andcon%u!ations (e%, 5contetml5) !elated to the se!#e! In cont!ast, 5W7B'I1<5 is 3o!

!esou!ces !elated to this web application, independent o3 the se!#e!

#.# WebappSpeci;c Con;!uration iles

 These a!e the con%u!ation les specic to a webapp: (a) W7B'I1<webmlC (b) M7TA'

I1<contetml

 4ou can con%u!e a webapp in man+ wa+s: (a) W!ite a Qcontet element

in se!#e!ml unde! QHost element, (b) contextRoot M7TA'I1<contetml, and

(c)con3=atalinalocalhostwebappml, and (d) con3contetml See 5Settin% the =ontet

oot .i!ecto!+ and e-uest U? o3 a Webapp5

#.' Web Application Deplo$"ent Descriptors <web.%"l<

 The 5webml5 contains the deplo+ment desc!ipto!s The!e a!e two sets o3 webml:

0 Q=ATA?I1ARHOM7con3webml : applicable to A?? webapps

;   &ontextRoot W7B'I1<webml: applicable to the specic web contet It o#e!!ides

the %lobal settin%, i3 an+

 The complete specication 3o! 5webml5 can be 3ound in the 5"a#a Se!#let Specication5

unde! 5.eplo+ment .esc!ipto!5

A sample con%u!ation o3 a se!#let with most o3 its sub'elements is as 3ollows:

Qweb'app

 

  Qse!#let

  Qicon

  Qsmall'iconGima%esGicon6p%QGsmall'icon

Page 36: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 36/52

  QGicon

  Qse!#let'nameM+Se!#latQGse!#let'name

  Qdispla+'nameM+ Se!#let .ispla+ 1ameQGdispla+'name

  Qdesc!iptionM+ Testin% Se!#let lon% desc!iptionQGdesc!iption

  Qse!#let'classM+Se!#let=lassnameQGse!#let'class

  Qinit'pa!am

  Qpa!am'namem+$a!m1ameQGpa!am'name

  Qpa!am'#aluem+$a!mEalueQGpa!am'#alue

  QGinit'pa!am

  Qload'on'sta!tup;FQGload'on'sta!tup

  QGse!#let

 

 

Qse!#let'mappin%

  Qse!#let'nameM+Se!#latQGse!#let'name

  Qu!l'patte!nGsa+helloQGu!l'patte!n

  QGse!#let'mappin% 

QGweb'app

#. Deplo$in! a Web Application in a WA: ;le

 4ou could use the "./s 6a! utilit+ to 59ip5 up all the les o3 a web application to p!oduce

a so'called WA (Web Application A!chi#e) le 3o! deplo+ment, o! dist!ibution

=han%e cu!!ent di!ecto!+ to the web applications contet !oot

contextRoot  ar cv& test.war .

.!op the testwa! into Q=ATA?I1ARHOM7webapps A contet called test will be c!eated

automaticall+ 4ou can access the web application #ia U? http:GGhost : port Gtest

 Tomcat actuall+ unpacs the testwa! into a 5test5 di!ecto!+

in Q=ATA?I1ARHOM7webapps 4ou need to !emo#e this di!ecto!+, i3 +ou !eload a new

#e!sion

'. :unnin! To"cat

'.1 To"cat7s 3ana!er

e3e!ences:

0 5Tomcat Web Application Mana%e! How To5 J 5webappsGdocsGhtml'mana%e!'

howtohtml5

; 5Tomcat Mana%e! App How'To5 J 5webappsGdocsGmana%e!'howtohtml5

 Tomcat 5mana%e!5 webapp allows +ou to deplo+ a new web applicationC sta!t, stop,

!eload o! un'deplo+ an eistin% one, without ha#in% to shut down and !esta!t the se!#e!,

in a p!oduction en#i!onment

Page 37: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 37/52

 To enable Tomcats mana%e!, edit 5Q=ATA?I1ARHOM7con3to"catusers.%"l5 to

include a !ole called 5mana%e!'%ui5 and a use! with this !ole 4ou ma+ also assi%n othe!

!oles, such as admin'%ui 3o! accessin% the Tomcat 5Host Mana%e!5

Q!ole !olename5mana%e!'%ui5 G

Q!ole !olename5mana%e!'status5 G

Q!ole !olename5mana%e!'sc!ipt5 G

Q!ole !olename5mana%e!'6m5 G

Q!ole !olename5admin'%ui5 G

Q!ole !olename5admin'sc!ipt5 G

 

Quse! use!name5m+mana%e!5 passwo!d55 !oles5mana%e!'%ui, admin'%ui5G

 To in#oe mana%e! web application, use http:GGlocalhost:88Gmana%e!Ghtml 4ou can use

 Tomcats mana%e! to:

0 ?ist all webapps

; Sta!tGStopGeload a pa!ticula! webapp

& .eplo+ a new webapp !emotel+, and undeplo+ a webapp without !esta!tin% the

containe!

D Te!minate (o! In#alidate) sessions ' a session has a p!e'set epi!+ time (e%, &

sec)

F Anal+9e memo!+ leas

2 Eiew "EM status and Se!#e! status

 Tomcat * p!o#ides sepa!ate mana%e! !oles 3o! the UI (mana%e!'%ui), status (mana%e!'

status), sc!iptin% (mana%e!'sc!ipt) and "MX p!o+ (mana%e!'6m), dened in

5webappsGmana%e!GW7B'I1<Gwebml5 This allows 3o! ne'%!ained access cont!ol to

mana%ement tass

• mana%e!'%ui ' Access to the HTM? 5web5 inte!3ace, #ia:

http:GGhostN:po!tNGmana%e!Ghtml

• mana%e!'status ' Access to the 5Se!#e! Status5 pa%e onl+, #ia:

http:GGhostN:po!tNGmana%e!Gstatus

• mana%e!'sc!ipt ' Access to the 5plain'tet5 inte!3ace, and to the 5Se!#e! Status5

pa%e, #ia command in the 3o!m o3:

• http:GGhostN:po!tNGmana%e!GtetGcommandN\pa!amete!sN

Page 38: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 38/52

•  

• GG 7amples

• http:GGhostN:po!tNGmana%e!GtetGlist GG ?ist all webapps

http:GGhostN:po!tNGmana%e!GtetGdeplo+\pathGtestapp GG .eplo+ webapp

• mana%e!'6m ' Access to "MX p!o+ inte!3ace and to the 5Se!#e! Status5 pa%e, #ia:

http:GGhostN:po!tNGmana%e!G6mp!o+G\commandNpa!amete!N

<o! secu!it+ !eason, a use! should 1OT be %i#en mo!e than one o3 the 3ollowin%

!oles: mana%e!'%ui, mana%e!'sc!ipt, and mana%e!'6mTo"cat7s <Ad"inistration Tool<

 The Tomcat 5Adminst!ati#e Tool5 (o! 5Administ!ato! =onsole5) has been !emo#ed since

 Tomcat 2 Use "MX mana%e! instead

'.# Auto"atic Startup at Bootti"e(Windows) :unnin! To"cat as a Windows Service

 4ou need to download the Windows'specic #e!sion o3 Tomcat (3!om Tomcats download,

choose &;'bit o! 2D'bit Windows 9ip #e!sion, e%, apache'tomcat'*N'windows'

829ip)

ead 5Windows se!#ice How'To5 in the Tomcat documentation

(Q=ATA?I1ARHOM7webappsdocswindows'se!#ice'howtohtml )

In a p!oduction en#i!onment, it is mo!e con#enient to !un Tomcat as a service, so that it

can sta!t automaticall+ whene#e! the s+stem is sta!ted (o! !e'sta!t automaticall+ a3te!

an unepected inte!!uption)

 To install Tomcat as a se!#ice, sta!t a =M. shell (with administ!ato! !i%ht) and !unthe Q=ATA?I1ARHOM7binse!#icebat  with install option:

=han%e di!ecto!+ to Q=ATA?I1ARHOM7bin

Q=ATA?I1ARHOM7bin service install

Installin% the se!#ice Tomcat*

 The se!#ice Tomcat* has been installed

 The Tomcat se!#ice called 5Apache Tomcat *5 is installed and will sta!t automaticall+

whene#e! the s+stem is sta!ted =hec the 5Se!#ices5 unde! 5=ont!ol $anel5 L

5Administ!ati#e Tools5A UI application called Tomcat*w  is a#ailable 3o! monito!in% and con%u!in% Tomcat

se!#ices ?aunch Tomcat*w:

Q=ATA?I1ARHOM7bin To"catMw

 4ou could put the Tomcat icon in the s+stem t!a+ #ia the MS (Monito! Se!#ice) option:

Page 39: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 39/52

Q=ATA?I1ARHOM7bin To"catMw @@3S@@

 4ou can sta!tGstop the Tomcat se!#ice now #ia:

0  Tomcat*wC

; 5=ont!ol $anel5 L 5Administ!ato! Tools5 L 5Se!#ices5 L 5Apache Tomcat *5 L5Sta!t5C

& <!om =M. shell, Issue 5net5 command:

D p!ompt net start to"catM

F The Apache Tomcat * se!#ice is sta!tin%

2 The Apache Tomcat * se!#ice was sta!ted success3ull+

*

8

K p!ompt net stop to"catM

0 The Apache Tomcat * se!#ice is stoppin%

 The Apache Tomcat * se!#ice was stopped success3ull+

 To uninstall Tomcat Se!#ice, !un

the Q=ATA?I1ARHOM7binse!#icebat  with !emo#e option:

Q=ATA?I1ARHOM7bin service re"ove

 The se!#ice Tomcat* has been !emo#ed

 4ou can also use Mic!oso3t Mana%ement =onsole (MM=) to mana%e the se!#ices: o to

5Sta!t5 L L un L ente! 5se!#icesmsc5

A ]ip side o3 !unnin% Tomcat as a se!#ice is +ou need to !ead the e!!o! messa%es

3!om Q=ATA?I1ARHOM7lo%s instead o3 the Tomcat console

(?inu% and 3ac 4S) Auto"atic Startup on :eboots

 To sta!t Tomcat automaticall+ when the machine boots up, +ou need to c!eate a init

sc!ipt in GetcGinitd, e%, GetcGinitdGtomcat, as 3ollows:

GbinGsh

Tomcat init sc!ipt 3o! ?inu

 "AEARHOM7path'to'6a#a'installed'di!ecto!+N

=ATA?I1ARHOM7path'to'Tomat'installed'di!ect%o!+N

epo!t "AEARHOM7 =ATA?I1ARHOM7

eec =ATA?I1ARHOM7GbinGcatalinash [

Sa#e the sc!ipt as 5tomcat5 in GetcGinitd

cd GetcGinitd

sudo chown !oot!oot tomcat GG chan%e owne! to use! !oot, %!oup !oot

Page 40: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 40/52

sudo chmod *FF tomcat GG chan%e mode uV!w, %V!, aV!

 4ou can sta!tstop!esta!t Tomcat #ia:

service to"cat startZstopZrestart

'.' To"cat7s Startup Script To sta!t tomcat se!#e!, +ou could in#oe the batch le 5sta!tupbat5 (3o! Windows) o!

5sta!tupsh5 (3o! ?inuGMac) (in di!ecto!+ 5Q=ATA?I1ARHOM7bin5,

whe!e Q=ATA?I1ARHOM7 !e3e!s to the Tomcat installed di!ecto!+) The 5sta!tupbat

sta!tupsh5 in#oes 5catalinabat sta!tcatalinash sta!t5

Alte!nati#el+, +ou could call the 5catalinabatcatalinash5 di!ectl+, which p!o#ides mo!e

options o3 sta!tin% Tomcat 7nte! 5catalina5 to #iew the options:

Q=ATA?I1ARHOM7Gbin catalina

Usin% =ATA?I1ARBAS7: .:tomcat*N

Usin% =ATA?I1ARHOM7: .:tomcat*NUsin% =ATA?I1ARTM$.I: .:tomcat*Ntemp

Usin% "7RHOM7: d:6d02

Usa%e: catalina ( commands )

commands:

  debu% Sta!t =atalina in a debu%%e!

  debu% 'secu!it+ .ebu% =atalina with a secu!it+ mana%e!

  6pda sta!t Sta!t =atalina unde! "$.A debu%%e!

  run Start Catalina in te current window

  !un 'secu!it+ Sta!t in the cu!!ent window with secu!it+ mana%e!

  start Start Catalina in a separate window

  sta!t 'secu!it+ Sta!t in a sepa!ate window with secu!it+ mana%e!

  stop Stop Catalina  con%test un a basic s+nta chec on se!#e!ml

  #e!sion What #e!sion o3 tomcat a!e +ou !unnin%\

Stud+ the sou!ce codes o3 5catalinabatcatalinash5 Tae note that the en#i!onment

#a!iable "AEARHOM7 is needed in this sc!ipt

 The othe! sc!ipts p!o#ided a!e:

• shutdownbatshutdownsh: 3o! shutin% down the Tomcat se!#e!

• con%testbatcon%testsh: 3o! checin% the con%u!ation le se!#e!ml, same

as 5catalina con%test5

• #e!sionbat#e!sionsh: 3o! displa+in% the #e!sions, same as 5catalina #e!sion5• di%estbatdi%estsh: main% passwo!d hash and enc!+ptin% passwo!d

• Inte!nal used: setclasspathbatsetclasspathsh, cpappendbat (classpath

append), tool'w!appe!battool'w!appe!sh , daemonsh

. Securit$

.1 :eal" and 6ser Autentication in To"cat

Page 41: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 41/52

e3e!ences:

0 5ealm =on%u!ation HOW'TO5 (J 5Q=ATA?I1ARHOM7webappsdocs!ealm'

howtohtml5)

; 5"a#a 77 F Tuto!ial5, $a!t IE 5Se!#ices5, =hapte!s ;8'& on Secu!it+

In In3o!mation Secu!it+:

•   Access control deals with identi3+in% which !esou!ces !e-ui!e p!otection, and

which use!s (!oles) a!e autho!i9ed to access the p!otected !esou!ces

•   Authentication deals with #e!i3+in% use!s c!edential, ie, ensu!in% the use! is

5who he said he is5 Use!s c!edential is t+picall+ p!o#ided in the 3o!m o3 

use!nameGpasswo!d and possibl+ I$ add!ess (hostname) Othe! means include

biomet!ics (n%e!'p!int, 3ace !eco%nition, !etina) and di%ital ce!ticates

•   &on$dentiality  deals with the enc!+ption o3 the t!ansmitted data o#e! the

netwo! This is o3ten ca!!ied out #ia emplo+in% HTT$ o#e! SS? (Secu!e Socet

?a+e!), nown as HTT$S•   (essa'e Inte'rity  ensu!es that messa%es a!e not tempe!ed du!in% t!ansmission

 This is done #ia messa%e di%est o! hash

•   )on-repudiation: I3 heGshe has sent a messa%e, heGshe cannot den+ This is done

#ia di%ital ce!ticate and publicGp!i#ate e+s

Secu!it+ can be mana%ed b+ the webapps themsel#es (called application-

mana'ed secu!it+) o! #ia the Tomcat containe! (called container-mana'ed secu!it+) In

containe!'mana%ed secu!it+, secu!it+ is handled b+ the se!#e! The se!#e!'side p!o%!ams

(se!#lets, "S$s) do not need an+ secu!it+'awa!e code That is, the secu!it+ cont!ol is

totall+ t!anspa!ent to the se!#e!'side p!o%!ams This section shall deal with containe!'

mana%ed secu!it+ 3o! access cont!ol and authentication

In Tomcat, a use! is identied #ia use!nameGpasswo!d A use! is assi%ned !ole(s) (e%,

mana%e!, admin, use!, etc) Tomcat %!ants access 3o! webapps to !ole(s), instead o3 

indi#idual use!s

A realm is a collection o3 use!namesGpasswo!ds and !oles Tomcat suppo!ts the 3ollowin%

t+pes o3 !ealms:

• Use!.atabaseealm: ept in a XM? le 5con3tomcat'use!sml5, accessed #ia ".1I

("a#a 1amin% and .i!ecto!+ Inte!3ace)

•  ".B=ealm: ept in a !elational database such as M+S>?, accessed #ia ".B=

• othe!s: "1.Iealm (uses ?.A$ di!ecto!+), "AASealm ("a#a Authentication and

Autho!i9ation Se!#ice)

 4ou can used the Q!ealm element to con%u!e a !ealm in

5con3se!#e!ml5 Q!ealm element can be placed in Qen%ine, Qhost, o! Qcontet,

which dete!mines the scope o3 the !ealm: all #i!tual hosts unde! the en%ine, a pa!ticula!

host, o! a pa!ticula! web application

Page 42: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 42/52

.# 6serDatabase:eal"

Use!.atabaseealm sto!es use! in3o!mation in a XM? le and accessed #ia "1.I ("a#a

1amin% and .i!ecto!+ Inte!3ace) B+ de3ault, the XM? le is

5Q=ATA?I1ARHOM7con3tomcat'use!sml 5Use!.atabaseealm is loaded into memo!+

3!om the specied le, and ept in memo!+ until Tomcat is shut down Tomcat p!o#ide a "S$ eample called 5<OM Authentication5 (J

http:GGlocalhost:88GeamplesG6spGsecu!it+Gp!otectedGinde6sp5), which

uses Use!.atabaseealm ?et us stud+ this eample But be3o!e we %et into the codes,

lets loo at the Tomcats con%u!ations<con&=server.%"l<

 4ou can speci3+ the t+pe o3 !ealm to be used #ia Qealm element in se!#e!ml In this

case, Use!.atabaseealm The Qealm  is dened within the Q7n%ine elements, and

thus applicable to all the #i!tual hosts and webapps, unde! this se!#e!

 To speci3+ the le used in Use!.atabaseealm, a ".1I !esou!ce named 5Use!.atabase5 is

dened, which maps to the le 5con3tomcat'use!sml5

QSe!#e!

  Q'' lobal "1.I !esou!ces ''

  Qlobal1amin%esou!ces

  Q'' 7ditable use! database that can also be used b+

  Use!.atabaseealm to authenticate use!s ''

  Qesou!ce name5Use!.atabase5 auth5=ontaine!5

  t+pe5o!%apachecatalinaUse!.atabase5

  desc!iption5Use! database that can be updated and sa#ed5

  3acto!+5o!%apachecatalinause!sMemo!+Use!.atabase<acto!+5

  pathname5con3Gtomcat'use!sml5 G

  QGlobal1amin%esou!ces

 

QSe!#ice name5=atalina5

  Q7n%ine name5=atalina5 de3aultHost5localhost5

  Q'' Use the ?ocOutealm to p!e#ent attempts to %uess use! passwo!ds

  #ia a b!ute'3o!ce attac ''

  Qealm class1ame5o!%apachecatalina!ealm?ocOutealm5

  Qealm class1ame5o!%apachecatalina!ealmUse!.atabaseealm5

  !esou!ce1ame5Use!.atabase5 G

  QGealm

  QHost name5localhost5

    QGHost

  QG7n%ine

  QGSe!#ice

QGSe!#e!

<con&=to"catusers.%"l<

ecall that a use! is identied #ia use!nameGpasswo!d A use! is assi%ned !ole(s)

Accesses 3o! web applications a!e %!anted to !ole(s) instead o3 indi#idual use!s 5 Tomcat'

Page 43: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 43/52

use!sml5 contains the 3ollowin% !oles and use!nameGpasswo!d, but commented-out 

Uncomment them 3o! testin% the eample Two !oles, tomcat and !ole0, and th!ee

use!s, tomcat, !ole0 and both a!e dened

Q\ml #e!sion505 encodin%5ISO'88FK'05 \

Qtomcat'use!s

  Q!ole !olename5tomcat5 G

  Q!ole !olename5!ole05 G

 

Quse! use!name5tomcat5 passwo!d5tomcat5 !oles5tomcat5 G

  Quse! use!name5both5 passwo!d5tomcat5 !oles5tomcat,!ole05 G

  Quse! use!name5!ole05 passwo!d5tomcat5 !oles5!ole05 G

QGtomcat'use!s

 Tae note that the passwo!ds a!e sto!ed in clea! tet, which is not !eall+ desi!able

<ContextRoot =W,BI>=web.%"l<

<o! Tomcats webapp called 5eamples5, the secu!it+ !oles a!e dened usin% Qsecu!it+'

const!aint element in 5webappseamplesW7B'I1<webml5 as 3ollows The U?

patte!nsG6spGsecu!it+Gp!otectedG[ a!e accessible b+ use!s ha#in% !oles

o3 tomcat and !ole0 onl+

Qweb'app

 

  Qsecu!it+'const!aint

  Qdispla+'name7ample Secu!it+ =onst!aintQGdispla+'name

  Qweb'!esou!ce'collection

  Qweb'!esou!ce'name$!otected A!eaQGweb'!esou!ce'name

  Q'' .ene the contet'!elati#e U?(s) to be p!otected ''

  Qu!l'patte!nG6spGsecu!it+Gp!otectedG[QGu!l'patte!n

  Q'' I3 +ou list http methods, onl+ those methods a!e p!otected ''

  Qhttp'method.7?7T7QGhttp'method

  Qhttp'method7TQGhttp'method

  Qhttp'method$OSTQGhttp'method

  Qhttp'method$UTQGhttp'method

  QGweb'!esou!ce'collection

  Qauth'const!aint

  Q'' An+one with one o3 the listed !oles ma+ access this a!ea ''

  Q!ole'nametomcatQG!ole'name

  Q!ole'name!ole0QG!ole'name

  QGauth'const!aint

  QGsecu!it+'const!aint

Page 44: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 44/52

 

Q'' .e3ault lo%in con%u!ation uses 3o!m'based authentication ''

  Qlo%in'con%

  Qauth'method<OMQGauth'method

  Q!ealm'name7ample <o!m'Based Authentication A!eaQG!ealm'name

  Q3o!m'lo%in'con%  Q3o!m'lo%in'pa%eG6spGsecu!it+Gp!otectedGlo%in6spQG3o!m'lo%in'pa%e

  Q3o!m'e!!o!'pa%eG6spGsecu!it+Gp!otectedGe!!o!6sp QG3o!m'e!!o!'pa%e

  QG3o!m'lo%in'con%

  QGlo%in'con%

 

Q'' Secu!it+ !oles !e3e!enced b+ this web application ''

  Qsecu!it+'!ole

  Q!ole'name!ole0QG!ole'name

  QGsecu!it+'!ole

  Qsecu!it+'!ole

  Q!ole'nametomcatQG!ole'name

  QGsecu!it+'!ole

.' HT3? 4:3based Autentication 3etod

 The eample uses HTM? <OM'based authentication method, dened in element Qlo%in'

con% All accesses to the p!otected U?s

(http:GGlocalhost:88GeamplesG6spGsecu!it+Gp!otectedG[) will be !edi!ected to

the lo%in6sp pa%e (dened in Q3o!m'lo%in'pa%e), which p!ompts use! 3o! the c!edential

<o! eample, i3 a use! !e-uests

3o!http:GGlocalhost:88GeamplesG6spGsecu!it+Gp!otectedGinde6sp, the lo%in6sp will be

displa+ed

 The lo%in6sp pa%e contain a html Q3o!m (thus called <OM'based authentication):

Qhtml

QheadQtitle?o%in $a%e 3o! 7amplesQGtitleQGhead

Qbod+

Q3o!m method5$OST5 actionQ` !esponseencodeU?(56Rsecu!it+Rchec5) ̀

  Use!name:Qinput t+pe5tet5 name5 6Ruse!name5

  $asswo!d:Qinput t+pe5passwo!d5 name5 6Rpasswo!d5

  Qinput t+pe5submit5 #alue5?o% In5

QG3o!m

QGbod+

QGhtml

 The lo%in pa%e submits the use!name and passwo!d in

pa!amete!s 6Ruse!name and 6Rpasswo!d to 6Rsecu!it+Rchec Qinput t+pe5passwo!d5  is

used 3o! passwo!d tet eld, which displa+s the passwo!d as [s

 The !esponseencodeU?("R#) encodes the U? 6Rsecu!it+Rchec b+ includin% the session

I. i3 U?'!ew!itin% is used 3o! session t!acin%C it !etu!ns the U? unchan%ed i3 cooie is

Page 45: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 45/52

used <o! !obust session t!acin%, all U?s emitted b+ se!#e!'side p!o%!ams (se!#letG"S$)

should be !un th!ou%h this method

I3 lo%in 3ails, use! will be !edi!ected to e!!o!6sp pa%e, as 3ollows,

Qhtml

QheadQtitle7!!o! $a%e <o! 7amplesQGtitleQGhead

Qbod+

In#alid use!name andGo! passwo!d, please t!+ a%ain

Qa h!e3Q` !esponseencodeU?(5inde6sp5) `a%ainQGa

QGbod+

QGhtml

I3 lo%in succeeds, the use! will %et the pa%e he !e-uested 3o! Stud+ the

5eamples6spsecu!it+p!otectedinde6sp5 sou!ce

•  To lo%o, te!minate the cu!!ent session #ia sessionin#alidate()

•  4ou can use !e-uest%etemoteUse!() to %et the authenticated lo%inuse!nameC !e-uest%etUse!$!incipal() to %et a 6a#asecu!it+$!incipal ob6ect containin%

the name o3 the cu!!ent authenticated use!C !e-uestisUse!Inole(role) to chec i3 the

authenticated use! is included in the specied role

. HTTPS

In <OM'based authentication, the use!nameGpasswo!d a!e sent in clea! tet, and

susceptible to ea#esd!oppin% Hence, it is impo!tant to enc!+pt the t!anspo!t b+ tu!nin%

on SS? (HTT$S) ead 5 Tomcat with SS?5 on how to setup Tomcat with SS?

 To en3o!ce use! to use secu!e t!anspo!t (HTT$S), add a Qt!anspo!t'

%ua!antee=O1<I.71TIA?QGt!anspo!t'%ua!antee, inside the Qsecu!it+'const!aint, as

3ollows:

Qsecu!it+'const!aint

  Qdispla+'name7ample Secu!it+ =onst!aintQGdispla+'name

 Qweb'!esou!ce'collection

  Qweb'!esou!ce'name$!otected A!eaQGweb'!esou!ce'name

  Qu!l'patte!nG6spGsecu!it+Gp!otectedG[QGu!l'patte!n

 

 QGweb'!esou!ce'collection

  Qauth'const!aint

  Q!ole'nametomcatQG!ole'name

 

  QGauth'const!aint

  Q'' must use SS? 3o! secu!e t!anspo!t ''

Page 46: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 46/52

  Quse!'data'const!aint

  Qt!anspo!t'%ua!antee=O1<I.71TIA?QGt!anspo!t'%ua!antee

  QGuse!'data'const!aint

QGsecu!it+'const!aint

All accesses to HTT$ at po!t 88 (e%,http:GGlocalhost:88GeamplesG6spGsecu!it+Gp!otectedGinde6sp) will be !edi!ected to

HTT$S at po!t 8DD& (e%,

https:GGlocalhost:8DD&GeamplesG6spGsecu!it+Gp!otectedGinde6sp)

.F HTTP BASIC Autentication

HTT$ denes two access authentication schemes to !e-uest 3o! use!nameGpasswo!d:

Basic and .i%est ead 5HTT$ Authentication5

 To use BASI= authentication, chan%e the Qlo%in'con%s Qauth'method to BASI=

Qlo%in'con%

  Qauth'methodBASI=QGauth'method

  Q!ealm'nameBasic Authentication A!eaQG!ealm'name

  Q'' emo#ed, no applicable 3o! BASI= authentication

  Q3o!m'lo%in'con%

  Q3o!m'lo%in'pa%eG6spGsecu!it+Gp!otectedGlo%in6spQG3o!m'lo%in'pa%e

  Q3o!m'e!!o!'pa%eG6spGsecu!it+Gp!otectedGe!!o!6spQG3o!m'e!!o!'pa%e

  QG3o!m'lo%in'con%

  ''

QGlo%in'con%

In BASI= authentication, Tomcat uses the HTT$ *asic Authentication to as 3o! use!name

and passwo!d T!+ http:GGlocalhost:88GeamplesG6spGsecu!it+Gp!otectedGinde6sp, +ou willbe p!ompted 3o! use!nameGpasswo!d automaticall+ The!e is no !edi!ect to lo%in6sp and

no need to w!ite the lo%in6sp

A%ain, the HTT$ Basic Authentication sends the use!name and passwo!d in clea! tet

(passwo!d is encoded in Base2D, but not enc!+pted) It is totall+ insecu!e, unless +ou

use a secu!e t!anspo!t (HTT$S) o! E$1 (Ei!tual $!i#ate 1etwo!)

 The Tomcats webapp mana%e! (unde! webappsGmana%e!) uses BASI= authentication

.G HTTP DI*,ST Autentication

 To use .I7ST authentation, chan%e the Qlo%in'con%s Qauth'method to .I7ST In

.I7ST authentication, Tomcat uses HTT$ +i'est Authentication %cheme to as 3o!use!nameGpasswo!d Instead o3 sendin% passwo!d in clea! tet, the di'est  o3 passwo!d is

send to the se!#e! Althou%h .I7ST authentication is mo!e secu!e than BASI=

authentication, HTT$S is much mo!e secu!e

Qlo%in'con%

  Qauth'method.I7STQGauth'method

Page 47: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 47/52

  Q!ealm'name.i%est Authentication A!eaQG!ealm'name

QGlo%in'con%

.M +DBC:eal"

Use!.atabaseealm  is not meant 3o! se!ious p!oduction en#i!onment, as it is ha!d to

maintain ".B=ealm is mo!e app!op!iateIn ".B=ealm, use! in3o!mation is ept in a !elational database, such as M+S>?,

accessed #ia ".B=, instead o3 an XM? le The in3o!mation can be secu!ed th!uou%h

p!ope! database secu!it+Settin! up Database

We shall set up ou! use! database in M+S>? ead 5How to Install M+S>? and et

Sta!ted5 i3 +ou a!e new to M+S>?

 The 3ollowin% sc!ipt can be used to set up the use! database Two tables a!e !e-ui!ed:

a use!s table containin% use!name and passwo!d, and a use!R!oles containin% use!name

and the !ole assi%ned

c!eate database tomcatRuse!sC

 

use tomcatRuse!sC

 

c!eate table use!s (

  use!name #a!cha!(0F) not null,

  passwo!d #a!cha!(0F) not null,

  p!ima!+ e+ (use!name)

)C

 

c!eate table use!R!oles (

  use!name #a!cha!(0F) not null,

  !ole #a!cha!(0F) not null,

  p!ima!+ e+ (use!name, !ole)

)C

 

inse!t into use!s #alues

(tomcat, tomcat),

(both, tomcat),

(!ole0, tomcat)C

 

Page 48: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 48/52

inse!t into use!R!oles #alues

(tomcat, tomcat),

(!ole0, !ole0),

(both, tomcat),

(both, !ole0)C

 +DBC Driver

1et, cop+ the M+S>?s ".B= d!i#e! (5m+s-l'connecto!'6a#a'F0N'bin6a!5) into

 Tomcats lib (5Q=ATA?I1ARHOM7lib5) ead 5How to Install M+S>? and et Sta!ted5<con&=server.%"l<

A%ain, the !ealm is dened in se!#e!ml #ia a Qealm element In this case,

a ".B=ealm, with a connectionU? p!o#idin% a M+S>? database connection

Qealm class1ame5o!%apachecatalina!ealm ".B=ealm5

  d!i#e!1ame5comm+s-l6dbc.!i#e!5

  connectionU?56dbc:m+s-l:GGlocalhost:po!tNGtomcatRuse!s5  connection1ame5dbuse!N5

  connection$asswo!d5dbpasswo!dN5

  use!Table5use!s5 use!1ame=ol5use!name5 use!=!ed=ol5passwo!d5

  use!oleTable5use!R!oles5 !ole1ame=ol5!ole5 G

eplace the po!tN with +ou! M+S>? se!#e! po!t numbe!,

and dbuse!N and dbpassN with an autho!i9ed M+S>? use!nameGpasswo!d<ContextRoot =W,BI>=web.%"l<

Same as Use!.atabaseealmAutentication 3etods

Same as Use!.atabaseealm, +ou can use <OM, BASI= o! .I7ST authenticationmethodTestin!

 4ou need to sta!t M+S>? se!#e! be3o!e sta!tin% the Tomcat Se!#e!

. Sin!le ?o!in

B+ de3ault, each p!otected webapp would !e-uest 3o! lo%in du!in% the !st access 4ou

can enable sin%le lo%in to all webapps unde! the host b+ uncommentin% the sin%le'lo%in

#al#e, as 3ollows:

QHost

  Q'' Sin%leSi%nOn #al#e, sha!e authentication between web applications

  .ocumentation at: GdocsGcon%G#al#ehtml ''

  QEal#e class1ame5o!%apachecatalinaauthenticato!Sin%leSi%nOn5 G

QGHost

.E To"cat wit SS?

Page 49: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 49/52

SS? (%ecure %oc,et #ayer ), allows web b!owse!s and web se!#e!s to communicate o#e!

a secu!ed (enc!+pted) connection Tomcat p!o#ides built'in suppo!t 3o! SS?

ead:

• 5SS? =on%u!ation How'to5 o3 Tomcat .ocumentation J

5Q=ATA?I1ARHOM7webappsdocsssl'howtohtml 5

• 5e+tool ' /e+ and =e!ticate Mana%ement Tool5 J "./ documentation

 The steps to tu!n on SS? suppo!t a!e:

Step 1- =hec +ou! "./ #e!sion Tomcats SS? uses "a#a Secu!e Socet 7tension

("SS7), which has been inte%!ated into "./ since 0D

Step #- $!epa!e the Tomcats se!#e! ce!ticate, usin% the "./s /e+ and =e!ticate

Mana%ement Tool called 5e+tool5 (in 5Q"AEARHOM7bin5 ), as 3ollows:

e$tool

displa+ the help menu  

GG ene!ate a sel3'si%ned ce!ticate 3o! Tomcat

e$tool !ene$ alias to"cat e$al! :SA e$store

OT43CAT9H43,=con&=.e$store

7nte! e+sto!e passwo!d: %%%%%%%%

e'ente! new passwo!d: %%%%%%%%

What is +ou! !st and last name\

  YUnnownZ:

What is the name o3 +ou! o!%ani9ational unit\

  YUnnownZ:

What is the name o3 +ou! o!%ani9ation\

  YUnnownZ:What is the name o3 +ou! =it+ o! ?ocalit+\

  YUnnownZ:

What is the name o3 +ou! State o! $!o#ince\

  YUnnownZ:

What is the two'lette! count!+ code 3o! this unit\

  YUnnownZ:

Is =1Unnown, OUUnnown, OUnnown, ?Unnown, STUnnown, =Unnown

co!!ect\

  YnoZ: $

7nte! e+ passwo!d 3o! Qtomcat

  (7TU1 i3 same as e+sto!e passwo!d):

•  The 5'%ene+5 option is used to %ene!ate a public'p!i#ate e+ pai! The public e+

is w!apped into an XFK #0 sel3'si%ned ce!ticate The ce!ticate and the p!i#ate

e+ a!e sto!ed in a new e+sto!e ent!+ identied b+ the alias In ou! case, the alias

name must be 5tomcat5

•  The 5'e+al%5 option species the e+ %ene!ation al%o!ithm SA public e+

al%o!ithm is used in this case

Page 50: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 50/52

•  The 5'e+sto!e5 option species the name and location o3 the e+ sto!e le

•  The passwo!d 3o! alias tomcat must be the same as the e+sto!e (ie, hit ente!

3o! the last -uestion)

Step '- 7nable SS? suppo!t 3o! Tomcat SS? is built into Tomcat The Tomcats

con%u!ation le commented out the SS? con%u!ation di!ecti#e Uncomment them b+

!emo#in% the Q'' and '' a!ound the SS? =o+ote HTT$G00 =onnecto! as 3ollows:

Q'' .ene a SS? HTT$G00 =onnecto! on po!t 8DD&

  This connecto! uses the "SS7 con%u!ation, when usin% A$, the

connecto! should be usin% the OpenSS? st+le con%u!ation

  desc!ibed in the A$ documentation ''

Q=onnecto! po!t58DD&5 protocol<or!.apace.co$ote.ttp11.Http11Protocol<

  SS?7nabled5t!ue5 maTh!eads50F5 scheme5https5 secu!e5t!ue5

  clientAuth53alse5 ssl$!otocol5T?S5

e$storeile<OT43CAT9H43,=con&=.e$store<

  e$storePass< passwordOfKeyStore< G

1ote that the SS? (o! HTT$S) is !unnin% on po!t 8DD& instead o3 its de3ault po!t numbe!

DD&

Add in the e+sto!e<ile and e+Sto!e$ass att!ibutes The e+sto!e<ile att!ibute specied

the location o3 the e+sto!e le The e+Sto!e$ass p!o#ides the passwo!d 3o! accessin%

the e+sto!e le

Step - Sta!t +ou! tomcat (!un 5Q=ATA?I1ARHOM7binsta!tupbat 5) A3te! that, sta!t a

web b!owse! and issue an HTT$S !e-uest as 3ollows:

https:GGlocalhost:8DD&

F. Clusterin!

YTO.OZ

F.1 Con;!urin! 8irtual Hosts

 To set up a #i!tual host called 5wwwm+testcom5 (suppose that +ou ha#e !e%iste!ed this

hostname with at static I$ add!ess) Include the 3ollowin% QHost element

in se!#e!ml unde! the 7n%ine =atalina:

Q7n%ine name5=atalina5

  QHost name5localhost

 

  QGHost

  QHost name5wwwm+testcom5 appBase5webappsRm+testcom5

  unpacWAs5t!ue5 auto.eplo+5t!ue5

  QAliasm+testcomQGAlias

  QEal#e class1ame5o!%apachecatalina#al#esAccess?o%Eal#e5

Page 51: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 51/52

  di!ecto!+5lo%s5

  p!e5m+testcomRaccessRlo%5 su@5lo%5

  patte!n5`h `l `u `t -uotC`!-uotC `s `b5

  !esol#eHosts53alse5 G

  QGHost

QG7n%ine

 The abo#e lines con%u!e a #i!tual host with hostname 5wwwm+testcom5, with

webapps base di!ecto!+ at 5Q=ATA?I1ARHOM7webappsRm+testcom 5 We also dene a

alias called 5m+testcom5 That is, this host can be accessed

#ia http:GGwwwm+testcom:po!t o! http:GGm+testcom:po!t We also dene a Eal#e, which

inte!cepts the !e-uest messa%e to w!ite a lo% ent!ies (simila! to localhost)

1et:

0 =!eate a di!ecto!+ 5webappsRm+testcom5 unde! Q=ATA?I1ARHOM7, acco!din% to

the appBase

; =!eate a web application called OOT, b+ c!eatin% a di!ecto!+ OOT unde! the

5webappsRm+testcom5 ecall that OOT was con%u!ed with an empt+ st!in% U?

In othe! wo!ds,http:GGwwwm+testcom:po!tG accesses the OOT application

& =!eate a di!ecto!+ 5wwwm+testcom5 unde! 5con3=atalina5

D W!ite a welcome pa%e called 5indehtml5 and sa#e it in

5webappsRm+testcomOOT5

F Qhtml

2 QheadQtitleTestin% Ei!tual HostQGtitleQGhead

* Qbod+

8 Qh0It7s wor on virtual ostQGh0

K QGbod+

QGhtml

 To test the #i!tual host, without !e%iste!in% the hostname with an IS$, edit

5=:WindowsS+stem&;d!i#e!setchosts5 to include the 3ollowin% lines (!e-ui!ed

administ!ati#e autho!it+):

0;*0 wwwm+testcom

0;*0 m+testcom

 These lines maps host names wwwm+testcom and m+testcom to I$ add!ess 0;*0,

which is the localhost As the I$ so3twa!e checs the host le be3o!e asin% .omain

1ame Se!#ice (.1S) to !esol#e a host name, +ou willl be able to test +ou! #i!tual host

1ow, +ou a!e !ead+ to test the #i!tual hosts Sta!t the Tomcat se!#e! and issue these

U?:

Page 52: How to Install Apache Tomcat 8

8/18/2019 How to Install Apache Tomcat 8

http://slidepdf.com/reader/full/how-to-install-apache-tomcat-8 52/52

http:GGwwwm+testcom:88

http:GGm+testcom:88

http:GGwwwm+testcom:88G

http:GGm+testcom:88G

http:GGwwwm+testcom:88Gindehtml

http:GGm+testcom:88Gindehtml

F.# To"cat Host 3ana!er &or Clusterin! and Ad"in:oles

e3e!ence: 5=luste!in%GSession eplication HOW'TO5 J 5webappsGdocsGcluste!'

howtohtml5

 The Tomcat 5host'mana%e!5 webapp allows +ou to mana%e Tomcat cluste!s To

access host'mana%e!, +ou need to dene a use! with admin !ole

 The admin !ole has been sepa!ated out as admin'%ui and admin'sc!ipt, in Tomcat *, asdened in webappsGhost'mana%e!GW7B'I1<Gwebml

 The 5Host Mana%e!5 web application can be accessed #ia http:GGhostN:po!tNGhost'

mana%e!Ghtml