35
國立台灣科技大學 資訊管理 IDSL 2014 Web Services Using Java Tomcat + Axis2 + Eclipse 羅乃維, Alexander Yohan

web services using java

Embed Size (px)

Citation preview

Page 1: web services using java

國立台灣科技大學 – 資訊管理 – IDSL

2014

Web Services Using Java Tomcat + Axis2 + Eclipse 羅乃維, Alexander Yohan

Page 2: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

2

1. Table of Contents

1. TABLE OF CONTENTS ................................................................................ 2

2. DEVELOPMENT TOOLS ................................................................................ 3

2.1 DEVELOPMENT ENVIRONMENT .......................................................................... 3

2.2 TOMCAT INSTALLATION .................................................................................... 3

2.3 CONFIGURING FIREWALL ................................................................................... 9

2.4 AXIS2 INSTALLATION ...................................................................................... 14

2.5 INSTALL THE NECESSARY JAR FILES ............................................................... 19

2.6 SETTING UP ECLIPSE ....................................................................................... 20

3. BUILD A WEB SERVICE PROGRAM ........................................................ 23

3.1 CREATE A NEW PROJECT .................................................................................. 23

3.2 CREATE JAVA CLASS ....................................................................................... 26

3.3 CREATE A SERVER ........................................................................................... 28

3.4 STARTING WEB SERVICES ............................................................................... 31

Page 3: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

3

2. Development Tools

Service:

