˘ j2ee 1/32krunapon/courses/178375/exercises/j2ee/j2eews.pdf · j2ee ˛˚ ˛˚ ˜ ! ˚ ˚" #...
TRANSCRIPT
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
1/32
�������������� ������������������ �� J2EE
$&�'�� ��������,-�������� � ���������� deploy ���������� � ������� ������������ ����"&4���������������� J2EE ������5�&���, 6���7-%�8���"&4������ JAX-RPC % ������ �?8�
JAX-RPC ��6�� Java API for XML-based RPC
���������������� Web Services ���� J2EE
#&���(����,6-I��������� Calculator Services ���� J2EE ���6���O�� #(��P �&���,
• Add
• Subtract
• Multiply
• Divide
)�*+��,*�-�+ ��
• Sun j2sdk 1.5.0_06
• Eclipse version 3.0.1 for Win32
• Sun Java System Application Server 8.2 (SJSAS 8.2) #��#&,�%�8 Port : 8080
• Microsoft Windows XP Professional
��+�-�+����,��������.�/��
• #&��-� JAVA_HOME ��,7-%�8 #d��$�(�%�8#��#&,� Sun j2sdk 1.5.0_06( c:\j2sdk1.5.0_06)
• #&��-� J2EE_HOME ��,7-%�8#d��$�(�%�8#��#&,� SJSAS 8.2 (c:\Sun\AppServer)
• �#&��-� PATH $�"�8� ;%J2EE_HOME%\bin
8�9���� ����.�/��
'&,�#�� ����"&4���������������� J2EE ������5�&���, 6��(�-I� 7 '&,�#�������&� 1. ����� Java Project ���d�$�� Environment #(��P $�$����(���"&4�� 2. ����� Remote Interface 3. ����� Calculator Implementation 4. �����7i�� Configuration "k8� �� ���� mapping �� generate wsdl 5. Generate mapping and wsdl file using wscompile 6. ����� Web Component ���� deploytool 7. Deploy Web Service 7-%�8 Sun Java System Application Server
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
2/32
'&,�#��%�8 1 : ����� Java Project ���d�$�� Environment #(��P $�$����(���"&4��
����� Java Project ���7-%�8 File > New > Project �&��p-%�8 1
�p-%�8 1 �k�� Project
6���&,�6��$���#(�� New Project '?,��� $�%d�����k�� Java Project ���� ���%�8 Next �&��p-%�8 2
�p-%�8 2 �k�� Java Project
�$���#(�� Create a Java Project $� �( (�#(��P #��#&���(�� ��p-%�8 3
• �(�� Project name $� �( jws
• �(�� Location $��k��%�8 Create project in workspace
• �(�� Project layout $��k��%�8 Create separate source and output folders 6���&,��� ���%�8 Next
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
3/32
�p-%�8 3 ����� Java Project
6��$���#(�� Java Settings '?,����&��p- $� ���%�8�%�� Libraries
�p-%�8 4 �k��%�8 Libraries
�$���#(�� Libraries $�%d���� ���%�8 Add External JARsI 6���&,� $��k��7i�� j2ee.jar %�8��p( � C:\Sun\AppServer\lib 6���&,� $��k��%�8 Open �&��p-%�8 5
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
4/32
�p-%�8 5 �d�$�� Library j2ee.jar "�8�#��
6���&,���6$���(� �� library "�8�'?,������$�?8�#&� �&��p-%�8 6 6���&,� $� ���%�8 Finish
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
5/32
�p-%�8 6
�$���#(�� Package Explorer 6$���(�6�� Project $�(%�8�k8� jws '?,��� �&��p-%�8 7
�p-%�8 7
'&,�#��%�8 2 ����� Remote Interface %d��������� interface '?,��� $�( ��� ���'��%�8 jws �����k��%�8 New > Interface �&��p-%�8 8
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
6/32
�p-%�8 8
�$���#(�� Java Interface $� �( (�#(��P �&��p-%�8 9
• �(�� Package �( ws
• �(�� Name �( Calculator 6���&,� $� ���%�8-�s� Add
6��$���#(�� Extended Interfaces Selection '?,��� $�%d���� �( (�#(��P �&��p-%�8 10
• ��(�� Choose interfaces : $� �( (� Remote 6���&,�6�� list '�� Class #(��P ��(�� Matching types %�8'?,�#������ Remote '?,��� $��k��
• ��(�� Matching types $��k��%�8 Remote
• ��(�� Qualifier : 6��%�8��p('�� Class %�8���k���(� ��p( � package �7� �(�#��#&���(�� -I� java.rmi
�p-%�8 9
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
7/32
�p-%�8 10 �k�� interface %�86%d���� extends
6���&,� $� ���%�8 Add ������ OK ��6��&���$���#(�� Java Interface �#(6����8�%�8"�8�'?,��� ��(�� Extened Interface �?8�-I� java.rmi.Remote �&��p-%�8 11 $�%d���� ���%�8 Finish
�p-%�8 11
�k8����p%�8$���#(�� Package Explorer ��6$�� 7i�� Calculator.java �&��p-%�8 12
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
8/32
�p-%�8 12
6���&,� $���%d����"�8�� �� �7i�� Calculator.java $�-I��&��p-%�8 13
�p-%�8 13 #&���(��7i�� Calculator.java
'&,�#��%�8 3 ����� Calculator Implementation %d��������� class '?,��� $�( ��� ���'��%�8 Project jws ����%d�����k�� New > Class �&��p-%�8 14
�p-%�8 14
6���&,�6��$���#(�� Java Class '?,��� $��� �( (�#(��P �&��p-%�8 15
package ws; import java.rmi.Remote; import java.rmi.RemoteException; public interface Calculator extends Remote { public double add(double op1, double op2) throws RemoteException; public double subtract(double op1, double op2) throws RemoteException;
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
9/32
• ��(�� Package �( (� ws
• ��(�� Name �( (� CalculatorImpl %d����#��6����p�(� � d�z��'����(����� Which method stubs would you like to create? ��(��
Constructors from superclass �� Inherited abstract methods �&,� zp��k��%&,�����(�� $�&�6���&,��� ���%�8-�s� Add..
�p-%�8 15
6��$���#(�� Implemented Interfaces Selection '?,��� $�%d���� �( (�#(��P �&���,
• ��(�� Choose interfaces : �( (� Calculator
• ��(�� Matching types $��k�� Calculator
• ��(�� Qualifier $��k�� ws "� �( �������� ���-�s� Add ������ ���%�8-�s� OK
�p-%�8 16
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
10/32
6���&,��-�������6������&���%�8$��� Java Class $�k����� �#(6����8�%�8"�8�'?,��� k� ��(�� Interface 6�� ws.Calculator 6���&,� ����%d���� ���%�8-�s� Finish
�p-%�8 17
6���&,�%d�������7'� �� CalculatorImpl.java $����p-����&��p-%�8 18
�p-%�8 18 #&���(��7i�� CalculatorImpl.java
package ws; import java.rmi.RemoteException; public class CalculatorImpl implements Calculator { public CalculatorImpl() { super(); } public double add(double op1, double op2) throws RemoteException { return op1+op2; } public double subtract(double op1, double op2) throws RemoteException { return op1-op2; } public double multiply(double op1, double op2) throws RemoteException { return op1*op2; } public double divide(double op1, double op2) throws RemoteException { return op1/op2; } }
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
11/32
'&,�#��%�8 4 �����7i�� Configuration "k8� �� ���� mapping �� generate wsdl %d���������7i�� configuration %�8���k8��(� config.xml �����8�6�� ���'��%�8 Project jws ����7-%�8 New > File �&��p-%�8 19
�p-%�8 19
6��$���#(��7i�� '?,��� $� �( (�#(��P �&��p-%�8 20
• ��(�� Enter or select the parent folder $� �( (� jws �?8�-�#� ���6%d� $���p(����
• ��(�� File name : $� �( (� config.xml �k8� �('���p� ��$������ $� ���%�8-�s� Finish
�p-%�8 20 �����7i�� config.xml
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
12/32
"�8�'���p��� �7i�� config.xml $����p-����&��p-%�8 21
�p-%�8 21 #&���(��7i�� config.xml 7i��%�8�������'?,��� 6��%&,�$�� 3 7i�� �&���, Calculator.java , CalculatorImpl.java �� config.xml #(�7-6-I�'&,�#�� '����� generate mapping 7i�� �� wsdl 7i�� '&,�#��%�8 5 Generate mapping and wsdl file using wscompile %d����-�� command prompt '?,��� ����"��"� ����%d����#��6����p�(� 7��%d���� set PATH $���,7-%�8 C:\Sun\AppServer\bin ���� �&��p-%�8 22
�p-%�8 22 #��6��� PATH
6���&,� $�'��7-%�8 #d��$�(�%�8������� �� �(�#��#&���(�� 6���7��%�8 c:\eclipse\workspace\jws
6��7i��#(��P -���� �&��p-%�8 23
<?xml version="1.0" encoding="UTF-8"?>
<configuration
xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
<service
name="CalculatorWS"
targetNamespace="http://campus.en.kku.ac.th"
typeNamespace="http://campus.en.kku.ac.th"
packageName="ws">
<interface name="ws.Calculator"/>
</service> </configuration>
echo %PATH%
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
13/32
�p-%�8 23 ����� �������'���-�6 %�8�����'?,���6�� eclipse #(���%d���� generate mapping �� wsdl 7i�� 6�� d��&8� wscompile �&��p-%�8 24
�p-%�8 24 ��� generate mapping �� wsdl
�O������� �� d��&8� wscompile d��&8� wscompile �&,� 6�� input -I� configuration 7i�� �?8�6�� options #(��P ����� ��������z�p help '�� d��&8� wscompile 7��6����� �� d��&8� wscompile �help �(��%�� command prompt �?8�6'��O����5"� d��&8�%�87�� ����� ����%������,%(��&,� #(��� '��7-�p7i�� � c:\eclipse\workspace\jws\bin 6$���(���7i��"�8�'?,��� 2 7i�������&� k� mapping.xml �&� CalculatorService.wsdl �&��p-%�8 25
�p-%�8 25 7i��%�8zp� generate '?,���
wscompile �define �d bin �nd bin �classpath bin �mapping bin/mapping.xml config.xml
-define �� ���������7i�� wsdl $��&�������� -d <directory> �� �����d�$��#d��$�(� output 7i�� -classpath <path> �� �������#d��$�(�'�� ���%�86 ��7"�� -mapping <file> �� ���� generate mapping file -nd <directory> �� �������#d��$�(� output 7i�� %�8-I� non-class -f:<features> �� �����d�$�� feature �(�6 $� generate 7i��-I��p-���7$�
�(� documentliteral, rpcliteral
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
14/32
'&,�#��%�8 6 ����� Web Component ���� deploytool deploytool -I� �k8���k�%�8 �� ����%d� packaging �������z deploy �-�����'��7-%�8 Sun Java System Application Server %�8��p(��� � �k8�� $�k� #(�� �k8��7�� ��� deploytool 6��p(%�8 C:\Sun\AppServer\bin �������z���� �� ���"��"� deploytool �(��%�� command prompt 7��%&�%� �k8��6����%d���� set path ������������ $���#�'���-����� 6-I��&��p-%�8 26
�p-%�8 26 �-����� deploytool
��8��������� Web Component ��� ���%�8��p File > New > Web Component �&��p-%�8 27
�p-%�8 27
6��$���#(�� Introduction '?,���#����&� �&��p-%�8 28 6���&,���%d���� ���%�8 Next
�p-%�8 28 $���#(�� Introduction
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
15/32
6��$���#(��'�� New Web Application Wizard '?,��� �&��p- ��(��'�� WAR Location $��k��-I� Create New Stand-Alone WAR Module 6���&,� ��(��'�� WAR Naming $� ���%�8 Browse
�p-%�8 29 Create War file
6��$���#(�� $��k��%�8��p('��7i�� WAR �?8����k��-I� c:\eclipse\workspace\jws �� ��(�� File Name $� �(-I� jws 6���&,� $� ���%�8 Create Module File
�p-%�8 30
�k8�%d��������� Module File ���6���� ���6��&���$��� New Web Application Wizard �#(6���(��%�8"�8�'?,��� k��(��'�� WAR Naming %�8������d�$�� (�%&,�$������
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
16/32
�p-%�8 31
6���&,� $��k�� Edit Contents �?8�6-����$���#(�� $��k��7i��%�86%d� packaging
�p-%�8 32
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
17/32
$�%d�����k��7i�� Calculator.class , CalculatorImpl.class, mapping.xml �� CalculatorWS.wsdl �&��p- ����-�s� Add
�p-%�8 33
6$���(������6&�����%�8��p('��7i�� $�( "k8� $��(��#(�����p���&��� 6���&,� ���%�8-�s� OK
�p-%�8 34
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
18/32
#(��� ��(��'�� Context Root $�"�8� ws '��7- ������ ���-�s� Next
�p-%�8 35
��(��'�� Choose component type $��k��-I� Web Services Endpoint ������ ��� Next
�p-%�8 36
z&��� ��(��'�� Choose Service $�%d����"�8�'���p��&���,
• Service Definition -I� WEB-INF/wsdl/CalculatorWS.wsdl
• Mapping File -I� mapping.xml
• Service Name -I� CalculatorWS
• Service Display Name -I� CalculatorWS
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
19/32
6���&,��� ��� Next
�p-%�8 37
��(�� Component General Properties $�"�8�'���p��&���,
• Service Endpoint Implementation -I� ws.CalculatorImpl
• Web Component Name -I� CalculatorImpl
• Web Component Display Name -I� CalculatorImpl
• Startup load sequence position -I� Load at any time
�p-%�8 38
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
20/32
��(��'�� Web Service Endpoint $�"�8�'���p��&���,
• Service Endpoint Interface -I� ws.Calculator
• Namespace -I� http://campus.en.kku.ac.th
• Local Part -I� CalculatorPort
• Port Component Name -I� Calculator
• Port Component Display Name -I� Calculator
• Endpoint Address -I� CalculatorWS �k8����6���� $� ��� Next
�p-%�8 39
6���&,�6-I�$���#(������(� ����� Web Component ���6������������ �k8���'����%�8$���#(�� deploytool 6$���(� �� Web Application %�8������� "�8�'?,��� �&��p-
�p-%�8 40
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
21/32
���%�8 CalculatorImpl ����7-%�8�%� Aliases 6���&,��� ���%�8 Add
�p-%�8 41
��(�� Aliases $�"�8�'���p�'��7--I� CalculatorWS
�p-%�8 42
6���&,� $���%d�����&�%?� ���7-%�8��p File > Save
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
22/32
'&,�#��%�8 7 Deploy Web Service 7-%�8 Sun Java System Application Server '&,�#�����%���'�������������������� �� k���� deploy �����6 ���-����� deploytool $�k����� ���7-%�8��p Tool > deploy
�p-%�8 43
6���&,�6��$���#(���d�$�&����������'��7-%�8 Server 6���&,����6%d���� deploy jws.war %�87�������'?,��� �?8������%d�����&��p-
�p-%�8 44
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
23/32
6���&,� $����%d��������7-%�8 http://localhost:8080/ws/CalculatorWS?WSDL 6-���� wsdl �&��p-%�8 45
�p-%�8 45
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
24/32
������� ������������
�������� ���������������� JAX-RPC �����&� 1.2 6��%&,�$�� 4 ��O� 1. Static Stub Client 2. Dynamic Proxy Client 3. Dynamic Invocation Interface Client (DII) 4. Application Client ����%������,��6�k�� ����� Dynamic Invocation Interface Client �?8�6��'���� �����%�8 Service %�8
���� �������-��8���-���(�� �����7�(6d�-I�#���7- Generate Stub $�(��� �&,� �#(������� �� ��� Dynamic Invocation Interface Client 6��'�����#��%�8 DII 6%d���������(���� Static Stub Client �� Dynamic Proxy Client '&,�#�� ����"&4��
1. ����� Java Project '?,��� ����d�$�� $����k8��(� jwsclient ���� ���%�8 Next
�p-%�8 46
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
25/32
2. ��z� Libraries $� ���%�8 Add External JARS ���k��%�8 j2ee.jar �&� jaxrpc-impl.jar �k8����6���� ���%�8 Open
�p-%�8 47
���6����,�-������&�#��� �( jsr173_api.jar "k8�%�86 $��������z?� ��� javax.xml.stream/XMLStreamWriter 7�� �?8�7i�� jsp173_api.jar �&,���p(%�8 <Netbeans-5.0_INSTALL_DIR>\ide6\modules\ext\jaxrpc16
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
26/32
3. 67��$���#(���&��p-%�8 48 �� ���%�8 Finish
�p-%�8 48
4. ����� java class '?,��� ��� ���'��%�8 jwsclient > New > Class �� �d�$�� (�#(��P �&���,
• ��(�� package -I� ws
• ��(�� Name -I� CalculatorClient
• ��(�� d�z�� Which method stubs would you like to create? $� ����k��%&,�$�� 6���&,��� ���%�8 Finish
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
27/32
�p-%�8 49
�p-���'�� WSDL %�86���� �� �?8���p(%�8 http://localhost:8080/ws/CalculatorWS?WSDL
<?xml version="1.0" encoding="UTF-8"?><definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://campus.en.kku.ac.th" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" name="CalculatorWS" targetNamespace="http://campus.en.kku.ac.th"> <types/> <message name="Calculator_add"> <part name="double_1" type="xsd:double"/> <part name="double_2" type="xsd:double"/></message> <message name="Calculator_addResponse"> <part name="result" type="xsd:double"/></message> <message name="Calculator_divide"> <part name="double_1" type="xsd:double"/>
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
28/32
<part name="double_2" type="xsd:double"/></message> <message name="Calculator_divideResponse"> <part name="result" type="xsd:double"/></message> <message name="Calculator_multiply"> <part name="double_1" type="xsd:double"/> <part name="double_2" type="xsd:double"/></message> <message name="Calculator_multiplyResponse"> <part name="result" type="xsd:double"/></message> <message name="Calculator_subtract"> <part name="double_1" type="xsd:double"/> <part name="double_2" type="xsd:double"/></message> <message name="Calculator_subtractResponse"> <part name="result" type="xsd:double"/></message> <portType name="Calculator"> <operation name="add" parameterOrder="double_1 double_2"> <input message="tns:Calculator_add"/> <output message="tns:Calculator_addResponse"/></operation> <operation name="divide" parameterOrder="double_1 double_2"> <input message="tns:Calculator_divide"/> <output message="tns:Calculator_divideResponse"/></operation> <operation name="multiply" parameterOrder="double_1 double_2"> <input message="tns:Calculator_multiply"/> <output message="tns:Calculator_multiplyResponse"/></operation> <operation name="subtract" parameterOrder="double_1 double_2"> <input message="tns:Calculator_subtract"/> <output message="tns:Calculator_subtractResponse"/></operation></portType> <binding name="CalculatorBinding" type="tns:Calculator"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/> <operation name="add"> <soap:operation soapAction=""/> <input> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="http://campus.en.kku.ac.th"/></input> <output> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="http://campus.en.kku.ac.th"/></output></operation> <operation name="divide">
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
29/32
<soap:operation soapAction=""/> <input> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="http://campus.en.kku.ac.th"/></input> <output> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="http://campus.en.kku.ac.th"/></output></operation> <operation name="multiply"> <soap:operation soapAction=""/> <input> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="http://campus.en.kku.ac.th"/></input> <output> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="http://campus.en.kku.ac.th"/></output></operation> <operation name="subtract"> <soap:operation soapAction=""/> <input> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="http://campus.en.kku.ac.th"/></input> <output> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="http://campus.en.kku.ac.th"/></output></operation></binding> <service name="CalculatorWS"> <port name="CalculatorPort" binding="tns:CalculatorBinding"> <soap:address location="http://172.16.21.174:8080/ws/CalculatorWS" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/></port></service></definitions>
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
30/32
5. �����7i�� CalculatorClient.java �?8����p-����&��p-%�8 50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
package ws; import javax.xml.rpc.Call; import javax.xml.rpc.Service; import javax.xml.rpc.JAXRPCException; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceFactory; import javax.xml.rpc.ParameterMode; public class CalculatorClient { private static String qnameService = "CalculatorWS"; private static String qnamePort = "Calculator"; private static String BODY_NAMESPACE_VALUE = "http://campus.en.kku.ac.th"; private static String ENCODING_STYLE_PROPERTY = "javax.xml.rpc.encodingstyle.namespace.uri"; private static String NS_XSD = "http://www.w3.org/2001/XMLSchema"; private static String URI_ENCODING = "http://schemas.xmlsoap.org/soap/encoding/"; public static void main(String[] args) { try { ServiceFactory factory = ServiceFactory.newInstance(); Service service = factory.createService(new QName(qnameService)); QName port = new QName(qnamePort); Call call = service.createCall(port); call.setTargetEndpointAddress("http://localhost:8080/ws/CalculatorWS?WSDL"); call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean(true)); call.setProperty(Call.SOAPACTION_URI_PROPERTY, ""); call.setProperty(ENCODING_STYLE_PROPERTY, URI_ENCODING); QName QNAME_TYPE_DOUBLE = new QName(NS_XSD, "double"); call.setReturnType(QNAME_TYPE_DOUBLE); call.setOperationName(new QName(BODY_NAMESPACE_VALUE, args[0])); call.addParameter("double_1", QNAME_TYPE_DOUBLE, ParameterMode.IN); call.addParameter("double_2", QNAME_TYPE_DOUBLE, ParameterMode.IN);
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
31/32
30 31 32 33 34 35 36 37 28
Double d1 = new Double(args[1]); Double d2 = new Double(args[2]); Double result = (Double)call.invoke(new Object[]{d1,d2}); System.out.println(result); } catch (Exception ex) { ex.printStackTrace(); } } }
�p-%�8 50 #&���(��7i��%�8 �� �������� CalculatorWS �O���������� �� CalculatorClient.java ���%&�%�8 9 private static String qnameService = "CalculatorWS"; ��� CalculatorWS 7����6�� �%�� <definitions> #�� attribute name=�CalculatorWS�
�p-%�8 51
���%&�%�8 10 private static String qnamePort = "Calculator"; ��� Calculator 7����6���%�� <portType> #�� attribute name=�Calculator�
�p-%�8 52
���%&�%�8 21 call.setTargetEndpointAddress("http://localhost:8080/ws/CalculatorWS?WSDL"); ���#d��$�(�'�� WSDL %�867-���� ����� ���%&�%�8 25 QName QNAME_TYPE_DOUBLE = new QName(NS_XSD, "double"); #�� �double� 6-I�����'��'���p�%�8 �(���&���6���������%�8������ �� ���%&�%�8 28 �� 29
call.addParameter("double_1", QNAME_TYPE_DOUBLE, ParameterMode.IN);
call.addParameter("double_2", QNAME_TYPE_DOUBLE, ParameterMode.IN);
#�� double_1 �� double_2 6-I�����'���p�'�� Parameter %�86�(�7- $��������%�8������ ��
�p-%�8 53
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
32/32
6. -�������-������(��%�� Eclipse IDE ���7-%�8��p Run > Run�
�p-%�8 54
7. 6-����$���#(�� Run '?,��� �&��p-%�8 55
�p-%�8 55
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
33/32
8. ��(�� Configuration $�%d�����&���� ���%�8 Java Application "k8������ New Run Application �?8� Eclipse IDE 6����� CalculatorClient '?,���
�p-%�8 56
9. ���%�8�z� Argument �� �( (� Program arguments �(� 6���&,��� ���%�8 Run
�p-%�8 57 �d�$�� Program arguments
add 10 20
�������������� ������������������ �� J2EE
���������������� �� ��!����� ��"��#��� �$���%���&�'����(�
34/32
10. ��%�87��6����� Run 6-I� 30.0 �p-%�8 58
�p-%�8 58 ��%�87��6������&��-�����
������������ The Java™ Web Services Tutorial http://java.sun.com/webservices/docs/1.5/tutorial/doc/index.html
J2EE 1.4 Documentation http://java.sun.com/j2ee/1.4/docs