package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.DataSupport;
public class MyServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -978202501310494110L;
/**
* Constructor of the object.
*/
public MyServlet() {
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 {
response.setContentType("text/xml");
response.setCharacterEncoding("utf-8");
String id = request.getParameter("id");//查询的数据
String table = request.getParameter("table");//查询的表
String column = request.getParameter("column");//查询的列
DataSupport data = new DataSupport();
ResultSet set = data.findData("select id,name from "+table+" where "+column+"="+id);
if(id==null || column==null || id.equals("") || column.equals("")){
set = data.findData("select id,name from "+table);//为province添加下拉时,没有id,column
//System.out.println("!!!!");
}
PrintWriter out = response.getWriter();
out.println("<root>");
out.println("<group>");
try {
while(set.next()){
out.println("<id>"+set.getString(1)+"</id>");
out.println("<name>"+set.getString(2)+"</name>");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
out.println("<id>1</id>");
out.println("<name>暂无数据</name>");
}
out.println("</group>");
out.println("</root>");
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 {
this.doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}