95-702 Organizational Communications and Distributed Object Technologies August 2007
Installation Lab
================
All students enrolled in 95-702 OCT must complete Part I of this lab
during the first ten days of class. Part II may be postponed until
later in the course when we begin to study web services.
In subsequent assignments, it will be assumed that the student
has completed this lab and has a working system up and running.
So, be sure to let us know as soon as possible if you are not
able to complete every step in this lab.
PART I. Installation of Java, Tomcat, Xerces, Eclipse and TcpMon
================================================================
0. Please try to avoid directory paths using spaces. Use an
underscore character instead.
1. Make a directory for downloads. Call it c:\downloads.
2. Download the following files
from www.andrew.cmu.edu/~mm6/95-702/downloads and
place them in your c:\download directory:
jdk-1_5_0_08-windows-i586-p (JDK and JRE from Sun)
apache-tomcat-5.5.17.exe (Apache Tomcat Install)
eclipse-SDK-3.2-win32.zip (Eclipse IDE)
tomcatPluginV31 (Eclipse plugin from Sysdeo)
Xerces-J-bin.2.8.0.zip (Xerces XML Parser from Apache)
3. Expand the Java to C:\Java.
Within this directory you should have C:\Java\jdk1.5.0_08, etc..
Double click and expand Tomcat.
Select Full Install.
Install to C:\Tomcat_5.5 (Note the use of underscore rather than space.)
Set Username and Password and remember these.
Set the port to 6502 (to stay away from Oracle's Tomcat).
Select the installed JRE that is under the new JDK that we just installed.
Don't use any other JRE. The last token in the path should be "jre".
Within the C:\Tomcat_5.5 directory you should have
C:\Tomcat_5.5\bin, C:\Tomcat_5.5\common, etc.
Expand eclipse to C:\eclipse
Within this directory you should have C:\eclipse\eclipse.exe, etc.
Expand plugin to C:\EclipseTomcatPlugin
Within this directory you should have
C:\EclipseTomcatPlugin\..\com.sysdeo.eclipse.tomcat_3.1.0
Copy the entire directory com.sysdeo.eclipse.tomcat_3.1.0 to
C:\eclipse\plugins.
This allows us to access Tomcat from within Eclipse.
Expand Xerces to C:\Xerces
Within this directory you should have xerces-2_8_0/xercesImpl.jar.
Xerces is an XML parser.
4. Use Start/Control Panel/Performance and Maintenance/System/
Advanced/Environment Variables/System Variables/New
to set the following:
JAVA_HOME C:\Java\jdk1.5.0_08
JRE_HOME C:\Java\jdk1.5.0_08\jre
Path C:\Java\jdk1.5.0_08\jre\bin;C:\Java\jdk1.5.0_08\bin;{everything already there follows here}
5. Make a shortcut to eclipse and place it on your desktop. Right click
on shortcut properties and set target to
C:\eclipse\eclipse.exe -vm C:\Java\jdk1.5.0_08\jre\bin\javaw
Note that we are pointing to the JDK we just installed.
6. Testing from the DOS prompt:
C:\>java -version
java version "1.5.0_08"
C:\>javac -version
javac 1.5.0_08
C:\>echo %JAVA_HOME%
C:\Java\jdk1.5.0_08
C:\>echo %JRE_HOME%
C:\Java\jdk1.5.0_08\jre
7. Double click eclipse.
Choose a workspace like C:\97-702\EclipseWorkSpace1.
The following Tomcat setting that we will make apply to this eclipse
workspace. These setting will still be there the next time you
choose this workspace.
Choose workbench.
Select Windows/Preferences/Tomcat and select * Version 5.0
and set Tomcat home to C:\Tomcat_5.5
Select Windows/Preferences/Tomcat/Advanced and set Tomcat Base
to C:\Tomcat_5.5
Select Windows/Preferences/Tomcat/Tomcat Manager and set your
user name and password.
These are the same names that you entered earlier when you
installed Tomcat.
Apply the changes and click the little cat (Start Tomcat).
If you throw exceptions try stopping Tomcat and starting it again.
If you can't get Tomcat to run see your TA or instructor ASAP.
8. Visit Tomcat with your browser at http://localhost:6502
Run the servlets and JSP's under Examples. These should all work.
Select Tomcat Manager and see if your user name and password work.
Go back to eclipse and click the little cat to stop Tomcat.
Exit eclipse.
9. Publish a Java Server Page (JSP).
Double click eclipse.
Choose the same workspace like C:\97-702\EclipseWorkSpace1.
Choose workbench.
Examine Windows/Preferences/Tomcat and see if your old settings are still there.
Choose File/New/Other/Java/Tomcat Project/Next
Project Name Cool-JSP-project Finish
On the left pane (Package Explorer) expand the Cool-JSP-project (it should
contain WEB-INF as well as a host of Java Archives (jar files))
On the left pane select Cool-jsp-project and Right Click and select new file.
Enter the file name MyCoolPage.jsp and click finish.
You should see this file in the WEB-INF directory.
Enter this document in the editor:
<% java.util.Date d = new java.util.Date(); %>
Today is <%= d.toString() %>
Click the little disk to save everything.
Start Tomcat with the little cat.
Use a browser to visit http://localhost:6502/Cool-JSP-project/MyCoolPage.jsp
10. Another tutorial covering this same topic is at
http://www-128.ibm.com/developerworks/library/os-ectom/
11. Publish a servlet.
Choose File/New/Other/Java/Tomcat Project/Next.
Project Name ANewApp-project Finish
On left pane expand the ANewApp-project project (it should contain WEB-INF)
On left pane select ANewApp-project and Right Click and select new file.
Select ANewApp-project and enter the file name index.jsp and click Finish.
Enter this document in the editor:
Introductions
Click the little disk to save everything.
Select ANewApp-project and right click
Select new/class and give it the name HandleForm (we will use the default
package). Click Finish.
Enter this java servlet in the editor for HandleForm.java.
// HandleForm.java
// An introductory servlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HandleForm extends HttpServlet{
public void doGet(HttpServletRequest req,
HttpServletResponse response)
throws ServletException,
IOException {
String personName = req.getParameter("personName");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType = "\n";
out.println(docType +
"\n" +
"Hi" + personName + "\n" +
"\n" +
" Hi"+ personName + "
\n" +
"");
}
}
In the left pane, select the WEB-INF folder. This
is NOT the WEB-INF/src folder.
Right click WEB-INF and select new file.
Enter file name web.xml and Finish.
Enter this web.xml file. This file associates the name
ProcessForm with the servlet HandleForm.
TestServlet
HandleForm
TestServlet
/ProcessForm/*
Stop Tomcat and then start Tomcat again.
Use a browser to visit http://localhost:6502/ANewApp-project/index.jsp
Enter your name and hit submit. The servlet should execute.
(You may need to add servlet-api.jar to compile the servlet.
In Eclipse, right click the project. select Properties,
select Libraries and add an external jar - servlet-api.jar.)
12. Include an XML parser.
In Eclipse, select Window/Preferences/Tomcat/JVM Settings
Add a jar "(before generated) " C:\Xerces\xerces-2_8_0\xercesImpl.jar
13. Run an XML enabled stand alone Java program.
For stand alone XML applications you may need to include the
parser found in XercesImpl.jar on your Eclipse classpath.
File/New/Other/Java Project/Next/UsingXerces-project
Right click project for New/Class/NotifyStr
Copy NotifyStr.java (see below) into the editor.
To place XercesImp.jar on the classpath:
Window/Preferences/Java/Build Path/classpath Variables
Assign Xerces_Home to XercesImpl.jar path.
Right click project name for Properties/Java Build Path/
Libraries/Add Variable and select Xerces_Home and path
This program reads a URL from the command line. But we
want to work in eclipse.
To place the URL on the command line from within eclipse:
Select Run../Arguments
Enter the URL in the edit window. The URL (in this case) should
be a path to an XML file. It may be an http URL or a path
on the local file system, e.g., C:\Foo\SomeCool.xml.
// *************************************************************
// NotifyStr.java
// Adapted from XML and Java by Maruyama, Tamura and Uramoto
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class NotifyStr extends DefaultHandler
{
public static void main (String argv []) throws IOException, SAXException
{
if (argv.length != 1) {
System.err.println ("Usage: java NotifyStr filename.xml or URL");
System.exit (1);
}
XMLReader reader = XMLReaderFactory.createXMLReader(
"org.apache.xerces.parsers.SAXParser");
InputSource inputSource = new InputSource(argv[0]);
reader.setContentHandler(new NotifyStr());
reader.parse(inputSource);
System.exit (0);
}
public NotifyStr() {}
public void startElement(String namespaceURI, String localName,
String qName, Attributes aMap)
throws SAXException {
System.out.println(namespaceURI);
System.out.println(localName);
}
public void characters(char[] ch, int start, int length) throws
SAXException {
// build String from char array
String dataFound = new String(ch,start,length);
System.out.println("characters called:" + dataFound);
}
}
14. To submit homework
Select Project/File/Export/General/Archive File/Next/Name
blackboard site/tools/Digital Dropbox/
send the zip file to blackboard
15. Watch TCP traffic.
Install TcpMon to watch traffic:
TCPMon may be found at www.andrew.cmu.edu/~mm6/95-702/downloads/tcpmon-1.0-bin.zip
Download this file and de-compressed it to C:\tcpmon-1.0-bin.
Create a new DOS batch file called tcpmon.bat and place it in a c:\bats
directory.
Add the c:\bats directory to your path variable.
The content of this file is one line of code:
java -cp C:\tcpmon-1.0-bin\tcpmon-1.0-bin\build\tcpmon-1.0.jar org.apache.ws.commons.tcpmon.TCPMon
There is a very nice tutorial that comes with this install.
Testing
Enter tcpmon at the DOS prompt
Choose the admin tab
Set listen port to 8080 (or some available port for tcpmon)
Set target to 6502 (where Tomcat is listening)
Visit a Tomcat web application but go through tcpmon's port.
http://localhost:8080/Cool-JSP-project/MyCoolPage.jsp
Tcpmon will display the messages transferred back and forth.
PART II. Using Axis2 for Web Services
Prerequisite Installations
--------------------------
1) Tomcat is installed and is available through Eclipse.
2) The Axis2 Standard Binary Distribution has been installed from the
following site: http://ws.apache.org/axis2/download/1_2/download.cgi.
3) The Axis2 WAR (Web Archive) Distribution containing the axis2.war file
has been downloaded.
4) The file named "axis2.war" has been placed in Tomcat's directory structure at
.../apache-tomcat-6.0.10/webapps/axis2.war.
5) Axis2.war has been expanded into a directory called axis2 at
.../apache-tomcat-6.0.10/webapps/axis2.
6) The expansion of axis2.war into the directory axis2 is performed
by Tomcat.
7) The axis2 directory contains META-INF, WEB-INF and axis2-web
subdirectories.
8) A visit to http://localhost:6502/axis2
shows services, a validation tool and an administration tool.
9) Two axis2 plugins for Eclipse have been installed.
These are:
The code generator wizard at
http://ws.apache.org/axis2/0_94/CodegenTools-EclipsePlugin.html
The archiver wizard at
http://ws.apache.org/axis2/tools/1_1/eclipse/servicearchiver-plugin.html
10) These wizards are available from within Eclipe with
file/new/other/axis2 wizards.
11) Write a simple web service
Start Eclipse.
Choose any workspace name.
Create a Java Project with separate source and output folders:
file/new/project/java project/next/
Project name: HelloWorldWS
Create separate source and output folders
Finish
Create a package called ws.first:
file/new/package
name: ws.first
finish
This ws.first should be shown under src.
Create Java class called HelloWorldWebService.java.
This class will be in the ws.first package:
select ws.first
file/new/class
name: HelloWorldWebService
finish
Enter the following code in the file HelloWorldWebService.java:
package ws.first;
public class HelloWorldWebService {
public String hello(String name) {
return "Hi " + name;
}
public String goodBye(String name) {
return "goodbye " + name;
}
}
12. Create the services.xml file
Select the project.
File/new/file
Name: services.xml
Use the following code:
My Cool Hello world web service
ws.first.HelloWorldWebService
13. Place axis2 jar files in your classpath.
There are two possibilities here. If this is the first
time you have done this you need to associate a variable
name with all of the axis2 jar files. These instructions
follow:
If this is the first time do {
Select project HelloWorldWS.
Right click/ Properties/ Java Build Path.
Libraries/add library/user library/next/user libraries/new.
Enter the user libray name axis2_lib.
Select axis2_lib and select addJars.
This is a very tedious process but needs to be done only once.
Browse to axis2-1.1.1/lib and select one jar at a time.
When two jars have a different version number select only the one with
the latest version.
The pattern is select axis_lib, click add jar, double click an axis2
jar file, repeat.
All the jars should be under the name axis_lib.
When complete, choose to export this jar list to the file system.
You'll need a file name.
When you finish the Java Build Path will contain the JRE System
Library and the axis2_lib.
Both of these jar lists should appear under your project.
}
else (you already have performed this activity) do {
Right click project / properties
Select Libraries/ add library
Select user library
next/ User Libraries/ import / browse to the old library
Your project should have two large lists of jars
One from the JRE and the other from Axis2.
}
14. Build the archive file to be placed under Tomcat.
Select the project.
File/New/Other/Axis2 Wizards/Service Archiver/next
Class File Location
Browse to this project's bin directory
Check include class files only
select next
Select Skip WSDL select next
Add libraries? No, just select next.
Browse to the services.xml file.
Select next.
The output file location is under Tomcat.
Browse to ...tomcat/webapps/axis2/WEB-INF/services
The output file name is HelloWorld
Select Finish and the service archive is now behind Tomcat.
15. Prepare Tomcat.
Window/preferences/tomcat
Version 5.x
Browse to Tomcat Home (mine is /usr/local/apache-tomcat-6.0.10)
Advanced Tomcat Base browse to Tomcat Home
Do Not Check add java projects to Tomcat Classpath
JVM Settings: Set JRE
Source Path: Do Not check add java .... HelloWorldWS
Manager: User name and password
16. Run Tomcat
Click the cat.
No exceptions are thrown.
Visit http://localhost:8080/axis2
Look for services: HelloWorld
View the wsdl at http://localhost:8080/axis2/services/HelloWorld?wsdl
17. Write a web service client.
We'll work within the same project to create a client.
First we need to create a stub class. And for fun,
we'll generate a wsdl.
File/New/Other Axis2 Code Generation wizard
Generate WSDL from Java source next.
Postpone entering the file name.
Select add folder. Browse to this project's bin folder (not Tomcat bin).
Enter the qualified class name ws.first.HelloWorldWebService.
Click Test Class Loading (this may erase an error message.)
Select Next.
Take the defaults select Next again.
Browse and add wsdl to project on current eclipse workspace.
Be sure to actually browse to the workspace
and select the project name folder.
Hit Finish.
File refresh.
You should see the wsdl under the project.
Generate a stub from the wsdl to be used by a client.
Select the project.
File/New/Other/Axis2 Codegen
Generate Java source code from a WSDL
Browse to the wsdl under the project folder
Use the defaults select next
Select Browse and select a project on current eclipse workspace
Select finish
Create the client HelloWorldWebServiceClient in the package ws.first
package ws.first;
import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
public class HelloWorldWebServiceClient {
public static void main(String[] args) {
HelloWorldWebServiceStub stub;
try {
String c_value = "Mike";
stub = new HelloWorldWebServiceStub
("http://localhost:8080/axis2/services/HelloWorld");
System.out.println("Stub created");
HelloWorldWebServiceStub.Hello x
= new HelloWorldWebServiceStub.Hello();
x.setName(c_value);
System.out.println("set name called with " + c_value);
HelloWorldWebServiceStub.HelloResponse res
= stub.hello(x);
System.out.println("Returned");
System.out.println("C Value : "+c_value+ "\tResult : " +res.get_return());
} catch (AxisFault e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
Run the client. If all goes well you should see
Stub created
set name called with Mike
Returned
C Value : Mike Result : Hi Mike