Lab 3 - IPFW.edu

coldwaterphewServers

Nov 17, 2013 (3 years and 7 months ago)

87 views


P.I. Lin

Lab 3


UML and JavaServlet

LAB 3


UML for small Java Servlet Development


OBJECTIVE AND BACKGROUND


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 java
Servlet class. At the end of

this lab, you should be able to run a simple Hello
servlet class using Tomcat 4.0.1 server on localhost.


WEB SITE REFERENCES




Professor Lin’s Web site:
http://www.etcs.ipfw.edu/~lin



Rational, the Software De
velopment Company
www.rational.com



Jakarta Tomcat 4.0.1
http://jakarta.apache.org/builds/jakarta
-
tomcat
-
4.0/release/v4.0.1/bin/



http://java.sun.com/docs/books/tutorial/servlets/index.html




EQUIPMENT AND SOFTWARE



PC (256 Mbytes, Windows 95/98/2000 OS)

Rational Rose Enterprise Software



SUN Java Developme
nt Tool kit J2SE (standard edition) or J2EE
(enterprise edition)



Notepad editor


PROCEDURE


a) Creating a simple servlet (HelloWWW.java)


Step 1: Run Rational Rose Enterprise

Step 2: Create a Use Case Diagram

1.

Click on
Use

Case View > Main



P.I. Lin

Lab 3


UML and JavaServlet



2.

From the me
nu bar select
Tool > Create > Use Case


3.

Select the
Package

from the Toolbox and place it into the Use Case
Diagram

4.

Right click on the package to see the pop
-
up menu, and then select
Open
Specification
menu

5.

Change the name of the package to
Browser
.

6.

Create
HTML pages
,
HTTP
,
Web server
, and
Server pages packages
.

7.

Use dependency arrow to associate packages.

8.

Between
Web server

and
Server pages
, draw dependency arrow once
from
Web server

to
Server pages
. And draw another arrow again from
Server pages

to
Web s
erver
. Do the same for
HTML pages

and
Server
pages
.


P.I. Lin

Lab 3


UML and JavaServlet

Browser
HTTP
Web server
Server pages
HTML pages

Step 3: Create HelloWWW Class Diagram

(classes, attributes, and operations)

1.

Click on
Browse>Class Diagram
. Make sure
Logical View

is selected in
the package windows. Select
New

to create a new logical

view.




2.

Give the Diagram title name as Main.



P.I. Lin

Lab 3


UML and JavaServlet




3.

Click on Use
Logical View > Main





4.

Create a new class called “HelloWWW”

-

Tools > Java/J2EE > New Servlet

-

Type in “HelloWWW” at Servlet Name text field and make sure “extends
HttpServlet
“and
GET

f
rom HTTP Request Handlers are checked.



P.I. Lin

Lab 3


UML and JavaServlet






3. The following class will appear in the logical view workspace.












P.I. Lin

Lab 3


UML and JavaServlet


Step 4. Code Generation

1.

From the menu bar, select

Tools > Java/J2EE > Project Specification
.







2.

Double
-
click on the last one

of the Classpaths as shown in the following
diagram.

3.

Click on the button with three dots to see a dialog box.

4.

Choose
Directory
.







P.I. Lin

Lab 3


UML and JavaServlet










5.

Right click on the class diagram and select

Java/J2EE> Generate Code
.
Choose your working directory and c
lick “
OK
”.




P.I. Lin

Lab 3


UML and JavaServlet




The following codes in
HelloWWW.java

are generated



//

//
--

Java Code Generation Process
--


// Import Statements

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;


public class HelloWWW extends javax.servlet.http.
HttpServlet

{




/**


* @roseuid 3C59EE93007C


* @J2EE_METHOD
--

HelloWWW


*/


public HelloWWW ()


{



}




/**


* @roseuid 3C59EE93009A


* @J2EE_METHOD
--

doGet


P.I. Lin

Lab 3


UML and JavaServlet


* Called by the server (via the servic
e method) to allow a servlet to handle a
GET request.


* The servlet container must write the headers before committing the
response, because


* in HTTP the headers must be sent before the response body. The GET
method should


* be safe and ide
mpotent. If the request is incorrectly formatted, doGet returns
an


* HTTP 'Bad Request' message.


*/


public void doGet (javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)


throws
javax.servlet.ServletException, java.io.IOException


{



}

}

Step 5. Code Editing


//

//
--

Java Code Generation Process
--


// Import Statements

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;


public class HelloWWW extends
javax.servlet.http.HttpServlet

{




/**


* @roseuid 3C59F57A0074


* @J2EE_METHOD
--

HelloWWW


*/


public HelloWWW ()


{



}




/**


* @roseuid 3C59F57A007D


* @J2EE_METHOD
--

doGet


* Called by the ser
ver (via the service method) to allow a servlet to handle a
GET request.


* The servlet container must write the headers before committing the
response, because


P.I. Lin

Lab 3


UML and JavaServlet


* in HTTP the headers must be sent before the response body. The GET
method should



* be safe and idempotent. If the request is incorrectly formatted, doGet returns
an


* HTTP 'Bad Request' message.


*/


public void doGet (javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)



throws javax.servlet.ServletException, java.io.IOException


{


response.setContentType("text/html");


PrintWriter out = response.getWriter();


out.println("<html>");


out.println("<body>");


out.prin
tln("<head>");


out.println("<title>Hello WWW!</title>");


out.println("</head>");


out.println("<body>");


out.println("<h1>Hello WWW!</h1>");


out.println("</body>");


out.println("</html>");



}

}




Step 6.
Compiling and Running the servlet

1.

To compile
HelloWorld.java
from command line, open a command shell.

2.

Make sure the path is set to
c:
\
jdk1.3.1_02
\
bin

or the directory where
Java Development Kit (JDK) was installed.

3.

Make sure you have jdk1.3.1_02 installed.

And also javax.servlet API is
installed in the working directory of the source code.

4.

Run
javac

program with the source file
HelloWorld.java

as the parameter
name.
C:>lab3> javac HelloWorld.java

Or Using Jbuilder 5

5.

Launch Jbuilder 5.0; Create a new pr
oject.


P.I. Lin

Lab 3


UML and JavaServlet



6.

Add java file HelloWWW.java to the project.






P.I. Lin

Lab 3


UML and JavaServlet


7.

Click on
project>properties.
Click on
Required Libraries

tab.

Make
sure that Tomcat 3.2 Servlet and Servlet libraries are added. If not click
on
Add
and add those.



8.

Compile the java file.

9.

Lo
ok for the HelloWWW.class file in your working directory.

10.

Download and install Tomcat 4.0.1 from
http://jakarta.apache.org/builds/jakarta
-
tomcat
-
4.0/release/v4.0.1/bin/


11.

Place your HelloWWW.class file into WEB_INF folder to Tomcat Directory.
C:
\
Program Files
\
Apache Tomcat4.0
\
webapps
\
examples
\
WEB
-
INF
\
classes
\



P.I. Lin

Lab 3


UML and JavaServlet



12.

Run your servlet program from any web browser by typing in

http://localhost:8080/examples/servlet/HelloWWW




13. Also upload this Servlet to your Linux Web server and prepare a HTML page
to run this Servlet.


Questions

(You may try to search through the Web for information):

1.

What is the CGI (Comm
on Gateway Interface) protocol?

2.

What supporting programs are needed for running a Servlet.

3.


Comparing Perl CGI programs, Java Servlet, JSP (Java Server page),
and Microsoft ASP (Application Server Page).