利用 axis 访问webservice有两种实现方式:
1. parse -> call -> return obj -> extract
2. generate stub by wsdl2java, then uses the stub to access
service.
下面介绍用第2种方式做一个web project:
1. install myeclipse (myeclipse 5.0 + eclipse 3.2)
2. create a web project named axis
3. create a generator to convert wsdl to stubs
package generator;
import java.util.List;
import org.apache.axis.utils.CLArgsParser;
import org.apache.axis.utils.CLOption;
import org.apache.axis.wsdl.WSDL2Java;
public class wsdl2java {
public static void main(String args[]) {
args = new String[] { "http://www.deeptraining.com/webservices/weather.asmx?WSDL" };
new MyWSDL2Java().generateWSDL(args);
}
static class MyWSDL2Java extends WSDL2Java {
public void generateWSDL(String[] args) {
org.apache.axis.utils.CLArgsParser argsParser = new CLArgsParser(
args, org.apache.axis.wsdl.WSDL2Java.options);
List<CLOption> clOptions = argsParser.getArguments();
for (int i = 0; i < clOptions.size(); i++) {
parseOption(clOptions.get(i));
}
validateOptions();
try {
parser.run(wsdlURI);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
run above program, stub will be generted. they are all in the
package com.litwinconsulting.webservices
4. create business classes
package business;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import com.litwinconsulting.webservices.WeatherLocator;
import com.litwinconsulting.webservices.WeatherSoap;
/**
* call stub to communicate with weather service.
* @author tychu
*/
public class WSWeather {
public String getWeatherByCity(String city) throws ServiceException,
RemoteException {
WeatherLocator locator = new WeatherLocator();
WeatherSoap service = locator.getWeatherSoap12();
return service.getWeather(city);
}
}
package business;
public class ManagefacedImp implements IManageFaced {
// invoked to create implementation object.
public WSWeather getWeatherManage() {
return new WSWeather();
}
}
package business;
/**
* all interface of providing services
* @author tychu
*/
public interface IManageFaced {
// get weather service
public WSWeather getWeatherManage();
}
5. create filter and servlet
package servlet;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.log4j.Logger;
/**
* uses filter to avoid to specify charset in every servlets
* @author tychu
*/
public class CharacterFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void init(FilterConfig fConfig) throws ServletException {
this.filterConfig = fConfig;
this.encoding = fConfig.getInitParameter("encoding");
String value = fConfig.getInitParameter("ignore");
if (value == null || "true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value))
this.ignore = true;
else
this.ignore = false;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
Logger log = Logger.getLogger(CharacterFilter.class);
if (ignore || (request.getCharacterEncoding() == null)) {
if (encoding != null) {
request.setCharacterEncoding(encoding);
log.info("filter used!!!");
}
}
response.setContentType("text/html; charset=" + encoding);
chain.doFilter(request, response);
}
public void destroy() {
// do nothing
}
}
package 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;
import javax.xml.rpc.ServiceException;
import org.apache.log4j.Logger;
import business.ManagefacedImp;
import business.WSWeather;
/**
* servlet invokes bussiness interface
* @author tychu
*/
public class InvokeWS extends HttpServlet {
private static final long serialVersionUID = 1646431374969L;
Logger logger = Logger.getLogger(InvokeWS.class.getName());
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// PropertyConfigurator.configure("log4j.properties");
WSWeather wsw = new ManagefacedImp().getWeatherManage();
PrintWriter pw = resp.getWriter();
String city = req.getParameter("city");
// if no filter set, you will get some unrecognized characters here
// logger.info("city:" + city);
if (city != null && city.length() != 0) {
String weatherinfo;
try {
weatherinfo = wsw.getWeatherByCity(city);
pw.println("<font size='2' color='blue'>" + weatherinfo
+ "</font><br>");
} catch (ServiceException e) {
e.printStackTrace();
}
} else {
pw.println("<font size='2' color='blue'>"
+ "the specified city not exists, please contact:********" + "</font><br>");
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
6. add servlet and filter to web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>
servlet.CharacterFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>InvokeWS</servlet-name>
<servlet-class>servlet.InvokeWS</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InvokeWS</servlet-name>
<url-pattern>/invoke</url-pattern>
</servlet-mapping>
</web-app>
7. web pages
index.htm
<html>
<head>
<title>WebService weather</title>
</head>
<frameset rows="5%,*%" Borders="No" border="0">
<frame src="top.htm" noresize="false" name="top" frameborder="0">
<frame src="bottom.htm" noresize="true" name="bottom" frameborder="0">
</frameset>
</html>
top.htm
<html>
<head>
<title>weather</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<form name="form1" method="post" action="invoke" target="bottom">
<input type="text" name="city" id="city">
<input type="submit" name="sub" value="GO">
</form>
</body>
</html>
bottom.htm
<head>
<title>weather</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css"/>
</head>
8. deploy and run
at location: http://localhost:8080/axis/
and input any
thing, you can get a result returned from servers.
分享到:
相关推荐
### Axis开发Web Service实例详解 #### 一、概述 在探讨如何使用Apache Axis来开发Web Service之前,我们首先需要了解一些基本概念。 **Web Service**是一种标准的技术框架,用于实现不同平台之间的应用通信。它...
接下来,通过一个简单实例——SayHello服务,了解使用Axis开发Web Service的全流程。 **2.1 WSDL编写** - 在MyEclipse中创建一个Web Project,命名为`SayHello`。 - 通过`File -> New -> Other -> MyEclipse -> ...
标题中的"web service Axis项目实例"表明我们将探讨使用Axis框架来创建和使用Web服务。Axis提供了从WSDL(Web Services Description Language)到Java代码的绑定,以及从Java类到WSDL的自动生成,简化了Web服务的...
在这个"Web Service Axis1.4 完整的实例"中,我们将深入探讨Axis1.4版本的相关知识点。 首先,了解Axis1.4的基础概念至关重要。它是Apache Axis的第1.4个版本,主要支持SOAP(Simple Object Access Protocol)协议...
本文档将对Web Service Axis进行详细总结,并提供相关实例说明。 1. Web Service简介 Web服务是通过HTTP协议传输数据的一种方式,它利用SOAP(Simple Object Access Protocol)作为消息传输格式,WSDL(Web ...
【标题】基于Tomcat5.0和Axis2开发Web Service应用实例 在Web服务的世界里,Axis2是一个高效且强大的工具,它允许开发者创建、部署和使用SOAP Web服务。本教程将详细介绍如何利用Apache Tomcat 5.0作为应用服务器,...
Axis+MyEclipse6.0+Tomcat5.0开发Web Service实例总结
总之,这个基于Tomcat 5.0和Axis2的Web Service实例旨在帮助开发者理解Web Service的工作原理,以及如何在Java环境下使用这些工具进行开发。通过实践这些步骤,你将能够熟练地创建、部署和调用Web Service,为构建...
### Axis开发Web Service的实例详解 #### 一、概述 在现代软件开发中,Web服务是一种重要的技术,它允许不同应用程序之间通过网络进行通信。Apache Axis是实现Web服务的一个流行框架,它支持SOAP协议,并提供了...
Web服务Axis 1.6是Apache软件基金会开发的一个开源工具,专门用于构建和部署Web服务。它是基于Java的,能够使开发者轻松地将现有业务逻辑转换为Web服务,或者消费其他发布的Web服务。在本文中,我们将深入探讨Axis ...
好像没有多少人讨论, 大多数的话题都是围绕xfire, cxf, axis/axis2等主流的Web Service框架.尽管是从事这方面的工作, 不过实际开发中还是公司内部开发的一个Web Service模块, 发现与Spring提供的这个模块的构架很像...
三、Web Service实例应用 1. **服务端创建**:使用Axis2,开发者可以轻松地将Java类转换为Web Service。通过编写Java代码,定义服务接口,然后使用Axis2工具生成服务部署描述符(WSDL),最后将服务部署到服务器上...
Web Service原理及应用 Web Service是一种基于互联网的、平台无关的、标准化的接口技术,它允许不同的应用程序之间进行通信和数据交换。Web Service的核心概念在于使用XML(可扩展标记语言)来描述服务,SOAP(简单...
基于Tomcat5_0和Axis2开发Web Service应用实例,上面的例子简单实用
2. **示例客户端**:演示如何使用Axis生成客户端代码并调用发布的Web服务,这通常包括从WSDL生成客户端存根、实例化客户端对象及调用服务方法。 3. **配置文件**:可能包含axis2.xml或服务特定的配置文件,这些文件...