Ajax应用实例

 

我顶 字号:

一、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="数据提交">&nbsp;&nbsp;&nbsp;&nbsp;
          <input name="btnReset" type="reset" value="重新输入">&nbsp;&nbsp;&nbsp;&nbsp;
    </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;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值