通过 jsp+ajax+servlet+webservice 远程访问天气预报服务

通过 jsp+ajax+servlet+webservice 远程访问天气预报服务
   - webservice 客户端访问的方式
     1. java代码来访问
     2. ajax 方式异步加载webservice

   - 通过 ajax 异步加载 天气信息实现步骤:
     1. 了解远程第三方的天气预报webservice服务
        广东,31124   广州,2350   东莞,2351  深圳,2419   温度信息  String[8]
     2. wsimport 生成本地代理
     3. 准备一个index.jsp  页面,显示城市信息 ,下拉城市,显示天气信息  
     4. 准备一个weatherServlet ,通过它来访问webservcie
     5. Servlet要在web .xml注册

  通过Jdk  
   声明 :@Webservice
     发布 :EndPoint
      不足: 希望tomcat启动时,webservice服务能够开启 ,最好与Spring集成
              希望有一个webservice的服务列表
   通过Webservice框架来实现 :axis2  、xfire

 

1.通过wsimport生成本地代理与打成jar包

wsimport http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL

发现产生错误:

将源码考到本地,将错误行删除(15,61,101),然后存为xml文件后重新进行wsimport,发现只有三个警告,同时生成对应的class文件打成jar包。

C:\Users\liqiang\Desktop>wsimport weather.xml
parsing WSDL...


[WARNING] Ignoring SOAP port "WeatherWSSoap12": it uses non-standard SOAP 1.2 bi
nding.
You must specify the "-extension" option to use this binding.
  line 533 of file:/C:/Users/liqiang/Desktop/weather.xml

[WARNING] ignoring port "WeatherWSHttpGet": no SOAP address specified. try runni
ng wsimport with -extension switch.
  line 536 of file:/C:/Users/liqiang/Desktop/weather.xml

[WARNING] ignoring port "WeatherWSHttpPost": no SOAP address specified. try runn
ing wsimport with -extension switch.
  line 539 of file:/C:/Users/liqiang/Desktop/weather.xml


Generating code...


Compiling code...


C:\Users\liqiang\Desktop>jar -cvf weather.jar ./cn

 

2.index.jsp:选择地区的时候异步请求天气情况

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <script type="text/javascript" src="jquery-1.8.2.js"></script>
  </head>
  
  <body>
   <select id="province">
   <option value="31124">广东省</option>
   </select>
   <select id="city">
   <option value="2350">广州</option>
   <option value="2419">深圳</option>
   <option value="2351">东莞</option>
   </select>
   <hr/>
   <span>XXXX</span>
   <script type="text/javascript">
     $("#city").change(function(){
       var city=$("#city option:selected").val();
       $.post("weatherServlet",{"city":city},function(backdata){
         $("span").text(backdata).css("color","blue");
       });
     });
   </script>
  </body>
</html>

 

 

 

3.接收请求的servlet

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.webxml.WeatherWS;
import cn.com.webxml.WeatherWSSoap;

public class WeatherServlet extends HttpServlet {
    private WeatherWS ws;

    public WeatherServlet() {
        super();
    }
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String cityCode=request.getParameter("city");
        System.out.println("获取城市的id"+cityCode);
        //通过webservice获取远程的天气预报信息
        WeatherWSSoap weatherWSSoap = ws.getWeatherWSSoap();
        List<String> weathers = weatherWSSoap.getWeather(cityCode, "").getString();
        String weather=weathers.get(8);//取得温度信息
        
        //把结果回显给页面
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter printWriter=response.getWriter();
        printWriter.write(weather);
        printWriter.flush();
        printWriter.close();
        
    }

    public void init() throws ServletException {
        // Put your code here
        ws=new WeatherWS();
    }

}

 

 

效果:

 

posted @ 2017-09-21 20:04  QiaoZhi  阅读(695)  评论(0编辑  收藏  举报