Tomcat (http://tomcat.apache.org/)

Web service deployment and development engine:

Apache Axis2 (http://axis.apache.org/axis2/java/core/)

Codehaus XFire (http://xfire.codehaus.org/)

Apache CXF (http://cxf.apache.org/)

JWSDP (Java Web Services Developer Pack)

IDE:

Eclipse (http://www.eclipse.org/)

2.1 Development environment

Software version:

Tomcat v7.0.42

Apache Axis2 v1.6.2

Eclipse Java EE IDE for Web Developers

2.2 Tomcat installation

Download stable version of tomcat from http://tomcat.apache.org. In this document

we will use Apache Tomcat v7.0.42 and it can be downloaded from

http://tomcat.apache.org/download-70.cgi, it can be seen on Figure 1. Choose 32-

bit/64-bit Windows Service installer to download apache-tomcat-7.0.42.exe.

Figure 1. Apache Tomcat download page

Page 4: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

4

Follow these steps below to install and configure Apache Tomcat:

1. Run the Apache Tomcat’s installer and accept the installation agreement as seen

in Figure 2 and Figure 3.

Figure 2. Apache Tomcat’s installation setup wizard

Figure 3. Apache Tomcat’s license agreement

Page 5: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

5

2. When asked about installation type, choose Normal. (see Figure 4. for the

detailed information)

Figure 4. Apache Tomcat’s installation type

3. Configure your Apache Tomcat’s installation. Set up your account’s User Name

and Password in the corresponding field. (see Figure 5. for the detailed

information)

Figure 5. Apache Tomcat Setup: Configuration Options

Page 6: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

6

4. Select the path of Java SE or JRE installed on your computer. (see Figure 6. for

the detailed information)

Figure 6. Apache Tomcat Setup: Java Virtual Machine path selection

5. Choose the destination folder to install the Apache Tomcat. (see Figure 7. for the

detailed information)

Figure 7. Apache Tomcat’s destination folder

Page 7: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

7

6. After choosing the Apache Tomcat’s installation folder, wait for the installation

setup to finish its process. After the installation has finished, the setup wizard

will looks like Figure 8.

Figure 8. Finished installation Apache Tomcat

7. To start Apache Tomcat, open up its configuration by: (WinKey)Start > All

Programs > Apache Tomcat 7.0 Tomcat7 > Configure Tomcat ↵. Then, when

it displays window such as in Figure 9, press the Start button.

Figure 9. Apache Tomcat 7.0 configuration window

Page 8: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

8

8. When Apache Tomcat first running on your system, usually it will display a pop-

up confirmation window asking permission for a program (Apache Tomcat) to

communicate through your computer’s firewall. Allow this program to

communicate through your firewall.

9. Open up your browser and go to http://localhost:8080. It will display a webpage

which is located in the install location that have been selected before. In our case,

the directory of the webpage would be in “C:\Program Files\Apache Software

Foundation\Tomcat 7.0\webapps\ROOT” (see Figure 10.)

Figure 10. Default Webpage display of Apache Tomcat

Page 9: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

9

2.3 Configuring firewall

After Apache Tomcat’s installation, make sure that port 8080 (default port used by

Apache Tomcat) has been enabled on your computer.

To activate port 8080 through your computer’s firewall, follow these steps:

1. Open up Windows Control Panel through Start Menu (see Figure 11.).

Figure 11. Open Start menu and select Control Panel

2. On control panel window under the category of System and Security, choose

Windows Firewall (see Figure 12).

Figure 12. Select Windows Firewall on Windows’ System and Security window

Page 10: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

10

3. On windows firewall’s window, choose “Advanced settings” from the sidebar

(see Figure 13).

Figure 13. Choose “Advanced settings” on Windows firewall sidebar

4. On windows firewall advance settings’ window, click on “Inbound Rules” and

then click on “New rule…” to add new rule that will allow Apache Tomcat to

use port 8080 through Windows Firewall (see Figure 14).

Figure 14. Add new inbound rule into windows firewall

5. In the New Inbound Rule Wizard, choose Port as the rule type and then press

button “Next” (see Figure 15).

Page 11: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

11

Figure 15. Select “Port” as the rule type in New Inbound Rule Wizard

6. Select “TCP” as the protocol and input “8080” as the port number (see Figure

16).

Figure 16. Select “TCP” as the protocol type and “8080” as the port number

Page 12: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

12

7. Select “Allow the connection” as the action to be taken (see Figure 17).

Figure 17. Choose “Allow the connection” on the action selection

8. Check on all options in the profile section (see Figure 18).

Figure 18. Check all options in the profile section

Page 13: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

13

9. Give a name on the newly created inbound rule in the corresponding field, in this

example: tomcat (see Figure 19). After giving a name to the new inbound rule,

press button “Finish” to complete the process.

Figure 19. Give a name to the newly created inbound rule

Page 14: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

14

2.4 Axis2 installation

Download stable version of Apache Axis2 from

http://axis.apache.org/axis2/java/core/download.cgi. In this document we will use

Apache Axis2 v1.6.2. Download binary distribution of the Apache Axis2 v1.6.2

(http://apache.cdpa.nsysu.edu.tw//axis/axis2/java/core/1.6.2/axis2-1.6.2-bin.zip).

Follow these steps below to configure Apache Axis2:

10. After downloading axis2-1.6.2-bin.zip, extract the file to C:\axis2-1.6.2.

11. Open up your computer properties and then select “Advanced system settings”

from the sidebar menu (see Figure 20).

Figure 20. Open computer’s properties and select “Advanced system settings”

12. On the advanced system settings window, click on the button “Environment

Variables…” as seen on Figure 21.

Figure 21. Click button “Environment Variables…” on the advanced system

properties window

Page 15: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

15

13. On the environment variable window, add a new system variables by clicking

button “New…” (see Figure 22).

Figure 22. Add new system variables

14. On the new system variable window, set the variable name as “AXIS2_HOME”

and variable value as the directory of the extracted Apache Axis2 file (C:\axis2-

1.6.2). After finish setting up the new system variable, click button “OK” (see

Figure 23).

Figure 23. New Apache Axis2 system variable

Page 16: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

16

15. Download axis2-1.6.2-war.zip from

http://ftp.mirror.tw/pub/apache//axis/axis2/java/core/1.6.2/axis2-1.6.2-war.zip

and extract the downloaded file to any directory.

16. Open your browser and go to http://localhost:8080/ and then click on “Manager

App” button (see Figure 24).

Figure 24. Click on “Manager App” button on the Apache Tomcat page

17. On Tomcat Web Application Manager’s page, upload the extracted file axis2.war

by clicking on button “Choose File” under the “Deploy” group. After selecting

the file to be uploaded, click on the “Deploy” button (see Figure 25).

Figure 25. Deploy the Web application ARchive (WAR) file

Page 17: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

17

18. If the Web application ARchive (WAR) file has been successfully deployed, it

will be displayed under the list of Applications (see Figure 26).

Figure 26. Successfully uploaded and deployed WAR file

19. After axis2 has been deployed, go to http://localhost:8080/axis2/ on the new tab

of your browser, and it will display a page such as in Figure 27.

Figure 27. Successfully deployed axis2 web application

Page 18: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

18

On the axis2 homepage, there are three (3) available links:

Link Function

Services View the list of all the available services deployed in this server.

Validate Check the system to see whether all the required libraries are in

place and view the system information.

Administration Console for administering Axis2 installation.

Axis2 provide one user account by default.

User Name : admin

Password : axis2

20. To change the default User Name and Password, go to Apache Tomcat

installation directory (C:\Program Files\Apache Software Foundation\Tomcat

7.0\webapps\axis2\WEB-INF\conf). Open axis2.xml file and look for the

following line:

<parameter name="userName">admin</parameter>

<parameter name="password">axis2</parameter>

Edit the parameter value and do not forget to save the file after you finished

editing.

Page 19: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

19

2.5 Install the necessary JAR files

Open up your browser and go to http://www.java2s.com/Code/Jar/CatalogJar.htm.

Download activation.jar, mail.jar, tools.jar, xmlsec.jar and put it insides lib folder in

your Apache Tomcat installation folder (see Figure 28). For example: C:\Program

Files\Apache Software Foundation\Tomcat 7.0\lib.

Figure 28. Store activation.jar, mail.jar, tools.jar, and xmlsec.jar inside lib folder

Copy all files inside C:\axis2-1.6.2\lib to C:\Program Files\Apache Software

Foundation\Tomcat 7.0\lib.

Page 20: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

20

2.6 Setting up Eclipse

Follow these steps below to configure eclipse:

1. Open Eclipse.

2. On the menu bar, select Window Preferences.

3. Select Server Runtime Environments Add.

4. On the New Server Runtime Environment, choose Apache Tomcat v7.0 and

then press button “Next” (see Figure 29).

Figure 29. Choose Apache Tomcat v7.0 as the new server runtime environment

5. Assign the Tomcat installation directory and then press button “Finish” (see

Figure 30).

Page 21: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

21

Figure 30. Assign Apache Tomcat installation directory into eclipse

6. Back to eclipse’s preferences window, now select Web Services Axis2

Preferences. Set Axis2 runtime location with the directory of extracted axis2-

1.6.2 in your computer, e.g. C:\axis2-1.6.2 (see Figure 31).

Figure 31. Assign Axis2 runtime location

Page 22: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

22

7. Back on the Web Services, now select Server and Runtime. Change the value of

Web service runtime from Apache Axis to Apache Axis2 (see Figure 32).

Figure 32. Change the value of Web service runtime to Apache Axis2

Page 23: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

23

3. Build a Web Service Program

This part will describe the process to create a web service program.

3.1 Create a new project

Follow these steps below to create a new project for the web service program:

1. Create a new project for the web service program by selecting “File New

Other Web Dynamic Web Project” on the Eclipse menu bar (see Figure

33). After selecting to create a dynamic web project, click on the “Next” button.

Figure 33. Create new dynamic web project in Eclipse

2. Fill in the project name (e.g. idsl-web-service) and set the dynamic web module

version to version 2.5 (see Figure 34).

Page 24: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

24

Figure 34. Configure the new web service project

3. Click on “Modify” button to set project configuration (see Figure 34). On the

project configuration window, check “Axis2 Web Services” option and then

press button “OK” (see Figure 35).

4. After finish adding “Axis2 Web Services” on the project configuration, click

button “Finish” to create the project.

Page 25: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

25

Figure 35. Check “Axis2 Web Services” on the project configuration window

Page 26: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

26

3.2 Create Java class

Follow these steps below to create Java class for your web service project:

1. Right click on the web service project and then on the pop-up menu select “New

Class” (see Figure 36).

Figure 36. Create new Java class inside the web service project

2. Assign a name (e.g. HelloWorld) for the new Java class to be created then click

on button “Finish” to create the class (see Figure 37).

3. Try to put some code into the created class (see Figure 38).

Page 27: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

27

Figure 37. Create HelloWorld class

Figure 38. Try put on some code into HelloWorld class

Page 28: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

28

3.3 Create a server

To create a server, follow these steps below:

1. There are two ways to create a server, via Eclipse’s menu bar or Eclipse’s

Servers page.

The first way to create server is by selecting “File New Other…” on

Eclipse’s menu bar. Then choose “Server” from the list as shown in Figure 39.

Figure 39. Create new server from Eclipse menu bar

Another way to do this is via Eclipse’s Servers window. Right click on Eclipse

Servers’ page and then choose “New Server” from pop-up menu as shown in

Figure 40. In case Eclipse’s Servers page is not displayed, select “Window

Show View Servers” from the menu bar.

Figure 40. Create new server from Eclipse Server window

Page 29: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

29

2. Choose “Tomcat v7.0 Server” from the list of server type and then click button

“Finish” (see Figure 41).

Figure 41. Create new Tomcat v7.0 server

3. In the Servers page, double click on the server that you have just created to open

up the general information about the server. Then change the server locations to

“Use Tomcat Installation (takes control of Tomcat installation)” (see Figure

42).

Page 30: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

30

Figure 42. Change the server location to “use tomcat installation”

Page 31: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

31

3.4 Starting Web Services

To start the web service, expand the Eclipse project explorer and look for class java

file that has been created (e.g. HelloWorld.java). Right click on that file and from the

pop-up menu choose “Web Services Create Web Service” as shown in Figure

43.

Figure 43. Create web service for HelloWorld.java

Check the created web service’s configuration and make sure that the “Web service

runtime” is set to “Apache Axis2” as shown in Figure 44, then click on button

“Next”. If the “Web service runtime” is not set to “Apache Axis2”, then change its

value by clicking on the web service runtime’s link and change its value to Apache

Axis2 as shown in Figure 45.

Page 32: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

32

Figure 44. Make sure that web service runtime is set to Apache Axis2

Figure 45. Choose Apache Axis2 as the web service runtime

Page 33: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

33

After clicking button “Next”, do not forget to choose “Generate a default

services.xml file” as shown in Figure 46.

Figure 46. Generate a default services.xml file

After clicking on “Finish” button, do not forget to start the Tomcat server by

clicking “Start server” button (see Figure 47).

Figure 47. Click on button “Start server” to start the Tomcat server

Page 34: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

34

After starting the Tomcat server, open up your browser and go to

http://localhost:8080/<your-web-service-project-name>. In this tutorial the

address would be http://localhost:8080/idsl-web-service/ (see Figure 48). Click on

the “Services” link to see all available services run on the server. In this tutorial you

will see the HelloWorld Web service as shown in Figure 49.

Figure 48. The display of idsl-web-service

Figure 49. The display of all available services on idsl-web-service

Page 35: web services using java

Web Services Using Java

Tomcat + Axis2 + Eclipse

羅乃維, Alexander Yohan

國立台灣科技大學 – 資訊管理 – IDSL | 台北市大安區基隆路 4 段 43 號

35

Try clicking on the HelloWorld web service to make it shows the WSDL as shown in

Figure 50.

Figure 50. HelloWorld web service’s WSDL

Use RESTful to test the return value from the code in file HelloWorld.java. In your

browser go to http://localhost:8080/<your-web-service-project-

name>/services/<service-name>/<function-name>?<parameter-

name>=<parameter-value>. In this tutorial, the address would be:

http://localhost:8080/idsl-web-

service/services/HelloWorld/printName?name=Alexander. (see Figure 51)

Figure 51. Use RESTful to test the return value