You are on page 1of 13

LAB 3

UML for small Java Servlet Development


OBJECTIVE AND BACKG OUND The purpose of this second UML lab is to familiarize programmers with Java Servlet development in Rational Rose. We will learn to use UML to create ava Servlet class. !t the end of this lab" #ou should be able to run a simple $ello servlet class using Tomcat %.&.' server on localhost. !EB SITE E"E ENCES (rofessor Lin)s Web site* http*++www.etcs.ipfw.edu+,lin Rational" the Software -evelopment .ompan# www.rational.com Ja/arta Tomcat %.&.' http*++ a/arta.apache.org+builds+ a/arta0tomcat0 %.&+release+v%.&.'+bin+ http*++ ava.sun.com+docs+boo/s+tutorial+servlets+inde1.html

E#UI$MENT AND SO"T!A E (. 2345 Mb#tes" Windows 64+67+3&&& 8S9 Rational Rose :nterprise Software SU; Java -evelopment Tool /it J3S: 2standard edition9 or J3:: 2enterprise edition9 ;otepad editor $ OCEDU E a% Creat&n' a s&mple servlet ()ello!!!*+ava% Step ,- .n at&onal ose Enterpr&se Step /- Create a Use Case D&a'ram '. .lic/ on Use Case V&e0 1 Ma&n

