- 浏览: 5595 次
- 性别:
- 来自: 杭州
文章分类
最新评论
利用java编写简单的wenservice实例
使用Axis编写WebService比较简单,就我的理解,WebService的实现代码和编写Java代码其实没有什么区别,主要是将哪些Java类发布为WebService。下面是一个从编写测试例子到发布WebService,以及编写测试代码的过程介绍。
本例子的WebService提供了两个方法,分别是sayHello和sayHelloToPerson,第一个只是返回一个"Hello"字符串,没有参数,第二个函数接受一个字符串作为参数,返回"Hello 参数值",该例子比较简单,但是清楚的说明了从编写代码到发布为WebService以及测试编写好的WebService全过程。
编写服务代码
服务代码提供了两个函数,分别为sayHello和sayHelloToPerson,源代码如下:
Java代码
1./*
2. * File name: HelloService.java
3. *
4. * Version: v1.0
5. *
6. * Created on Aug 2, 2008 9:40:20 AM
7. *
8. * Designed by Stephen
9. *
10. * (c)Copyright 2008
11. */
12.package com.sinosoft.webservice;
13.
14./** *//**
15. * @author Stephen
16. *
17. * Test web service
18. */
19.public class HelloService {
20. /** *//**
21. * 不带参数的函数
22. *
23. * @return 返回Hello字符串
24. */
25. public String sayHello() {
26. return "Hello";
27. }
28.
29. /** *//**
30. * 带参数的函数
31. *
32. * @param name
33. * 名称
34. * @return 返回加上名称的欢迎词
35. */
36. public String sayHelloToPerson(String name) {
37. if (name == null || name.equals("")) {
38. name = "nobody";
39. }
40. return "Hello " + name;
41. }
42.}
/*
* File name: HelloService.java
*
* Version: v1.0
*
* Created on Aug 2, 2008 9:40:20 AM
*
* Designed by Stephen
*
* (c)Copyright 2008
*/
package com.sinosoft.webservice;
/** *//**
* @author Stephen
*
* Test web service
*/
public class HelloService {
/** *//**
* 不带参数的函数
*
* @return 返回Hello字符串
*/
public String sayHello() {
return "Hello";
}
/** *//**
* 带参数的函数
*
* @param name
* 名称
* @return 返回加上名称的欢迎词
*/
public String sayHelloToPerson(String name) {
if (name == null || name.equals("")) {
name = "nobody";
}
return "Hello " + name;
}
}
发布WebService
要将上边写的HelloService类发布为WebService,需要先搭建Web应用。下面是在Tomcat下使用Axis(http://ws.apache.org/axis/)创建WebService服务的例子。
在Tomcat下创建Web应用
在该例子中,在Tomcat下创建了一个context path为ws的WEB应用。
1. 在myeclipse中创建WebServiceTest的webproject
2. 在WEB-INF文件夹下web.xml文件,该文件的内容如下:
Xml代码
1.<?xml version="1.0" encoding="ISO-8859-1"?>
2.
3.<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
4.Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
5.
6.<web-app>
7. <display-name>Apache-Axis</display-name>
8.
9. <listener>
10. <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
11. </listener>
12.
13. <servlet>
14. <servlet-name>AxisServlet</servlet-name>
15. <display-name>Apache-Axis Servlet</display-name>
16. <servlet-class>
17. org.apache.axis.transport.http.AxisServlet
18. </servlet-class>
19. </servlet>
20.
21. <servlet>
22. <servlet-name>AdminServlet</servlet-name>
23. <display-name>Axis Admin Servlet</display-name>
24. <servlet-class>
25. org.apache.axis.transport.http.AdminServlet
26. </servlet-class>
27. <load-on-startup>100</load-on-startup>
28. </servlet>
29.
30. <servlet>
31. <servlet-name>SOAPMonitorService</servlet-name>
32. <display-name>SOAPMonitorService</display-name>
33. <servlet-class>
34. org.apache.axis.monitor.SOAPMonitorService
35. </servlet-class>
36. <init-param>
37. <param-name>SOAPMonitorPort</param-name>
38. <param-value>5001</param-value>
39. </init-param>
40. <load-on-startup>100</load-on-startup>
41. </servlet>
42.
43. <servlet-mapping>
44. <servlet-name>AxisServlet</servlet-name>
45. <url-pattern>/servlet/AxisServlet</url-pattern>
46. </servlet-mapping>
47.
48. <servlet-mapping>
49. <servlet-name>AxisServlet</servlet-name>
50. <url-pattern>*.jws</url-pattern>
51. </servlet-mapping>
52.
53. <servlet-mapping>
54. <servlet-name>AxisServlet</servlet-name>
55. <url-pattern>/services/*</url-pattern>
56. </servlet-mapping>
57.
58. <servlet-mapping>
59. <servlet-name>SOAPMonitorService</servlet-name>
60. <url-pattern>/SOAPMonitor</url-pattern>
61. </servlet-mapping>
62.
63. <!-- uncomment this if you want the admin servlet -->
64. <!--
65. <servlet-mapping>
66. <servlet-name>AdminServlet</servlet-name>
67. <url-pattern>/servlet/AdminServlet</url-pattern>
68. </servlet-mapping>
69. -->
70.
71. <session-config>
72. <!-- Default to 5 minute session timeouts -->
73. <session-timeout>5</session-timeout>
74. </session-config>
75.
76. <!-- currently the W3C havent settled on a media type for WSDL;
77. http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft
78. for now we go with the basic 'it's XML' response -->
79. <mime-mapping>
80. <extension>wsdl</extension>
81. <mime-type>text/xml</mime-type>
82. </mime-mapping>
83.
84.
85. <mime-mapping>
86. <extension>xsd</extension>
87. <mime-type>text/xml</mime-type>
88. </mime-mapping>
89.
90. <welcome-file-list id="WelcomeFileList">
91. <welcome-file>index.jsp</welcome-file>
92. <welcome-file>index.html</welcome-file>
93. <welcome-file>index.jws</welcome-file>
94. </welcome-file-list>
95.
96.</web-app>
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Apache-Axis</display-name>
<listener>
<listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
</listener>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis Servlet</display-name>
<servlet-class>
org.apache.axis.transport.http.AxisServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>AdminServlet</servlet-name>
<display-name>Axis Admin Servlet</display-name>
<servlet-class>
org.apache.axis.transport.http.AdminServlet
</servlet-class>
<load-on-startup>100</load-on-startup>
</servlet>
<servlet>
<servlet-name>SOAPMonitorService</servlet-name>
<display-name>SOAPMonitorService</display-name>
<servlet-class>
org.apache.axis.monitor.SOAPMonitorService
</servlet-class>
<init-param>
<param-name>SOAPMonitorPort</param-name>
<param-value>5001</param-value>
</init-param>
<load-on-startup>100</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SOAPMonitorService</servlet-name>
<url-pattern>/SOAPMonitor</url-pattern>
</servlet-mapping>
<!-- uncomment this if you want the admin servlet -->
<!--
<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/servlet/AdminServlet</url-pattern>
</servlet-mapping>
-->
<session-config>
<!-- Default to 5 minute session timeouts -->
<session-timeout>5</session-timeout>
</session-config>
<!-- currently the W3C havent settled on a media type for WSDL;
http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft
for now we go with the basic 'it's XML' response -->
<mime-mapping>
<extension>wsdl</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xsd</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<welcome-file-list id="WelcomeFileList">
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jws</welcome-file>
</welcome-file-list>
</web-app>
在上面的web.xml中主要是配置了axis的相关配置。
axis的相关配置
在上述的web.xml文件中已经对axis进行了配置,但是还需要进行额外的配置。
复制axis相关的jar文件
将axis的相关jar文件复制到WEB-INF\lib文件夹下。这些文件包括:
activation.jar
axis.jar
axis-ant.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
log4j-1.2.8.jar
saaj.jar
wsdl4j-1.5.1.jar
测试发布的Web应用
启动Tomcat服务,打开IE浏览器,访问地址http://127.0.0.1:8080/WebServiceTest/services,如果看到如下界面就说明AXIS部署成功了。
发布WebService
发布WebService需要使用现有的AdminService来实现,这里我写了一个批处理文件来发布WebService,以后如果需要发布其他文件,只需要修改相应的参数就可以了。
创建deploy.wsdd文件
文件deploy.wsdd内容如下所示:
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
3. <service name="HelloServices" provider="java:RPC">
4. <parameter name="className" value="com.sinosoft.webservice.HelloService"/>
5. <parameter name="allowedMethods" value="*"/>
6. </service>
7.</deployment>
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="HelloServices" provider="java:RPC">
<parameter name="className" value="com.sinosoft.webservice.HelloService"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>
创建发布WebService服务的批处理文件
批处理文件deploywebservice.bat内容如下:
Xml代码
1.java -cp axis-ant.jar;axis-schema.jar;axis.jar;commons-discovery-0.2.jar;commons-logging-1.0.4.jar;jaxrpc.jar;log4j-1.2.8.jar;saaj.jar;wsdl4j-1.5.1.jar;xmlsec-1.3.0.jar org.apache.axis.client.AdminClient -lhttp://localhost:8080/WebServiceTest/services/AdminService deploy.wsdd
2.
3.pause
java -cp axis-ant.jar;axis-schema.jar;axis.jar;commons-discovery-0.2.jar;commons-logging-1.0.4.jar;jaxrpc.jar;log4j-1.2.8.jar;saaj.jar;wsdl4j-1.5.1.jar;xmlsec-1.3.0.jar org.apache.axis.client.AdminClient -lhttp://localhost:8080/WebServiceTest/services/AdminService deploy.wsdd
pause
其中上面的jar包我都拷到和bat文件在同一个目录,现在将所有的jar文件都加入到classpath中进行执行。
-l后的参数是本地要发布WebService的AdminService对应的访问地址。
最后deploy.wsdd是对应的配置文件名称。
发布WebService服务
将deploy.wsdd文件和deploywebservice.bat文件复制到同一个文件夹下,执行deploywebservice.bat批处理文件,就可以将deploy.wsdd中描述的Java类发布为WebService。发布完成之后在访问http://host:port/ws/services如下图所示:
从上图可以看出,发布成功后,多了一个HelloServices的服务。这样就说明HelloService发布成功了。
查看HelloServices的wsdl
访问http://127.0.0.1:8080/WebServiceTest/services/HelloServices?wsdl可以看到如下wsdl的内容:
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<wsdl:definitions targetNamespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:intf="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3.<!--WSDL created by Apache Axis version: 1.4
4.Built on Apr 22, 2006 (06:55:48 PDT)-->
5.
6. <wsdl:message name="sayHelloToPersonRequest">
7.
8. <wsdl:part name="name" type="soapenc:string"/>
9.
10. </wsdl:message>
11.
12. <wsdl:message name="sayHelloRequest">
13.
14. </wsdl:message>
15.
16. <wsdl:message name="sayHelloToPersonResponse">
17.
18. <wsdl:part name="sayHelloToPersonReturn" type="soapenc:string"/>
19.
20. </wsdl:message>
21.
22. <wsdl:message name="sayHelloResponse">
23.
24. <wsdl:part name="sayHelloReturn" type="soapenc:string"/>
25.
26. </wsdl:message>
27.
28. <wsdl:portType name="HelloService">
29.
30. <wsdl:operation name="sayHello">
31.
32. <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>
33.
34. <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>
35.
36. </wsdl:operation>
37.
38. <wsdl:operation name="sayHelloToPerson" parameterOrder="name">
39.
40. <wsdl:input message="impl:sayHelloToPersonRequest" name="sayHelloToPersonRequest"/>
41.
42. <wsdl:output message="impl:sayHelloToPersonResponse" name="sayHelloToPersonResponse"/>
43.
44. </wsdl:operation>
45.
46. </wsdl:portType>
47.
48. <wsdl:binding name="HelloServicesSoapBinding" type="impl:HelloService">
49.
50. <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
51.
52. <wsdl:operation name="sayHello">
53.
54. <wsdlsoap:operation soapAction=""/>
55.
56. <wsdl:input name="sayHelloRequest">
57.
58. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>
59.
60. </wsdl:input>
61.
62. <wsdl:output name="sayHelloResponse">
63.
64. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/>
65.
66. </wsdl:output>
67.
68. </wsdl:operation>
69.
70. <wsdl:operation name="sayHelloToPerson">
71.
72. <wsdlsoap:operation soapAction=""/>
73.
74. <wsdl:input name="sayHelloToPersonRequest">
75.
76. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>
77.
78. </wsdl:input>
79.
80. <wsdl:output name="sayHelloToPersonResponse">
81.
82. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/>
83.
84. </wsdl:output>
85.
86. </wsdl:operation>
87.
88. </wsdl:binding>
89.
90. <wsdl:service name="HelloServiceService">
91.
92. <wsdl:port binding="impl:HelloServicesSoapBinding" name="HelloServices">
93.
94. <wsdlsoap:address location="http://127.0.0.1:8080/WebServiceTest/services/HelloServices"/>
95.
96. </wsdl:port>
97.
98. </wsdl:service>
99.
100.</wsdl:definitions>
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:intf="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
<wsdl:message name="sayHelloToPersonRequest">
<wsdl:part name="name" type="soapenc:string"/>
</wsdl:message>
<wsdl:message name="sayHelloRequest">
</wsdl:message>
<wsdl:message name="sayHelloToPersonResponse">
<wsdl:part name="sayHelloToPersonReturn" type="soapenc:string"/>
</wsdl:message>
<wsdl:message name="sayHelloResponse">
<wsdl:part name="sayHelloReturn" type="soapenc:string"/>
</wsdl:message>
<wsdl:portType name="HelloService">
<wsdl:operation name="sayHello">
<wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>
<wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>
</wsdl:operation>
<wsdl:operation name="sayHelloToPerson" parameterOrder="name">
<wsdl:input message="impl:sayHelloToPersonRequest" name="sayHelloToPersonRequest"/>
<wsdl:output message="impl:sayHelloToPersonResponse" name="sayHelloToPersonResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloServicesSoapBinding" type="impl:HelloService">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="sayHelloRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="sayHelloToPerson">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="sayHelloToPersonRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>
</wsdl:input>
<wsdl:output name="sayHelloToPersonResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloServiceService">
<wsdl:port binding="impl:HelloServicesSoapBinding" name="HelloServices">
<wsdlsoap:address location="http://127.0.0.1:8080/WebServiceTest/services/HelloServices"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
用Java调用WebService实例
下面是用Java调用刚发布的WebService例子。
Java代码
1./*
2. * File name: TestHelloService.java
3. *
4. * Version: v1.0
5. *
6. * Created on Aug 2, 2008 9:54:10 AM
7. *
8. * Designed by Stephen
9. *
10. * (c)Copyright 2008
11. */
12.package test.com.sinosoft.webservice;
13.
14.import java.io.IOException;
15.import java.net.MalformedURLException;
16.
17.import javax.xml.namespace.QName;
18.import javax.xml.rpc.ServiceException;
19.
20.import org.apache.axis.client.Call;
21.import org.apache.axis.client.Service;
22.import org.apache.commons.logging.Log;
23.import org.apache.commons.logging.LogFactory;
24.
25./**
26. * @author Stephen
27. *
28. * 测试调用WebService
29. */
30.public class TestHelloService {
31. private static final Log log = LogFactory.getLog(TestHelloService.class);
32. private static final String HELLO_SERVICE_ENDPOINT = "http://127.0.0.1:8080/WebServiceTest/services/HelloServices?wsdl";
33.
34. public static void main(String[] args) {
35. TestHelloService tester = new TestHelloService();
36. // tester.callSayHello();
37. tester.callSayHelloToPerson();
38. }
39.
40. public void callSayHello() {
41. try {
42. Service service = new Service();
43. Call call = (Call) service.createCall();
44. call.setTargetEndpointAddress(new java.net.URL(
45. HELLO_SERVICE_ENDPOINT));
46. //下面名字查询的http://127.0.0.1:8080/WebServiceTest/services/HelloServices?wsdl文件里有
47. call.setOperationName(new QName("http://webservice.sinosoft.com/",
48. "sayHello"));
49. call.setReturnType(org.apache.axis.Constants.XSD_STRING);
50. try {
51. //远程调用发布的方法
52. String ret = (String) call.invoke(new Object[] {});
53. System.out.println("The return value is:" + ret);
54. return;
55. } catch (IOException e) {
56. e.printStackTrace();
57. }
58. } catch (MalformedURLException e) {
59. e.printStackTrace();
60. } catch (ServiceException e) {
61. e.printStackTrace();
62. }
63. log.error("call sayHello service error!");
64. }
65.
66. public void callSayHelloToPerson() {
67. try {
68. Service service = new Service();
69. Call call = (Call) service.createCall();
70. call.setTargetEndpointAddress(new java.net.URL(HELLO_SERVICE_ENDPOINT));
71. call.setOperationName(new QName("http://webservice.sinosoft.com/",
72. "sayHelloToPerson"));
73. call.addParameter("name", org.apache.axis.Constants.XSD_STRING,
74. javax.xml.rpc.ParameterMode.IN);
75. call.setReturnType(org.apache.axis.Constants.XSD_STRING);
76. try {
77. String ret = (String) call.invoke(new Object[] { "Stephen" });
78. System.out.println("The return value is:" + ret);
79. return;
80. } catch (IOException e) {
81. e.printStackTrace();
82. }
83. } catch (MalformedURLException e) {
84. e.printStackTrace();
85. } catch (ServiceException e) {
86. e.printStackTrace();
87. }
88. log.error("call sayHello service error!");
89. }
90.}
本例子的WebService提供了两个方法,分别是sayHello和sayHelloToPerson,第一个只是返回一个"Hello"字符串,没有参数,第二个函数接受一个字符串作为参数,返回"Hello 参数值",该例子比较简单,但是清楚的说明了从编写代码到发布为WebService以及测试编写好的WebService全过程。
编写服务代码
服务代码提供了两个函数,分别为sayHello和sayHelloToPerson,源代码如下:
Java代码
1./*
2. * File name: HelloService.java
3. *
4. * Version: v1.0
5. *
6. * Created on Aug 2, 2008 9:40:20 AM
7. *
8. * Designed by Stephen
9. *
10. * (c)Copyright 2008
11. */
12.package com.sinosoft.webservice;
13.
14./** *//**
15. * @author Stephen
16. *
17. * Test web service
18. */
19.public class HelloService {
20. /** *//**
21. * 不带参数的函数
22. *
23. * @return 返回Hello字符串
24. */
25. public String sayHello() {
26. return "Hello";
27. }
28.
29. /** *//**
30. * 带参数的函数
31. *
32. * @param name
33. * 名称
34. * @return 返回加上名称的欢迎词
35. */
36. public String sayHelloToPerson(String name) {
37. if (name == null || name.equals("")) {
38. name = "nobody";
39. }
40. return "Hello " + name;
41. }
42.}
/*
* File name: HelloService.java
*
* Version: v1.0
*
* Created on Aug 2, 2008 9:40:20 AM
*
* Designed by Stephen
*
* (c)Copyright 2008
*/
package com.sinosoft.webservice;
/** *//**
* @author Stephen
*
* Test web service
*/
public class HelloService {
/** *//**
* 不带参数的函数
*
* @return 返回Hello字符串
*/
public String sayHello() {
return "Hello";
}
/** *//**
* 带参数的函数
*
* @param name
* 名称
* @return 返回加上名称的欢迎词
*/
public String sayHelloToPerson(String name) {
if (name == null || name.equals("")) {
name = "nobody";
}
return "Hello " + name;
}
}
发布WebService
要将上边写的HelloService类发布为WebService,需要先搭建Web应用。下面是在Tomcat下使用Axis(http://ws.apache.org/axis/)创建WebService服务的例子。
在Tomcat下创建Web应用
在该例子中,在Tomcat下创建了一个context path为ws的WEB应用。
1. 在myeclipse中创建WebServiceTest的webproject
2. 在WEB-INF文件夹下web.xml文件,该文件的内容如下:
Xml代码
1.<?xml version="1.0" encoding="ISO-8859-1"?>
2.
3.<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
4.Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
5.
6.<web-app>
7. <display-name>Apache-Axis</display-name>
8.
9. <listener>
10. <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
11. </listener>
12.
13. <servlet>
14. <servlet-name>AxisServlet</servlet-name>
15. <display-name>Apache-Axis Servlet</display-name>
16. <servlet-class>
17. org.apache.axis.transport.http.AxisServlet
18. </servlet-class>
19. </servlet>
20.
21. <servlet>
22. <servlet-name>AdminServlet</servlet-name>
23. <display-name>Axis Admin Servlet</display-name>
24. <servlet-class>
25. org.apache.axis.transport.http.AdminServlet
26. </servlet-class>
27. <load-on-startup>100</load-on-startup>
28. </servlet>
29.
30. <servlet>
31. <servlet-name>SOAPMonitorService</servlet-name>
32. <display-name>SOAPMonitorService</display-name>
33. <servlet-class>
34. org.apache.axis.monitor.SOAPMonitorService
35. </servlet-class>
36. <init-param>
37. <param-name>SOAPMonitorPort</param-name>
38. <param-value>5001</param-value>
39. </init-param>
40. <load-on-startup>100</load-on-startup>
41. </servlet>
42.
43. <servlet-mapping>
44. <servlet-name>AxisServlet</servlet-name>
45. <url-pattern>/servlet/AxisServlet</url-pattern>
46. </servlet-mapping>
47.
48. <servlet-mapping>
49. <servlet-name>AxisServlet</servlet-name>
50. <url-pattern>*.jws</url-pattern>
51. </servlet-mapping>
52.
53. <servlet-mapping>
54. <servlet-name>AxisServlet</servlet-name>
55. <url-pattern>/services/*</url-pattern>
56. </servlet-mapping>
57.
58. <servlet-mapping>
59. <servlet-name>SOAPMonitorService</servlet-name>
60. <url-pattern>/SOAPMonitor</url-pattern>
61. </servlet-mapping>
62.
63. <!-- uncomment this if you want the admin servlet -->
64. <!--
65. <servlet-mapping>
66. <servlet-name>AdminServlet</servlet-name>
67. <url-pattern>/servlet/AdminServlet</url-pattern>
68. </servlet-mapping>
69. -->
70.
71. <session-config>
72. <!-- Default to 5 minute session timeouts -->
73. <session-timeout>5</session-timeout>
74. </session-config>
75.
76. <!-- currently the W3C havent settled on a media type for WSDL;
77. http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft
78. for now we go with the basic 'it's XML' response -->
79. <mime-mapping>
80. <extension>wsdl</extension>
81. <mime-type>text/xml</mime-type>
82. </mime-mapping>
83.
84.
85. <mime-mapping>
86. <extension>xsd</extension>
87. <mime-type>text/xml</mime-type>
88. </mime-mapping>
89.
90. <welcome-file-list id="WelcomeFileList">
91. <welcome-file>index.jsp</welcome-file>
92. <welcome-file>index.html</welcome-file>
93. <welcome-file>index.jws</welcome-file>
94. </welcome-file-list>
95.
96.</web-app>
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Apache-Axis</display-name>
<listener>
<listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
</listener>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis Servlet</display-name>
<servlet-class>
org.apache.axis.transport.http.AxisServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>AdminServlet</servlet-name>
<display-name>Axis Admin Servlet</display-name>
<servlet-class>
org.apache.axis.transport.http.AdminServlet
</servlet-class>
<load-on-startup>100</load-on-startup>
</servlet>
<servlet>
<servlet-name>SOAPMonitorService</servlet-name>
<display-name>SOAPMonitorService</display-name>
<servlet-class>
org.apache.axis.monitor.SOAPMonitorService
</servlet-class>
<init-param>
<param-name>SOAPMonitorPort</param-name>
<param-value>5001</param-value>
</init-param>
<load-on-startup>100</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SOAPMonitorService</servlet-name>
<url-pattern>/SOAPMonitor</url-pattern>
</servlet-mapping>
<!-- uncomment this if you want the admin servlet -->
<!--
<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/servlet/AdminServlet</url-pattern>
</servlet-mapping>
-->
<session-config>
<!-- Default to 5 minute session timeouts -->
<session-timeout>5</session-timeout>
</session-config>
<!-- currently the W3C havent settled on a media type for WSDL;
http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft
for now we go with the basic 'it's XML' response -->
<mime-mapping>
<extension>wsdl</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xsd</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<welcome-file-list id="WelcomeFileList">
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jws</welcome-file>
</welcome-file-list>
</web-app>
在上面的web.xml中主要是配置了axis的相关配置。
axis的相关配置
在上述的web.xml文件中已经对axis进行了配置,但是还需要进行额外的配置。
复制axis相关的jar文件
将axis的相关jar文件复制到WEB-INF\lib文件夹下。这些文件包括:
activation.jar
axis.jar
axis-ant.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
log4j-1.2.8.jar
saaj.jar
wsdl4j-1.5.1.jar
测试发布的Web应用
启动Tomcat服务,打开IE浏览器,访问地址http://127.0.0.1:8080/WebServiceTest/services,如果看到如下界面就说明AXIS部署成功了。
发布WebService
发布WebService需要使用现有的AdminService来实现,这里我写了一个批处理文件来发布WebService,以后如果需要发布其他文件,只需要修改相应的参数就可以了。
创建deploy.wsdd文件
文件deploy.wsdd内容如下所示:
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
3. <service name="HelloServices" provider="java:RPC">
4. <parameter name="className" value="com.sinosoft.webservice.HelloService"/>
5. <parameter name="allowedMethods" value="*"/>
6. </service>
7.</deployment>
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="HelloServices" provider="java:RPC">
<parameter name="className" value="com.sinosoft.webservice.HelloService"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>
创建发布WebService服务的批处理文件
批处理文件deploywebservice.bat内容如下:
Xml代码
1.java -cp axis-ant.jar;axis-schema.jar;axis.jar;commons-discovery-0.2.jar;commons-logging-1.0.4.jar;jaxrpc.jar;log4j-1.2.8.jar;saaj.jar;wsdl4j-1.5.1.jar;xmlsec-1.3.0.jar org.apache.axis.client.AdminClient -lhttp://localhost:8080/WebServiceTest/services/AdminService deploy.wsdd
2.
3.pause
java -cp axis-ant.jar;axis-schema.jar;axis.jar;commons-discovery-0.2.jar;commons-logging-1.0.4.jar;jaxrpc.jar;log4j-1.2.8.jar;saaj.jar;wsdl4j-1.5.1.jar;xmlsec-1.3.0.jar org.apache.axis.client.AdminClient -lhttp://localhost:8080/WebServiceTest/services/AdminService deploy.wsdd
pause
其中上面的jar包我都拷到和bat文件在同一个目录,现在将所有的jar文件都加入到classpath中进行执行。
-l后的参数是本地要发布WebService的AdminService对应的访问地址。
最后deploy.wsdd是对应的配置文件名称。
发布WebService服务
将deploy.wsdd文件和deploywebservice.bat文件复制到同一个文件夹下,执行deploywebservice.bat批处理文件,就可以将deploy.wsdd中描述的Java类发布为WebService。发布完成之后在访问http://host:port/ws/services如下图所示:
从上图可以看出,发布成功后,多了一个HelloServices的服务。这样就说明HelloService发布成功了。
查看HelloServices的wsdl
访问http://127.0.0.1:8080/WebServiceTest/services/HelloServices?wsdl可以看到如下wsdl的内容:
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<wsdl:definitions targetNamespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:intf="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3.<!--WSDL created by Apache Axis version: 1.4
4.Built on Apr 22, 2006 (06:55:48 PDT)-->
5.
6. <wsdl:message name="sayHelloToPersonRequest">
7.
8. <wsdl:part name="name" type="soapenc:string"/>
9.
10. </wsdl:message>
11.
12. <wsdl:message name="sayHelloRequest">
13.
14. </wsdl:message>
15.
16. <wsdl:message name="sayHelloToPersonResponse">
17.
18. <wsdl:part name="sayHelloToPersonReturn" type="soapenc:string"/>
19.
20. </wsdl:message>
21.
22. <wsdl:message name="sayHelloResponse">
23.
24. <wsdl:part name="sayHelloReturn" type="soapenc:string"/>
25.
26. </wsdl:message>
27.
28. <wsdl:portType name="HelloService">
29.
30. <wsdl:operation name="sayHello">
31.
32. <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>
33.
34. <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>
35.
36. </wsdl:operation>
37.
38. <wsdl:operation name="sayHelloToPerson" parameterOrder="name">
39.
40. <wsdl:input message="impl:sayHelloToPersonRequest" name="sayHelloToPersonRequest"/>
41.
42. <wsdl:output message="impl:sayHelloToPersonResponse" name="sayHelloToPersonResponse"/>
43.
44. </wsdl:operation>
45.
46. </wsdl:portType>
47.
48. <wsdl:binding name="HelloServicesSoapBinding" type="impl:HelloService">
49.
50. <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
51.
52. <wsdl:operation name="sayHello">
53.
54. <wsdlsoap:operation soapAction=""/>
55.
56. <wsdl:input name="sayHelloRequest">
57.
58. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>
59.
60. </wsdl:input>
61.
62. <wsdl:output name="sayHelloResponse">
63.
64. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/>
65.
66. </wsdl:output>
67.
68. </wsdl:operation>
69.
70. <wsdl:operation name="sayHelloToPerson">
71.
72. <wsdlsoap:operation soapAction=""/>
73.
74. <wsdl:input name="sayHelloToPersonRequest">
75.
76. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>
77.
78. </wsdl:input>
79.
80. <wsdl:output name="sayHelloToPersonResponse">
81.
82. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/>
83.
84. </wsdl:output>
85.
86. </wsdl:operation>
87.
88. </wsdl:binding>
89.
90. <wsdl:service name="HelloServiceService">
91.
92. <wsdl:port binding="impl:HelloServicesSoapBinding" name="HelloServices">
93.
94. <wsdlsoap:address location="http://127.0.0.1:8080/WebServiceTest/services/HelloServices"/>
95.
96. </wsdl:port>
97.
98. </wsdl:service>
99.
100.</wsdl:definitions>
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:intf="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
<wsdl:message name="sayHelloToPersonRequest">
<wsdl:part name="name" type="soapenc:string"/>
</wsdl:message>
<wsdl:message name="sayHelloRequest">
</wsdl:message>
<wsdl:message name="sayHelloToPersonResponse">
<wsdl:part name="sayHelloToPersonReturn" type="soapenc:string"/>
</wsdl:message>
<wsdl:message name="sayHelloResponse">
<wsdl:part name="sayHelloReturn" type="soapenc:string"/>
</wsdl:message>
<wsdl:portType name="HelloService">
<wsdl:operation name="sayHello">
<wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>
<wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>
</wsdl:operation>
<wsdl:operation name="sayHelloToPerson" parameterOrder="name">
<wsdl:input message="impl:sayHelloToPersonRequest" name="sayHelloToPersonRequest"/>
<wsdl:output message="impl:sayHelloToPersonResponse" name="sayHelloToPersonResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloServicesSoapBinding" type="impl:HelloService">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="sayHelloRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="sayHelloToPerson">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="sayHelloToPersonRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>
</wsdl:input>
<wsdl:output name="sayHelloToPersonResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloServiceService">
<wsdl:port binding="impl:HelloServicesSoapBinding" name="HelloServices">
<wsdlsoap:address location="http://127.0.0.1:8080/WebServiceTest/services/HelloServices"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
用Java调用WebService实例
下面是用Java调用刚发布的WebService例子。
Java代码
1./*
2. * File name: TestHelloService.java
3. *
4. * Version: v1.0
5. *
6. * Created on Aug 2, 2008 9:54:10 AM
7. *
8. * Designed by Stephen
9. *
10. * (c)Copyright 2008
11. */
12.package test.com.sinosoft.webservice;
13.
14.import java.io.IOException;
15.import java.net.MalformedURLException;
16.
17.import javax.xml.namespace.QName;
18.import javax.xml.rpc.ServiceException;
19.
20.import org.apache.axis.client.Call;
21.import org.apache.axis.client.Service;
22.import org.apache.commons.logging.Log;
23.import org.apache.commons.logging.LogFactory;
24.
25./**
26. * @author Stephen
27. *
28. * 测试调用WebService
29. */
30.public class TestHelloService {
31. private static final Log log = LogFactory.getLog(TestHelloService.class);
32. private static final String HELLO_SERVICE_ENDPOINT = "http://127.0.0.1:8080/WebServiceTest/services/HelloServices?wsdl";
33.
34. public static void main(String[] args) {
35. TestHelloService tester = new TestHelloService();
36. // tester.callSayHello();
37. tester.callSayHelloToPerson();
38. }
39.
40. public void callSayHello() {
41. try {
42. Service service = new Service();
43. Call call = (Call) service.createCall();
44. call.setTargetEndpointAddress(new java.net.URL(
45. HELLO_SERVICE_ENDPOINT));
46. //下面名字查询的http://127.0.0.1:8080/WebServiceTest/services/HelloServices?wsdl文件里有
47. call.setOperationName(new QName("http://webservice.sinosoft.com/",
48. "sayHello"));
49. call.setReturnType(org.apache.axis.Constants.XSD_STRING);
50. try {
51. //远程调用发布的方法
52. String ret = (String) call.invoke(new Object[] {});
53. System.out.println("The return value is:" + ret);
54. return;
55. } catch (IOException e) {
56. e.printStackTrace();
57. }
58. } catch (MalformedURLException e) {
59. e.printStackTrace();
60. } catch (ServiceException e) {
61. e.printStackTrace();
62. }
63. log.error("call sayHello service error!");
64. }
65.
66. public void callSayHelloToPerson() {
67. try {
68. Service service = new Service();
69. Call call = (Call) service.createCall();
70. call.setTargetEndpointAddress(new java.net.URL(HELLO_SERVICE_ENDPOINT));
71. call.setOperationName(new QName("http://webservice.sinosoft.com/",
72. "sayHelloToPerson"));
73. call.addParameter("name", org.apache.axis.Constants.XSD_STRING,
74. javax.xml.rpc.ParameterMode.IN);
75. call.setReturnType(org.apache.axis.Constants.XSD_STRING);
76. try {
77. String ret = (String) call.invoke(new Object[] { "Stephen" });
78. System.out.println("The return value is:" + ret);
79. return;
80. } catch (IOException e) {
81. e.printStackTrace();
82. }
83. } catch (MalformedURLException e) {
84. e.printStackTrace();
85. } catch (ServiceException e) {
86. e.printStackTrace();
87. }
88. log.error("call sayHello service error!");
89. }
90.}
相关推荐
### 利用Java编写简单的WebService实例 #### Axis2:新一代的WebService引擎 Axis2作为Apache组织下的一款开源WebService引擎,自其诞生起便受到了广泛的关注。相比于前代产品Axis1.x,Axis2进行了全面的重构与...
java对象与类实例java对象与类实例java对象与类实例java对象与类实例java对象与类实例java对象与类实例java对象与类实例java对象与类实例java对象与类实例java对象与类实例java对象与类实例java对象与类实例java对象...
综上所述,这个“java编写论坛框架实例”结合了iBatis、Struts2、Spring和Ajax,利用FTL进行页面设计,构建出一个功能完备、用户体验良好的在线论坛系统。每个组件都发挥着其独特的作用,共同协作以实现论坛的各项...
这是我收藏的小程序,用Java编写的碰撞的小球,我觉得写的挺好的,希望分享给大家!!
Java波浪文字,一个利用Java处理字符的实例,可以设置运动方向参数,显示文本的字符数组,高速文本颜色,显示字体的 FontMetrics对象,得到Graphics实例,得到Image实例,填充颜色数组数据,初始化颜色数组。...
在Java编程语言中,创建一个简单的记事本程序是一个经典的初学者项目,它可以帮助学习者理解面向对象编程、文件操作以及基本的用户交互。本文将详细介绍如何使用Java来实现这样一个简单记事本。 首先,我们需要了解...
java web 学习简单实例
总的来说,这个“java实现体温单实例-eclipse-java工程”涵盖了Java基础、面向对象编程、数据结构、GUI编程和测试等多个方面。通过学习这个实例,开发者不仅可以掌握Java编程的基本技能,还能了解到如何在Eclipse中...
"JAVA简单入门实例"这个压缩包提供了一百八十个小案例,旨在帮助新手快速掌握Java的基本语法和编程技巧。 1. **Java基本语法**: 这些实例将涵盖Java的基础语法,包括变量声明、数据类型(如整型、浮点型、字符型、...
【Java编写的万年历】是一个实用的编程项目,它展示了如何使用Java语言来实现一个功能完备的日历系统,能够显示从公元1年至公元9999年间的日期信息。这个程序对于初学者来说,是一个很好的学习Java编程、日期处理...
java源码包J实例源码JAVA开发源码55个合集: Java中的Blowfish对称密钥加密算法类和实例.rar Java中的EJB编程实例代码.rar Java中的SSL及HTTPS协议实例源码.rar Java写的ATM机取款模拟程序.zip Java写的一个mp3播放器...
【JAVA编写的简单聊天软件】是一个使用JAVA编程语言开发的简易通信应用,它允许用户进行基本的文字交流。这个项目的核心在于实现客户端(Client)与服务器端(Server)之间的实时通信,通过交换数据包来传递消息。...
可惜网上实例太少,只好根据别人的思路写一下这个简单的实例。以后也许会补充图片导出Word模块,敬请其代表吧!还有,本实例只适合初学者。大牛绕路。当然本实例也存在不足之处,请各位多多包含。
这里我们探讨的是"Java编写的一些简单实例",这些实例通常涵盖了基础语法、数据结构、控制流、类与对象等核心概念,是初学者巩固理论知识和实践经验的好资料。 1. **基础语法** - 变量声明:Java中的变量必须先...
Java编程语言是世界上最流行的编程语言之一,以其“一次编写,到处运行”的特性闻名。它具有丰富的类库,广泛应用于企业级应用、移动开发(尤其是Android)、大数据处理、云计算等多个领域。对于初学者而言,理解并...
java源码包实例源码JAVA开发源码50个合集: Ajax框架 ZK.rar Java图书馆管理系统源程序.rar Java图片倒影效果实例源码.rar Java图片翻折,将图像压扁.rar Java坦克大战网络对战版源代码.rar Java声音播放程序源代码....
本教程主要聚焦于使用Java语言和特定技术栈创建Web服务的实例。我们将深入探讨以下几个关键知识点: 1. **Java Web Service**: Java Web Service是使用Java语言开发的、遵循Web服务标准(如SOAP、WSDL和UDDI)的...
用java2D开发的小型计算器,有较强的容错能力。
在本实例中,我们主要探讨如何使用Java的Servlet技术与MySQL数据库进行交互,这是一个基于DAO(Data Access Object)模式但不使用Bean的简单应用。首先,让我们深入了解一下Servlet、DAO模式以及MySQL数据库的基础...