經過ajax訪問遠程天氣預報服務

http://www.webxml.com.cn/zh_cn/index.aspxjavascript

更改wsdl文件css

打開文件將15行,51行,101行去掉html

而後把文件複製到c盤java

而後在桌面上面就生成了文件jquery

將文件打成jar包web

package cn.it.ws.weather;

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

import javax.servlet.ServletConfig;
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;

/**
 * Servlet implementation class weatherServlet
 */

public class WeatherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private WeatherWS ws;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WeatherServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		ws = new WeatherWS();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected 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();
	}

}
<%@ 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>

web.xmlapp

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>WeatherServlet</servlet-name>
    <servlet-class>cn.it.ws.weather.WeatherServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>WeatherServlet</servlet-name>
    <url-pattern>/weatherServlet</url-pattern>
  </servlet-mapping>

</web-app>

相關文章
相關標籤/搜索