(.<. Lin

Lab = > UML and JavaServlet

3. ?rom the menu bar select Tool 1 Create 1 Use Case =. Select the $a23a'e from the Toolbo1 and place it into the Use .ase -iagram %. Right clic/ on the pac/age to see the pop0up menu" and then select Open Spe2&f&2at&on menu 4. .hange the name of the pac/age to Browser. 5. .reate HTML pages" HTTP" Web server" and Server pages packages. @. Use dependenc# arrow to associate pac/ages. 7. Aetween Web server and Server pages" draw dependenc# arrow once from Web server to Server pages. !nd draw another arrow again from Server pages to Web server. -o the same for HTML pages and Server pages.

(.<. Lin

Lab = > UML and JavaServlet

Arowser

$TT(

Web server

$TML pages

Server pages

Step 3- Create )ello!!! Class D&a'ram 2classes" attributes" and operations9 '. .lic/ on Bro0se1Class D&a'ram. Ma/e sure Lo'&2al V&e0 is selected in the pac/age windows. Select Ne0 to create a new logical view.

3. Bive the -iagram title name as Main.

(.<. Lin

Lab = > UML and JavaServlet

=. .lic/ on Use Lo'&2al V&e0 1 Ma&n

%. .reate a new class called C$elloWWWD 0 Tools 1 Java4J/EE 1 Ne0 Servlet 5 T#pe in C$elloWWWD at Servlet ;ame te1t field and ma/e sure Ce1tends )ttpServletCand GET from $TT( ReEuest $andlers are chec/ed.

(.<. Lin

Lab = > UML and JavaServlet

=. The following class will appear in the logical view wor/space.

$elloWWW $elloWWW29 doBet29

(.<. Lin

Lab = > UML and JavaServlet

Step 6* Co7e Generat&on '. ?rom the menu bar" select Tools 1 Java4J/EE 1 $ro+e2t Spe2&f&2at&on .

3. -ouble0clic/ on the last one of the .lasspaths as shown in the following diagram. =. .lic/ on the button with three dots to see a dialog bo1. %. .hoose D&re2tor8.

(.<. Lin

Lab = > UML and JavaServlet

4. Right clic/ on the class diagram and select Java4J/EE1 Generate Co7e. .hoose #our wor/ing director# and clic/ COKD.

(.<. Lin

Lab = > UML and JavaServlet

The following codes in )ello!!!*+ava are generated ++ ++ 00 Java .ode Beneration (rocess 00 ++ <mport Statements import ava.io.FG import ava1.servlet.FG import ava1.servlet.http.FG public class $elloWWW e1tends ava1.servlet.http.$ttpServlet H +FF F Iroseuid =.46::6=&&@. F IJ3::JM:T$8- 00 $elloWWW F+ public $elloWWW 29 H K +FF F Iroseuid =.46::6=&&6! F IJ3::JM:T$8- 00 doBet

(.<. Lin

Lab = > UML and JavaServlet

F .alled b# the server 2via the service method9 to allow a servlet to handle a B:T reEuest. F The servlet container must write the headers before committing the response" because F in $TT( the headers must be sent before the response bod#. The B:T method should F be safe and idempotent. <f the reEuest is incorrectl# formatted" doBet returns an F $TT( LAad ReEuestL message. F+ public void doBet 2 ava1.servlet.http.$ttpServletReEuest reEuest" ava1.servlet.http.$ttpServletResponse response9 throws ava1.servlet.Servlet:1ception" ava.io.<8:1ception H K K Step 9* Co7e E7&t&n' ++ ++ 00 Java .ode Beneration (rocess 00 ++ <mport Statements import ava.io.FG import ava1.servlet.FG import ava1.servlet.http.FG public class $elloWWW e1tends ava1.servlet.http.$ttpServlet H +FF F Iroseuid =.46?4@!&&@% F IJ3::JM:T$8- 00 $elloWWW F+ public $elloWWW 29 H K +FF F Iroseuid =.46?4@!&&@F IJ3::JM:T$8- 00 doBet F .alled b# the server 2via the service method9 to allow a servlet to handle a B:T reEuest. F The servlet container must write the headers before committing the response" because

(.<. Lin

Lab = > UML and JavaServlet

F in $TT( the headers must be sent before the response bod#. The B:T method should F be safe and idempotent. <f the reEuest is incorrectl# formatted" doBet returns an F $TT( LAad ReEuestL message. F+ public void doBet 2 ava1.servlet.http.$ttpServletReEuest reEuest" ava1.servlet.http.$ttpServletResponse response9 throws ava1.servlet.Servlet:1ception" ava.io.<8:1ception H response*setContentT8pe(:te;t4<tml:%= $r&nt!r&ter o.t > response*'et!r&ter(%= o.t*pr&ntln(:?<tml1:%= o.t*pr&ntln(:?@o781:%= o.t*pr&ntln(:?<ea71:%= o.t*pr&ntln(:?t&tle1)ello !!!A?4t&tle1:%= o.t*pr&ntln(:?4<ea71:%= o.t*pr&ntln(:?@o781:%= o.t*pr&ntln(:?<,1)ello !!!A?4<,1:%= o.t*pr&ntln(:?4@o781:%= o.t*pr&ntln(:?4<tml1:%= K K

Step B* Comp&l&n' an7 .nn&n' t<e servlet '. To compile )ello!orl7*+ava from command line" open a command shell. 3. Ma/e sure the path is set to 2-C+73,*3*,DE/C@&n or the director# where Java -evelopment Mit 2J-M9 was installed. =. Ma/e sure #ou have d/'.=.'J&3 installed. !nd also ava1.servlet !(< is installed in the wor/ing director# of the source code. %. Run +ava2 program with the source file )ello!orl7*+ava as the parameter name. C-1la@31 +ava2 )ello!orl7*+ava 8r Using Jbuilder 4 4. Launch Jbuilder 4.&G .reate a new pro ect.

(.<. Lin

Lab = > UML and JavaServlet

5. !dd ava file $elloWWW. ava to the pro ect.

(.<. Lin

Lab = > UML and JavaServlet

@. .lic/ on project>properties. .lic/ on Required Libraries tab. Ma/e sure that Tomcat =.3 Servlet and Servlet libraries are added. <f not clic/ on A77 and add those.

7. .ompile the ava file. 6. Loo/ for the $elloWWW.class file in #our wor/ing director#. 10. -ownload and install Tomcat %.&.' from http*++ a/arta.apache.org+builds+ a/arta0tomcat0%.&+release+v%.&.'+bin+ ''. (lace #our $elloWWW.class file into W:AJ<;? folder to Tomcat -irector#. .*N(rogram ?ilesN!pache Tomcat%.&NwebappsNe1amplesNW:A0 <;?NclassesN

(.<. Lin

Lab = > UML and JavaServlet

'3. Run #our servlet program from an# web browser b# t#ping in http*++localhost*7&7&+e1amples+servlet+$elloWWW

'=. !lso upload this Servlet to #our Linu1 Web server and prepare a $TML page to run this Servlet. #.est&ons 2Oou ma# tr# to search through the Web for information9* '. What is the .B< 2.ommon Batewa# <nterface9 protocolP 3. What supporting programs are needed for running a Servlet. =. .omparing (erl .B< programs" Java Servlet" JS( 2Java Server page9" and Microsoft !S( 2!pplication Server (age9.

(.<. Lin

Lab = > UML and JavaServlet