1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>    
  2. <%    
  3.     String path = request.getContextPath();    
  4.     String basePath = request.getScheme() + "://"    
  5.             + request.getServerName() + ":" + request.getServerPort()    
  6.             + path + "/";    
  7. %>    

  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">    
  9. <html>    
  10.     <head>    
  11.         <base href="<%=basePath%>">    

  12.         <title>My JSP 'emp.jsp' starting page</title>    

  13.         <meta http-equiv="pragma" content="no-cache">    
  14.         <meta http-equiv="cache-control" content="no-cache">    
  15.         <meta http-equiv="expires" content="0">    
  16.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    
  17.         <meta http-equiv="description" content="This is my page">    
  18.         <!--    
  19.     <link rel="stylesheet" type="text/css" href="styles.css">    
  20.     -->    

  21.         <script type="text/javascript" src="./js/util.js">    
  22. </script>    
  23.         <script>    
  24. window.onload = function() {    
  25.     document.getElementById("addemp").style.display = "none";    
  26. }    
  27. var pageinfo;    

  28. function findAll(nowpage) {    
  29.     //获取xhr对象    
  30.     var xhr = getXhr();    
  31.     //封装请求信息    
  32.     xhr.open("get", "./listEmp.do?nowpage=" + nowpage, true);    

  33.     //发送请求    
  34.     xhr.send();    

  35.     //相应    
  36.     xhr.onreadystatechange = function() {    
  37.         if (xhr.readyState == 4 && xhr.status == 200) {    

  38.             var jsonResponse = eval("(" + xhr.responseText + ")");    

  39.             //获取写入的tbody的DOM对象    
  40.             cha(jsonResponse);    

  41.         }    
  42.     }    
  43. }    

  44. function cha(jsonResponse) {    
  45.     var listemps = $("listemps");    
  46.     listemps.innerHTML = "";    
  47.     for ( var i = 0; i < jsonResponse.length; i++) {    
  48.         var emptr = document.createElement("tr");    

  49.         var empno = document.createElement("td");    
  50.         var ename = document.createElement("td");    
  51.         var job = document.createElement("td");    
  52.         var hiredate = document.createElement("td");    
  53.         var mgr = document.createElement("td");    
  54.         var sal = document.createElement("td");    
  55.         var comm = document.createElement("td");    
  56.         var deptno = document.createElement("td");    
  57.         var cz = document.createElement("td");    
  58.         empno.appendChild(document.createTextNode(jsonResponse[i].empno));    
  59.         ename.appendChild(document.createTextNode(jsonResponse[i].ename));    
  60.         job.appendChild(document.createTextNode(jsonResponse[i].job));    
  61.         var a = (jsonResponse[i].hiredate.year + 1900) + "年"    
  62.                 + (jsonResponse[i].hiredate.month + 1) + "月"    
  63.                 + jsonResponse[i].hiredate.date + "日";    
  64.         hiredate.appendChild(document.createTextNode(a));    
  65.         mgr.appendChild(document.createTextNode(jsonResponse[i].mgr));    
  66.         sal.appendChild(document.createTextNode(jsonResponse[i].sal));    
  67.         comm.appendChild(document.createTextNode(jsonResponse[i].comm));    
  68.         deptno.appendChild(document.createTextNode(jsonResponse[i].deptno));    
  69.         var udmp = document.createElement("input");    
  70.         var delmp = document.createElement("input");    
  71.         delmp.setAttribute("type", "button");    
  72.         udmp.setAttribute("type", "button");    
  73.         delmp.setAttribute("value", "删除");    
  74.         udmp.setAttribute("value", "编辑");    

  75.         emptr.appendChild(empno);    
  76.         emptr.appendChild(ename);    
  77.         emptr.appendChild(job);    
  78.         emptr.appendChild(hiredate);    
  79.         emptr.appendChild(mgr);    
  80.         emptr.appendChild(sal);    
  81.         emptr.appendChild(comm);    
  82.         emptr.appendChild(deptno);    
  83.         cz.appendChild(udmp);    
  84.         cz.appendChild(delmp);    
  85.         emptr.appendChild(cz);    
  86.         listemps.appendChild(emptr);    

  87.         delmp.onclick = function() {    
  88.             if (window.confirm("您确定删除??")) {    
  89.                 var empno = this.parentNode.parentNode.firstChild.innerHTML;    
  90.                 listemps.innerHTML = "";    
  91.                 var xhr = getXhr();    
  92.                 //封装请求信息    
  93.                 xhr.open("get", "./DelEmp.do?empno=" + empno, true);    

  94.                 //发送请求    
  95.                 xhr.send();    

  96.                 //相应    
  97.                 xhr.onreadystatechange = function() {    
  98.                     if (xhr.readyState == 4 && xhr.status == 200) {    
  99.                         var jsonResponse = eval("(" + xhr.responseText + ")");    

  100.                         //获取写入的tbody的DOM对象    
  101.                         cha(jsonResponse);    
  102.                     }    
  103.                 }    
  104.             }    

  105.         }    

  106.         udmp.onclick = function() {    
  107.             document.getElementById("addemp").style.display = "block";    
  108.             document.getElementById("a").style.display = "none";    
  109.             document.getElementById("b").style.display = "block";    
  110.             var empno = this.parentNode.parentNode.firstChild.innerHTML;    
  111.             var xhr = getXhr();    
  112.             //封装请求信息    
  113.             xhr.open("get", "./DeptServlet.do?empno=" + empno, true);    

  114.             //发送请求    
  115.             xhr.send();    
  116.             xhr.onreadystatechange = function() {    
  117.                 if (xhr.readyState == 4 && xhr.status == 200) {    

  118.                     var jsonResponse1 = eval("(" + xhr.responseText + ")");    

  119.                     document.getElementById("empno").value = jsonResponse1.empno;    

  120.                     document.getElementById("ename").value = jsonResponse1.ename;    

  121.                     document.getElementById("job").value = jsonResponse1.job;    

  122.                     var a = (jsonResponse1.hiredate.year + 1900) + "-"    
  123.                             + (jsonResponse1.hiredate.month + 1) + "-"    
  124.                             + jsonResponse1.hiredate.date;    

  125.                     document.getElementById("hiredate").value = a;    

  126.                     document.getElementById("mgr").value = jsonResponse1.mgr;    

  127.                     document.getElementById("sal").value = jsonResponse1.sal;    

  128.                     document.getElementById("comm").value = jsonResponse1.comm;    

  129.                     select(xhr, jsonResponse[i].deptno);    
  130.                 }    
  131.             }    
  132.         }    

  133.         pageinfo = jsonResponse[jsonResponse.length - 1];    

  134.     }    
  135. }    

  136. function update() {    
  137.     if (window.confirm("您确定修改??")) {    
  138.         listemps.innerHTML = "";    
  139.         var xhr = getXhr()    
  140.         var empno = document.getElementById("empno").value;    
  141.         var ename = document.getElementById("ename").value;    
  142.         var job = document.getElementById("job").value;    
  143.         var hiredate = document.getElementById("hiredate").value;    
  144.         var mgr = document.getElementById("mgr").value;    
  145.         var sal = document.getElementById("sal").value;    
  146.         var comm = document.getElementById("comm").value;    
  147.         var deptno = document.getElementById("sel").value;    
  148.         xhr    
  149.                 .open("get", "./UpdateEmp.do?empno1=" + empno + "&ename1="    
  150.                         + ename + "&job1=" + job + "&hiredate1=" + hiredate    
  151.                         + "&mgr1=" + mgr + "&sal1=" + sal + "&comm=" + comm    
  152.                         + "&deptno1=" + deptno, true);    

  153.         xhr.send();    
  154.         xhr.onreadystatechange = function() {    
  155.             if (xhr.readyState == 4 && xhr.status == 200) {    
  156.                 cha();    
  157.             }    
  158.         }    
  159.     }    
  160. }    

  161. function tijiao() {    

  162.     var xhr = getXhr()    
  163.     var empno = document.getElementById("empno").value;    
  164.     var ename = document.getElementById("ename").value;    
  165.     var job = document.getElementById("job").value;    
  166.     var hiredate = document.getElementById("hiredate").value;    
  167.     var mgr = document.getElementById("mgr").value;    
  168.     var sal = document.getElementById("sal").value;    
  169.     var comm = document.getElementById("comm").value;    
  170.     var deptno = document.getElementById("sel").value;    

  171.     xhr.open("get", "./AddEmp.do?empno1=" + empno + "&ename1=" + ename    
  172.             + "&job1=" + job + "&hiredate1=" + hiredate + "&mgr1=" + mgr    
  173.             + "&sal1=" + sal + "&comm=" + comm + "&deptno1=" + deptno, true);    
  174.     xhr.send();    
  175.     xhr.onreadystatechange = function() {    
  176.         if (xhr.readyState == 4 && xhr.status == 200) {    
  177.             findAll(1);    
  178.         }    
  179.     }    

  180. }    
  181. function addEmp() {    

  182.     document.getElementById("addemp").style.display = "block";    
  183.     document.getElementById("b").style.display = "none";    
  184.     document.getElementById("a").style.display = "block";    
  185.     var sel = document.getElementById("sel");    
  186.     var xhr = getXhr()    
  187.     select(xhr, "")    

  188. }    
  189. function select(xhr, vaule) {    

  190.     xhr.open("get", "./Dept.do", true);    
  191.     xhr.send();    

  192.     xhr.onreadystatechange = function() {    
  193.         if (xhr.readyState == 4 && xhr.status == 200) {    

  194.             var jsonResponse = eval("(" + xhr.responseText + ")");    
  195.             c = jsonResponse;    
  196.             for ( var i = 0; i < jsonResponse.length; i++) {    
  197.                 var emptr = document.createElement("option");    
  198.                 if (vaule == jsonResponse[i].deptno) {    
  199.                     emptr.appendChild(document    
  200.                             .createTextNode(jsonResponse[i].dname));    
  201.                     emptr.setAttribute("value", jsonResponse[i].deptno);    
  202.                     emptr.setAttribute("selected", true);    
  203.                 } else {    
  204.                     emptr.appendChild(document    
  205.                             .createTextNode(jsonResponse[i].dname));    
  206.                     emptr.setAttribute("value", jsonResponse[i].deptno);    
  207.                 }    
  208.                 sel.appendChild(emptr);    
  209.             }    
  210.         }    
  211.     }    

  212. }    
  213. function getpageInfo(oper) {    

  214.     switch (oper) {    
  215.     case 1:    
  216.         pageinfo.nowpage = 1;    
  217.         break;    
  218.     case 2:    
  219.         pageinfo.nowpage--;    
  220.         break;    
  221.     case 3:    
  222.         pageinfo.nowpage++;    
  223.         break;    
  224.     case 4:    
  225.         pageinfo.nowpage = pageinfo.countpage;    
  226.         break;    
  227.     }    

  228.     findAll(pageinfo.nowpage);    

  229. }    

  230. function changeTest() {    

  231.     if ($("ordername").className == "asc") {    
  232.         $("ordername").className = "desc";    

  233.         //获取xhr对象    
  234.         var xhr = getXhr();    
  235.         //封装请求信息    
  236.         xhr.open("get", "./listEmp.do?nowpage=" + nowpage+"&lob=asc", true);    

  237.         //发送请求    
  238.         xhr.send();    

  239.         //相应    
  240.         xhr.onreadystatechange = function() {    
  241.             if (xhr.readyState == 4 && xhr.status == 200) {    

  242.                 var jsonResponse = eval("(" + xhr.responseText + ")");    

  243.                 //获取写入的tbody的DOM对象    
  244.                 cha(jsonResponse);    

  245.             }    
  246.         }    

  247.     } else {    
  248.         $("ordername").className = "asc";    

  249.     }    

  250. }    
  251. </script>    

  252.         <style>    
  253. .asc {    
  254.     background-p_w_picpath: url(p_w_picpaths/asc.gif);    
  255.     background-repeat: no-repeat;    
  256. }    

  257. .desc {    
  258.     background-p_w_picpath: url(p_w_picpaths/desc.gif);    
  259.     background-repeat: no-repeat;    
  260. }    
  261. </style>    
  262.     </head>    

  263.     <body>    
  264.         <div>    
  265.             <h1>    
  266.                 显示所有的员工信息    
  267.             </h1>    

  268.             <input type="button" value="采用JSON返回数据显示所有的员工信息" onclick="findAll(1)" />    
  269.             <input type="button" value="添加用户" onclick="addEmp()" />    
  270.             <div>    
  271.                 <table border="1px" cellpadding="0" cellspacing="0">    

  272.                     <caption>    
  273.                         员工信息表    
  274.                     </caption>    
  275.                     <thead>    
  276.                         <tr>    
  277.                             <th>    
  278.                                 编号    
  279.                             </th>    
  280.                             <th class="asc" id="ordername" onclick="changeTest()"    
  281.                                 width="55px">    
  282.                                 姓名    
  283.                             </th>    
  284.                             <th>    
  285.                                 岗位    
  286.                             </th>    
  287.                             <th>    
  288.                                 入职时间    
  289.                             </th>    
  290.                             <th>    
  291.                                 领导    
  292.                             </th>    
  293.                             <th>    
  294.                                 薪水    
  295.                             </th>    
  296.                             <th>    
  297.                                 奖金    
  298.                             </th>    
  299.                             <th>    
  300.                                 部门    
  301.                             </th>    
  302.                             <th>    
  303.                                 操作    
  304.                             </th>    
  305.                         </tr>    
  306.                     </thead>    
  307.                     <tbody id="listemps">    

  308.                     </tbody>    
  309.                 </table>    
  310.             </div>    
  311.             <div>    
  312.                 <a href="javascript:getpageInfo(1)">首页</a>    
  313.                 <a href="javascript:getpageInfo(2)">上一页</a>    
  314.                 <a href="javascript:getpageInfo(3)">下一页</a>    
  315.                 <a href="javascript:getpageInfo(4)">末页</a>    
  316.             </div>    
  317.             <div id="addemp">    
  318.                 <table width="410" height="198" border="1" align="center">    
  319.                     <tr>    
  320.                         <td width="135" align="right">    
  321.                             编号    
  322.                         </td>    
  323.                         <td width="259">    
  324.                             <label>    
  325.                                 <input type="text" name="empno" id="empno" />    
  326.                             </label>    
  327.                         </td>    
  328.                     </tr>    
  329.                     <tr>    
  330.                         <td align="right">    
  331.                             姓名    
  332.                         </td>    
  333.                         <td>    
  334.                             <input type="text" name="ename" id="ename" />    
  335.                         </td>    
  336.                     </tr>    
  337.                     <tr>    
  338.                         <td align="right">    
  339.                             岗位    
  340.                         </td>    
  341.                         <td>    
  342.                             <input type="text" name="job" id="job" />    
  343.                         </td>    
  344.                     </tr>    
  345.                     <tr>    
  346.                         <td align="right">    
  347.                             入职时间    
  348.                         </td>    
  349.                         <td>    
  350.                             <input type="text" name="hiredate" id="hiredate" />    
  351.                         </td>    
  352.                     </tr>    
  353.                     <tr>    
  354.                         <td align="right">    
  355.                             领导    
  356.                         </td>    
  357.                         <td>    
  358.                             <input type="text" name="mgr" id="mgr" />    
  359.                         </td>    
  360.                     </tr>    
  361.                     <tr>    
  362.                         <td height="25" align="right">    
  363.                             薪水    
  364.                         </td>    
  365.                         <td>    
  366.                             <input type="text" name="sal" id="sal" />    
  367.                         </td>    
  368.                     </tr>    
  369.                     <tr>    
  370.                         <td height="11" align="right">    
  371.                             奖金    
  372.                         </td>    
  373.                         <td>    
  374.                             <input type="text" name="comm" id="comm" />    
  375.                         </td>    
  376.                     </tr>    
  377.                     <tr>    
  378.                         <td height="12" align="right">    
  379.                             部门    
  380.                         </td>    
  381.                         <td>    
  382.                             <select id="sel">    
  383.                                 <option value="10">    
  384.                                     请选择部门    
  385.                                 </option>    

  386.                             </select>    


  387.                         </td>    
  388.                     </tr>    
  389.                     <tr>    
  390.                         <td height="12" colspan="2" align="center">    
  391.                             <input type="button" name="a" id="a" value="提交"    
  392.                                 onclick="tijiao()" />    
  393.                             <input type="button" name="b" id="b" value="修改"    
  394.                                 onclick="update()" />    

  395.                         </td>    
  396.                     </tr>    
  397.                 </table>    
  398.             </div>    
  399.         </div>    
  400.     </body>    
  401. </html>    

只有JSP的javascript中的代码, 其他代码跟以前连接数据库时的代码是一样的,另外代码可能有一些BUG 望能提一下