加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

ws之天气预报

发布时间:2020-12-17 00:36:23 所属栏目:安全 来源:网络整理
导读:? ?前两天学习了使用Axis开发WebService程序,写了一个HelloWorld程序,感觉流程明白了,可是其中的原理还是不够清楚,今天在写一个demo,使用别人发布的一个天气预报webservice,根据提供的wsdl文件,生成本地的java框架,写自己的业务方法,使用jsp做页面展示. ????

? ?前两天学习了使用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.7webappsaxis

2.使用 命令提示符 进入D:jakarta-tomcat-5.5.7webappsaxis,设置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.7webappsaxis 目录后 运行 java org.apache.axis.wsdl.WSDL2Java getweather.wsdl -s?? 之后你可以看到该目录下多了一个文件夹 com 其中的 目录结构为 axiscom_6617fhsWeatherService 下面有 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.进入axiscom_6617fhsWeatherService 文件夹下,编译这些.java文件?? javac *.java

??? 如果这一步有问题,一般是classpath变量设置的问题,仔细检查所需要的jar 包是否都设置正确了吗?还有我们编译可能会遇到一大堆警告,没关系,这是字符编码的问题,不属于错误,不影响我们后续的使用,将这些.class文件拷贝到axisWEB-INFclasses 目录下

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

?

Html代码??

收藏代码

  1. <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.0?Transitional//EN">??
  2. <htmlhead>title>天气预报</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>??

top.html???

