Servlet 后台获取XML

D

package net.nw.servlet;
import java.io.IOException;
import java.io.PrintWriter; import javassist.expr.NewArray; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import net.nw.dao.Conn;
import net.nw.dao.ModelcodesDAO; import java.sql.*;
import java.util.ArrayList;
import java.util.List; public class DateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private ModelcodesDAO mcd = new ModelcodesDAO();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String where = null;
String currdate= request.getParameter("currdate");
String projectname=request.getParameter("projectname");
String codeid = request.getParameter("modelname");
String mname = mcd.getNameById(Integer.parseInt(codeid));
where="where ProjectName like '%"+projectname+"%' AND ModelCode like '%"+mname+"%'"; StringBuffer xmlDOM = new StringBuffer();
ResultSet rs=null;
rs = this.getResultSet(where);
xmlDOM.append("<root>");
try {
xmlDOM.append("<defects>");
while (rs.next()) {
xmlDOM.append("<defectmonth>");
xmlDOM.append("<Day1>" + rs.getString("Day1") + "</Day1>");
xmlDOM.append("<Day2>" + rs.getString("Day2") + "</Day2>");
xmlDOM.append("</defectmonth>");
request.setAttribute("daay1", rs.getString("Day1"));
System.out.println(request.getAttribute("daay1"));
}
rs.close();
xmlDOM.append("</defects>");
} catch (SQLException e) {
System.out.println(e.getMessage());
} xmlDOM.append("</root>");
this.print(request, response, xmlDOM.toString());
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
} private void print(HttpServletRequest request, HttpServletResponse response,String xmlDOM) throws IOException{
response.setCharacterEncoding("utf-8");
response.setContentType("text/xml");
PrintWriter out = response.getWriter();
out.print(xmlDOM);
out.close();
} private ResultSet getResultSet(String where){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConn();
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} catch (SQLException e) {
System.out.println(e.getMessage());
} String sql = "select * from defects_infor_daily "+where;
System.out.println(sql);
//String sql = "select top "+len+" * from person where pid >= ? order by pid"; try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return rs;
} private Connection getConn() throws ClassNotFoundException, SQLException{ String driver = "com.mysql.jdbc.Driver";
String url="jdbc:mysql://109.110.100.56/tsdr_project";
String user="root";
String password="";
Class.forName(driver);
System.out.println("��ݿ�����");
return DriverManager.getConnection(url,user,password); } }

前台获取

  var js = {
XMLHttp:null,
//发送请求函数
sendRequest:function(url,responseFun,callback){
//创建XMLHTTPRequest对象
(function(){
//根据浏览器类型创建XMLHTTPRequest对象
if(window.XMLHttpRequest){
js.XMLHttp = new XMLHttpRequest();
}
else{
try{
js.XMLHttp = new ActionXObject("Msxml2.XMLHTTP");
}catch (e){
  try{
js.XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e0){alert("Microsoft"+e0);}
}
} })(); js.XMLHttp.open("POST", url, true);
js.XMLHttp.onreadystatechange = function(responseFunction){
  if(js.XMLHttp.readyState == 4){
if(js.XMLHttp.status == 200){
responseFun(js.XMLHttp);
}else{
document.getElementById("canvasDivZhe").innerHTML = "<font color='red'>连接服务器异常...</font>" ;
}
  }
  else{
//document.getElementById("div").innerHTML = "<font color='red'>数据加载中...</font>" ;
  }
};//指定响应函数
js.XMLHttp.send(null);
return js.XMLHttp;
}
  };   //响应函数
  function responseFunction(xmlhttp){
var xmlDOM = xmlhttp.responseXML; //接受服务器返回的xml文档
//document.getElementById("div").innerHTML =xmlhttp.responseText;
//alert(xmlhttp.responseText);
parse(xmlDOM);//解析XML文档
  } //解析XML文档
  function parse(xmlDOM){
//alert(xmlDOM);
//var da=#request.daay1;
var person = xmlDOM.getElementsByTagName("defectmonth");
var day1=person[0].getElementsByTagName("Day1")[0].firstChild.data;
alert(day1); }//function end
//主调函数,以当前页作为参数
  function AjaxTest(currdate,projectname,modelname) {
var projectname="<%=session.getAttribute("projectname_session")%>";
var modelname= document.getElementById("codeid").value;
//alert(currdate+ " " +projectname+" "+modelname);
js.sendRequest("DateServlet?currdate="+currdate+"&projectname="+projectname+"&modelname="+modelname,responseFunction,null);
  }
上一篇:干货:Java多线程详解(内附源码)


下一篇:javascript call()与apply()