package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.sql.*;
import cn.jansen.bean.Weather;
import cn.jansen.service.*;
import javax.naming.*;
import java.net.URLDecoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WeatherServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public WeatherServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String sort=request.getParameter("sort");
sort=URLDecoder.decode(sort,"utf-8");
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
props.setProperty("java.naming.provider.url", "localhost");
InitialContext ctx;
try {
ctx = new InitialContext(props);
QueryWeather queryWeather = (QueryWeather) ctx.lookup("QueryWeatherBean/remote");
List list1 =queryWeather.getCity(sort);
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write(
"<?xml version='1.0' encoding='UTF-8' ?>");//重要!!若无,则不能拿到XML元素
out.println("<response>");
for(int i=0;i<list1.size();i++)
{
out.println("<res>" + list1.get(i).toString() + "</res>");
}
out.println("</response>");
} catch (NamingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
this.doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}