style?type="text/css" <!--??
  • .style1?{??
  • ????color:?#FF0000;??
  • ????font-weight:?bold;??
  • ????font-size:?12px;??
  • }??
  • --stylescript?type="text/javascript" ????function?check()??
  • ????{??
  • ??????????
  • ????????if(document.getElementById("city").value==null?||?document.getElementById("city").value=="")??
  • ????????{??
  • ????????window.alert?('城市名称必须填写');??
  • ????????return;??
  • ????????}??
  • ????????document.forms[0].submit();??
  • ????}??
  • scriptbody ?pb>WebService?天气预报(下一个Demo是IP地址查询,敬请期待!)p?class="style1">请输入需要查询的城市名称: ??
  • form?name="form1"?method="post"?action="invoke"?target="bottom" ?????
  • ?????input?type="text"?name="city"?id="city"input?type="button"?name="sub"?value="查询"?onclick="check()" ???
  • form bottom.jsp

    <%@?page?contentType="text/html;?charset=utf-8"%>WebService?天气预报meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8" ???
  • 7.在web开发中,一个头疼的问题就是字符编码问题,在这里我们虽然是一个demo,为了有好的习惯,我们也来使用一个过滤器,来解决字符编码的问题,而不是每次在servlet中使用request.setCharacterEncoding(),或者用new String(....)等来手动转码.

    ??? a.这个过滤器是SetCharacterEncodingFilter.java 它继承子Filter

    Java代码??

    收藏代码

      package?com._6617.fhs.WeatherService;??
    1. import?java.io.IOException;??
    2. import?javax.servlet.*;??
    3. import?org.apache.log4j.Logger;??
    4. public?class?SetCharacterEncodingFilter?implements?Filter??
    5. {??
    6. ????protected?String?encoding?=?null;??
    7. protected?FilterConfig?filterConfig?=?protected?boolean?ignore?=?true;??
    8. ??????
    9. void?init(FilterConfig?filterConfig)?throws?ServletException??
    10. ????????Logger?log=Logger.getLogger(SetCharacterEncodingFilter.class);??
    11. ????????log.info("过滤器被调用!!!");??
    12. ????????this.filterConfig?=?filterConfig;??
    13. this.encoding?=?filterConfig.getInitParameter("encoding");??
    14. ????????String?value?=?filterConfig.getInitParameter("ignore");??
    15. if?(value?==?null)??
    16. ????????????this.ignore?=?else?if?(value.equalsIgnoreCase("true"))??
    17. if?(value.equalsIgnoreCase("yes"))??
    18. else??
    19. false;??
    20. void?doFilter(ServletRequest?request,??
    21. ?????????????????????????ServletResponse?response,250); line-height:18px"> ?????????????????????????FilterChain?chain)??
    22. ??????????????????throws?IOException,?ServletException??
    23. if?(ignore?||?(request.getCharacterEncoding()?==?null))??
    24. ????????????String?encoding?=?selectEncoding(request);??
    25. if?(encoding?!=? ????????????????request.setCharacterEncoding(encoding);??
    26. ????????response.setContentType("text/html;?charset="+encoding);??
    27. ????????chain.doFilter(request,?response);??
    28. protected?String?selectEncoding(ServletRequest?request)??
    29. return?(this.encoding);??
    30. void?destroy()??
    31. this.encoding?=?this.filterConfig?=? }??

    ?b.部署过滤器,在web.xml中

    Xml代码?? filter ????????filter-name>SetCharacterEncodingFilterfilter-class>com._6617.fhs.WeatherService.SetCharacterEncodingFilterinit-param ????????????param-name>encodingparam-value>GBK>ignore>truefilter-mappingurl-pattern>/* ?8.写业务方法和处理的servlet

    ?? a. InvokeWS.java? 这是一个标准的servlet,根据用户的请求来执行相应的业务方法

    package?com.business;??
  • 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;??
  • class?InvokeWS?extends?HttpServlet??
  • 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();??
  • ??????????????????????
  • ??????????????????
  • ????????????}??
  • ????????????????pw.println("<font?size='2'?color='blue'>"+"没有查到你要的城市,请联系:134********"+"</font><br>");??
  • ????????}?????
  • void?doGet(HttpServletRequest?req,250); line-height:18px"> ????????doGet(req,?resp);??
  • 在这里我们使用了Face模式,这样方便我们后续IP地址查询,手机号码查询等模块的开发

    ???b.BaseBusiness.java???????获取所有业务的接口

    //获取所有业务的接口??
  • public??class?BaseBusiness??
  • protected?ManageFaced?getManage()??
  • return?new?ManagefacedImp();??
  • ?? c.ManageFaced.java???????标准接口,里面包含了得到单个业务的管理类

    interface?ManageFaced??
  • ????//获取查询天气的管理类??
  • public?WSWeather?getWeatherManage();??
  • //可以在加上查询IP的管理类??
  • ?? d.ManagefacedImp.java? 实现接口中的方法,根据不同的业务,实现不同的业务

    class?ManagefacedImp?implements?ManageFaced??
  • //获取天气信息??
  • public?WSWeather?getWeatherManage()??
  • new?WSWeather();??
  • //在这里可以添加获取IP信息??
  • ?? e.WSWeather.java??????????单个具体业务的实现类??????

    import?java.rmi.RemoteException;??

  • import?com._6617.fhs.WeatherService.Weather_x0020_WebServiceLocator;??
  • import?com._6617.fhs.WeatherService.Weather_x0020_WebServiceSoap;??
  • class?WSWeather??
  • //判断用户查询结果是否存在??
  • boolean?isOK(String?city)???
  • ????????//getCityWeather()返回的是一个对象数组??
  • ????????Object[]?o= ????????Weather_x0020_WebServiceLocator?locator=?new?Weather_x0020_WebServiceLocator();??
  • ????????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)??
  • ?????????{??
  • ??????????????
  • ????????????? ?????????}??
  • ?????????public?String[]?getWeatherByCity(String?city)?throws?ServiceException,?RemoteException??
  • ????????Object[]?o=service.getCityWeather(city);??
  • ????????System.out.println(o.length);??
  • if(o!=null?||?o.length!= ????????{?????
  • ????????????String[]?info=new?String[o.length];??
  • 0;i<o.length;i++)??
  • ????????????????info[i]=o[i].toString();??
  • return?info;??
  • 9.在web.xml中配置InvokeWS..java这个servlet,在TocmcatD:jakarta-tomcat-5.5.7confCatalinalocalhost中配置这个项目的访问路径

    a.在web.xml中

    servlet ???servlet-name>InvokeWS ????
  • servlet-class>com.business.InvokeWSservlet-mapping>/invoke b.配置访问路径 axistest.xml (在TocmcatD:jakarta-tomcat-5.5.7confCatalinalocalhost下,根据自己的tomcat安装路径)

    Context?path="/axistest"?docBase="D:workspacewebservice"?reloadable="true"Context 10.启动Tomcat?访问?http://localhost:8080/axistest/index2.html?可以看到下面的画面 输入 西安 点击查询

    ?

    ?

    ?

    未处理

    1.业务方法中 如何根据生成的java方法 调webservice

    2.关于再次发布的问题?

  • (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读