前两天学习了使用Axis开发WebService程序,写了一个HelloWorld程序,感觉流程明白了,可是其中的原理还是不够清楚,今天在写一个demo,使用别人发布的一个天气预报webservice,根据提供的wsdl文件,生成本地的java框架,写自己的业务方法,使用jsp做页面展示.
这个天气预报的webservice来自于网上http://fhs.6617.com/getweather.asmx,下面我们将使用最原始的开发方法,不实用任何辅助工具来写代码,这样有助于我们了解详细的步骤和其中的原理.
目的:这个demo要做成web项目,在jsp页面上,用户输入城市名称后,调用webservice,显示出天气预报的信息
步骤:
1. 首先建立项目的目录结构,我们这里还是使用,axis-bin-1_4.zip包中的axis目录.本demo的目录为D:\jakarta-tomcat-5.5.7\webapps\axis
2.使用 命令提示符 进入D:\jakarta-tomcat-5.5.7\webapps\axis,设置classpath,主要是编译文件所需要的一些jar包的路径,关于设置classpath 可以参考我的另一篇博客 使用Axis框架开发webservice的HelloWord 中的init.bat文件
3.得到wsdl(webservice描述文件),使用WSDL2Java 命令将它转化为本地java框架,方便自己调用
a.进入http://fhs.6617.com/getweather.asmx?WSDL 你将看到这个webservice的描述,复制其中的内容,保存为getweather.wsdl ,放在axis目录下
b.在设置好环境变量,命令提示符为当前的D:\jakarta-tomcat-5.5.7\webapps\axis 目录后 运行 java org.apache.axis.wsdl.WSDL2Java getweather.wsdl -s 之后你可以看到该目录下多了一个文件夹 com 其中的 目录结构为 axis\com\_6617\fhs\WeatherService 下面有 11个文件,其中有9个 .java文件 和两个 wsdd文件
c.由于我们使用了 -s 选项,则生成了两个wsdd 文件,其中deploy.wsdd 是帮助我们发布webservice 的,undeploy.wsdd,是卸载webservice服务的.
d.我们也可以不下载getweather.wsdl 文件,使用下面这种方式也可以生成本地java文件框架
java org.apache.axis.wsdl.WSDL2Java http://fhs.6617.com/getweather.asmx?WSDL
4.进入axis\com\_6617\fhs\WeatherService 文件夹下,编译这些.java文件 javac *.java
如果这一步有问题,一般是classpath变量设置的问题,仔细检查所需要的jar 包是否都设置正确了吗?还有我们编译可能会遇到一大堆警告,没关系,这是字符编码的问题,不属于错误,不影响我们后续的使用,将这些.class文件拷贝到axis\WEB-INF\classes 目录下
5.这时候我们客户端的框架就搭好了,现在我们可以在写jsp页面,servlet,字符集过滤器了,为了方便开发,我们现在使用eclipse,在eclipse下新建立一个web项目名字交 webservice,按照web项目的目录结构将 我们的axis目录下的 文件分门别类的放入到webservice目录下,比如 java源文件放在src下,.class文件放在web-inf/class下等. 注意把servlet.jar包和 jsp.jar包添加到项目的classpath中
6.在webservice项目的根目录下写3个页面
a.index.html 这是一个框架页面 一个frameset 中套两个frame (我们把页面分成上下两个部分)
b.top.html 这是页面上部分的查询页面 用户输入城市名称 点击查询 在下部分的页面中显示查询结果
c.bottom.jsp 这是西部分的结果显示页面
如图看项目结构,包括时候WSDL2Java 生成的9个java文件
index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head><title>天气预报</title></head>
<frameset rows="25%,*%" Borders="No" border="0">
<frame src="top.html" noresize="false" name="top" frameborder="0">
<frame src="bottom.jsp" noresize="true" name="bottom" frameborder="0">
</frameset>
</html>
top.html
<html>
<head><title></title>
<style type="text/css">
<!--
.style1 {
color: #FF0000;
font-weight: bold;
font-size: 12px;
}
-->
</style>
</head>
<script type="text/javascript">
function check()
{
if(document.getElementById("city").value==null || document.getElementById("city").value=="")
{
window.alert ('城市名称必须填写');
return;
}
document.forms[0].submit();
}
</script>
<body>
<p><b>WebService 天气预报(下一个Demo是IP地址查询,敬请期待!)</b></p>
<p class="style1">请输入需要查询的城市名称:</p>
<form name="form1" method="post" action="invoke" target="bottom">
<input type="text" name="city" id="city">
<input type="button" name="sub" value="查询" οnclick="check()">
</form>
</body>
</html>
bottom.jsp
<%@ page contentType="text/html; charset=utf-8"%>
<head><title>WebService 天气预报</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css">
</head>
7.在web开发中,一个头疼的问题就是字符编码问题,在这里我们虽然是一个demo,为了有好的习惯,我们也来使用一个过滤器,来解决字符编码的问题,而不是每次在servlet中使用request.setCharacterEncoding(),或者用new String(....)等来手动转码.
a.这个过滤器是SetCharacterEncodingFilter.java 它继承子Filter
package com._6617.fhs.WeatherService;
import java.io.IOException;
import javax.servlet.*;
import org.apache.log4j.Logger;
public class SetCharacterEncodingFilter implements Filter
{
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void init(FilterConfig filterConfig) throws ServletException
{
Logger log=Logger.getLogger(SetCharacterEncodingFilter.class);
log.info("过滤器被调用!!!");
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
if (ignore || (request.getCharacterEncoding() == null))
{
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
response.setContentType("text/html; charset="+encoding);
chain.doFilter(request, response);
}
protected String selectEncoding(ServletRequest request)
{
return (this.encoding);
}
public void destroy()
{
this.encoding = null;
this.filterConfig = null;
}
}
b.部署过滤器,在web.xml中
<filter> <filter-name>SetCharacterEncodingFilter</filter-name> <filter-class>com._6617.fhs.WeatherService.SetCharacterEncodingFilter</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>
8.写业务方法和处理的servlet
a. InvokeWS.java 这是一个标准的servlet,根据用户的请求来执行相应的业务方法
package com.business;
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;
public class InvokeWS extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
WSWeather wsw= new BaseBusiness().getManage().getWeatherManage();
PrintWriter pw=resp.getWriter();
String city=req.getParameter("city");
if(city!=null && city.length()!=0)
{
if(wsw.isOK(city))
{
String[] weatherinfo;
try
{
weatherinfo = wsw.getWeatherByCity(city);
for(int i=0;i<weatherinfo.length;i++)
{
pw.println("<font size='2' color='blue'>"+weatherinfo[i]+"</font><br>");
}
}
catch (ServiceException e)
{
e.printStackTrace();
}
}
else
{
pw.println("<font size='2' color='blue'>"+"没有查到你要的城市,请联系:134********"+"</font><br>");
}
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
doGet(req, resp);
}
}
在这里我们使用了Face模式,这样方便我们后续IP地址查询,手机号码查询等模块的开发
b.BaseBusiness.java 获取所有业务的接口
package com.business;
//获取所有业务的接口
public class BaseBusiness
{
protected ManageFaced getManage()
{
return new ManagefacedImp();
}
}
c.ManageFaced.java 标准接口,里面包含了得到单个业务的管理类
package com.business;
public interface ManageFaced
{
//获取查询天气的管理类
public WSWeather getWeatherManage();
//可以在加上查询IP的管理类
}
d.ManagefacedImp.java 实现接口中的方法,根据不同的业务,实现不同的业务
package com.business;
public class ManagefacedImp implements ManageFaced
{
//获取天气信息
public WSWeather getWeatherManage()
{
return new WSWeather();
}
//在这里可以添加获取IP信息
}
e.WSWeather.java 单个具体业务的实现类
package com.business;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import com._6617.fhs.WeatherService.Weather_x0020_WebServiceLocator;
import com._6617.fhs.WeatherService.Weather_x0020_WebServiceSoap;
public class WSWeather
{
//判断用户查询结果是否存在
public boolean isOK(String city)
{
//getCityWeather()返回的是一个对象数组
Object[] o=null;
Weather_x0020_WebServiceLocator locator= new Weather_x0020_WebServiceLocator();
try
{
Weather_x0020_WebServiceSoap service=locator.getWeather_x0020_WebServiceSoap12();
o=service.getCityWeather(city);
}
catch(Exception e)
{
e.printStackTrace();
}
String flagcity=(String)o[0];
if(flagcity.indexOf(city)!=-1)
{
return true;
}
else
{
return false;
}
}
public String[] getWeatherByCity(String city) throws ServiceException, RemoteException
{
Weather_x0020_WebServiceLocator locator= new Weather_x0020_WebServiceLocator();
Weather_x0020_WebServiceSoap service=locator.getWeather_x0020_WebServiceSoap12();
Object[] o=service.getCityWeather(city);
System.out.println(o.length);
if(o!=null || o.length!=0)
{
String[] info=new String[o.length];
for(int i=0;i<o.length;i++)
{
info[i]=o[i].toString();
}
return info;
}
else
{
return null;
}
}
}
9.在web.xml中配置InvokeWS..java这个servlet,在TocmcatD:\jakarta-tomcat-5.5.7\conf\Catalina\localhost中配置这个项目的访问路径
a.在web.xml中
<servlet> <servlet-name>InvokeWS</servlet-name> <servlet-class>com.business.InvokeWS</servlet-class> </servlet> <servlet-mapping> <servlet-name>InvokeWS</servlet-name> <url-pattern>/invoke</url-pattern> </servlet-mapping>
b.配置访问路径 axistest.xml (在TocmcatD:\jakarta-tomcat-5.5.7\conf\Catalina\localhost下,根据自己的tomcat安装路径)
<Context path="/axistest" docBase="D:\workspace\webservice" reloadable="true"> </Context>
10.启动Tomcat 访问 http://localhost:8080/axistest/index2.html 可以看到下面的画面 输入 西安 点击查询
未处理
1.业务方法中 如何根据生成的java方法 调webservice
2.关于再次发布的问题