一、Ajax功能模块:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.lang.*,computer.*"%>
<html>
<head>
<title>计算机设备管理</title>
<%
GetData gd=new GetData();
%>
<style>
<script language=javascript>
//Ajax功能模块
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function createQueryString() {
var equipid = document.getElementById("MTEquip").value;
var queryString = "PageID=Maintain&EquipID=" + equipid;
return queryString;
}
function doAjax() {
createXMLHttpRequest();
var queryString = "servlet/AjaxTest?";
queryString = queryString + createQueryString() + "&timeStamp="
+ new Date().getTime();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", queryString, true);
xmlHttp.send(null);
}
function handleStateChange() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
parseResults();
}
}
}
function parseResults() {
document.getElementById("resultOK").innerHTML=xmlHttp.responseText;
}
</script>
</head>
<form name="form1" action="SaveData.jsp">
<table border="0" cellspacing="1" cellpadding="2" width="97%" align="center" bgcolor="#F6F6F6">
<tr><td colspan="5"><div align="center"><img src="images/text01.jpg" align="center"></div></td></tr>
<tr><td colspan="5">
<hr>
</td></tr>
<tr>
<td width="15%"><div align="right">设备编号</div></td>
<td width="20%"><input name="MTEquip" id="MTEquip"></td>
<td width="15%"><input type="button" value="设备资料" οnclick="doAjax();"></td>
<td width="15%"><div align="right">维修原因</div></td>
<td width="35%"><input name='Trouble' type='text'></td>
</tr>
<tr>
<td><div align="right">维修结果</div></td>
<td><input name='Result' type='text'></td>
<td></td>
<td><div align="right">维修费用</div></td>
<td><input name='MMoney' type='text'></td>
</tr>
<tr>
<td><div align="right">维修日期</div></td>
<td><input name='MDate' type='text'></td>
<td><div align="right">维修人</div></td>
<td><input name='Server' id='Server'></td>
</tr>
<tr>
<td colspan="5"><div align="center">
<input name="btnDo" type="submit" value="数据提交">
<input name="btnReset" type="reset" value="重新输入">
</div></td></tr>
<tr><td colspan="5">
<hr>
</td></tr>
<tr><td colspan="5" align="center">
<div align="center" id="resultOK"></div>
</td></tr>
</table>
<input name="Page_Mark" type="hidden" value="Maintain">
</form>
</html>
二、Serlet(AjaxTest)代码:
package computer;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AjaxTest extends HttpServlet {
/**
* Constructor of the object.
*/
public AjaxTest() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response, String method)
throws ServletException, IOException {
// 设置返回内容的类型
response.setContentType("text/html; charset=UTF-8");
// 获取用户传过来的参数
String pageid = request.getParameter("PageID");
String equipid = request.getParameter("EquipID");
String responseStr="";
// 将服务器的回应写回浏览器
PrintWriter out = response.getWriter();
responseStr=GetEquipData(pageid,equipid);
out.println(responseStr);
out.close();
}
/**
* 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 {
processRequest(request, response, "GET");
}
/**
* 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 {
processRequest(request, response, "POST");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
/*
* 获得数据库数据
*/
public String GetEquipData(String pid,String epid) {
String str = "";
GetData gd=new GetData();
str="select * from Equip_View where EquipID='"+epid+"'";
String aa=gd.GetEquipData(str);
str="select * from EquipMaintain_View where EquipID='"+epid+"'";
String bb=gd.GetMaintainData(str);
aa += "<tr><td colspan=/"4/"><hr></td></tr>";
bb += "<tr><td colspan=/"4/"><hr></td></tr>";
str=aa+bb;
return str;
}