資源簡介
該代碼使用的api接口為:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?theUserId=&theCityCode=792,可以預測未來5天的粗略天氣(包括今天)。
適合初學的朋友使用。界面很丑。
代碼片段和文件信息
package?com.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?com.weather.WeatherUtil;
public?class?WeatherServlet?extends?HttpServlet?{
/**
?*?Constructor?of?the?object.
?*/
public?WeatherServlet()?{
super();
}
/**
?*?Destruction?of?the?servlet.?
?*/
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?{
response.setContentType(“text/html“);
doPost(request?response);
}
public?void?doPost(HttpServletRequest?request?HttpServletResponse?response)
throws?ServletException?IOException?{
???request.setCharacterEncoding(“UTF-8“);
response.setContentType(“text/html;charset=utf-8“);
response.setCharacterEncoding(“utf-8“);
String?provinceStr?=?request.getParameter(“provinceCode“);
String??cityStr?=request.getParameter(“cityCode“);
System.out.println(provinceStr?+?“?“?+?cityStr);
WeatherUtil?db?=new?WeatherUtil();
int?provinceCode?=db.getProinceCode(provinceStr);
System.out.println(provinceCode);
int?cityCode?=?db.getCityCode(provinceCode?cityStr);
System.out.println(cityCode?);
List?list=db.getWeather(cityCode);
response.setContentType(“text/html“);
PrintWriter?out?=?response.getWriter();
out.println(““);
out.println(““);
out.println(“??tle>A?Servlet tle>“);
out.println(“??“);
for(String?s:list){
out.print(s);
out.print(““);
}
????????out.println(“??“);
out.println(““);
out.flush();
out.close();
}
/**
?*?Initialization?of?the?servlet.?
?*
?*?@throws?ServletException?if?an?error?occurs
?*/
public?void?init()?throws?ServletException?{
//?Put?your?code?here
}
}
評論
共有 條評論