6月15日学习日志
今天完成了web实验四综合应用设计。
文件代码如下:
<html> <head> <title>学生管理系统</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="css/style.css"> <link rel="stylesheet" type="text/css" href="css/jquery-ui.css"> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="js/jquery-ui.js"></script> <script type="text/javascript"> //通过AJAX方式检索所有的学生信息 function RetrieveStuednts() { $.post("list.action", {}, function(data) { $("#StudentsTable tr:gt(0)").remove(); for (var i = 0; i < data.length; i++) { //插入表行 var trHtml = "<tr id = "+data[i].id +"><td>" + data[i].id + "</td><td>" + data[i].name + "</td><td>" + data[i].sex + "</td><td>" + data[i].birth + "</td><td>" + data[i].address + "</td><td><a href=\"#\" class=\"updateLink\">修改</a> <a href=\"#\" class=\"deleteLink\">删除</a></td></tr>"; $("#StudentsTable").append(trHtml); } //绑定修改链接 $(".updateLink").click(function() { $.post("edit.action", {id:$(this).closest("tr").attr("id")}, function(data){ $("#UpdateId").val(data.id); $("#UpdateName").val(data.name); $("#UpdateSex").val(data.sex); $("#UpdateBirth").val(data.birth); $("#UpdateAddress").val(data.address); $("#UpdateDiv").dialog("open"); }, "json"); }); //绑定删除链接 $(".deleteLink").click(function(){ $.post("delete.action", {id:$(this).closest("tr").attr("id")}, function(data){ if (data=="1") { RetrieveStuednts(); } else { alert("删除学生信息失败!"); } }, "json"); }); }, "json"); } $(function() { //设定Ajax提交编码格式为utf-8 $.ajaxSetup({ contentType: "application/x-www-form-urlencoded; charset=utf-8" }); //对“添加学生信息”窗口进行初始化 $("#AddDiv").dialog({ title: "添加学生信息", autoOpen : false, height : 280, width : 400, modal : true, show: "blind", hide: "fade", close : function(){ $("#AddId").val(""); $("#AddName").val(""); $("#AddSex").val(""); $("#AddBirth").val(""); $("#AddAddress").val(""); } }); //对“修改学生信息”窗口进行初始化 $("#UpdateDiv").dialog({ title: "修改学生信息", autoOpen : false, height : 280, width : 400, modal : true, show: "blind", hide: "fade", close : function(){ $("#UpdateId").val(""); $("#UpdateName").val(""); $("#UpdateSex").val(""); $("#UpdateBirth").val(""); $("#UpdateAddress").val(""); } }); //对添加学生信息窗口的添加键绑定事件驱动程序 $("#AddSubmit").click(function(){ //提交服务器 $.post("add.action", {id:$("#AddId").val(),name:$("#AddName").val(),sex:$("#AddSex").val(), birth:$("#AddBirth").val(), address:$("#AddAddress").val()}, function(data){ if (data=="1") { $("#AddDiv").dialog("close"); RetrieveBooks(); } else { $("#AddTip").html("添加学生信息失败!请重新输入数据。"); $("#AddTip").show().delay(5000).hide(0); } }, "json"); }); //对添加学生信息窗口的添加键绑定事件驱动程序 $("#UpdateSubmit").click(function(){ //提交服务器 $.post("update.action", {id:$("#UpdateId").val(),name:$("#UpdateName").val(), sex:$("#UpdateSex").val(),birth:$("#UpdateBirth").val(), address:$("#UpdateAddress").val()}, function(data){ if (data=="1") { $("#UpdateDiv").dialog("close"); RetrieveBooks(); } else { $("#UpdateTip").html("更新学生信息失败!请重新输入数据。"); $("#UpdateTip").show().delay(5000).hide(0); } }, "json"); }); //对“新增学生信息”链接绑定事件驱动程序 $("#AddButton").click(function() { $("#AddDiv").dialog("open"); }); //第一次加载检索所有学生信息 RetrieveStuednts(); }); </script> </head> <body> <script type="text/javascript"> function checkform(form) { if(form.id.value=="") { alert("学号不能为空"); form.name.focus(); return false; } if(form.name.value=="") { alert("姓名不能为空"); form.name.focus(); return false; } if(form.sex.value=="") { alert("性别不能为空"); form.name.focus(); return false; } if(form.birth.value=="") { alert("出生年月不能为空"); form.name.focus(); return false; } if(form.address.value=="") { alert("住址不能为空"); form.name.focus(); return false; } return true; } </script> <h1>学生管理系统</h1> <a id="AddButton" href="#">增加学生信息</a> <table style="width: 50%" id="StudentsTable"> <tr> <th>学号</th> <th>姓名</th> <th>性别</th> <th>出生年月</th> <th>住址</th> <th>管理</th> </tr> </table> <div id="AddDiv" style="display: hidden" onsubmit="return checkform(this);"> <form id="AddForm"> <table style="width: 350px;" id="AddTable"> <tr> <th width="30%">学号:</th> <td width="70%" class="ltd"><input name="id" type="text" id="AddId"></td> </tr> <tr> <th>姓名:</th> <td class="ltd"><input name="name" type="text" id="AddName"></td> </tr> <tr> <th>性别:</th> <td class="ltd"><input name="sex" type="text" id="AddSex">元</td> </tr> <tr> <th>出生年月:</th> <td class="ltd"><input name="birth" type="text" id="AddBirth">元</td> </tr> <tr> <th>住址:</th> <td class="ltd"><input name="address" type="text" id="AddAddress">元</td> </tr> <tr> <th colspan="2"><input type="button" value="添加" id ="AddSubmit"> <input type="reset" value="重置"></th> </tr> </table> </form> <span style="color:red;" id="AddTip"></span> </div> <div id="UpdateDiv" style="display: hidden" onsubmit="return checkform(this);"> <form id="UpdateForm"> <table style="width: 350px;" id="UpdateTable"> <tr> <th width="30%">学号:</th> <td width="70%" class="ltd"><input name="id" type="text" id="UpdateId"></td> </tr> <tr> <th>姓名:</th> <td class="ltd"><input name="name" type="text" id="UpdateName"></td> </tr> <tr> <th>性别:</th> <td class="ltd"><input name="sex" type="text" id="UpdateSex"></td> </tr> <tr> <th>出生年月:</th> <td class="ltd"><input name="birth" type="text" id="UpdateBirth"></td> </tr> <tr> <th>住址:</th> <td class="ltd"><input name="address" type="text" id="UpdateAddress"></td> </tr> <tr> <th colspan="2"><input type="button" value="修改" id ="UpdateSubmit"> <input type="reset" value="重置"></th> </tr> </table> </form> <span style="color:red;" id="UpdateTip"></span> </div> <br /> <hr /> <div style="text-align: center; width: 100%; font-size: 12px; color: #333;"> ©版权所有:石家庄铁道大学信息科学与技术学院 <a href="Lab04-2.png" target="_blank">网站地图</a> </div> </body> </html>
<%@ page pageEncoding="utf-8" import="java.util.ArrayList,student.bean.StudentInfo"%> <html> <head> <title>学生管理系统</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <h1>学生管理系统</h1> <a href="addview.do">增加学生信息</a> <p /> <table style="width: 50%"> <tr> <th>学号</th> <th>姓名</th> <th>性别</th> <th>出生年月</th> <th>住址</th> <th>管理</th> </tr> <% @SuppressWarnings("unchecked") ArrayList<StudentInfo> list = (ArrayList<StudentInfo>) request.getAttribute("list"); for (StudentInfo si : list) { String id = si.getId(); %> <tr> <td><%=si.getId()%></td> <td><%=si.getName()%></td> <td><%=si.getSex()%></td> <td><%=si.getBirth()%></td> <td><%=si.getAddress()%></td> <td><a href="edit.do?id=<%=id%>">修改</a> <a href="delete.do?id=<%=id%>">删除</a></td> </tr> <% } %> </table> <br /> <hr /> <div style="text-align: center; width: 100%; font-size: 12px; color: #333;"> ©版权所有:石家庄铁道大学信息科学与技术学院 <a href="Lab04-1.png" target="_blank">网站地图</a> </div> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="css/style.css"> <title>操作成功提示</title> </head> <body> <p style="color: #CC1111">操作成功!</p> <a href="index.do">转到主页</a> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="css/style.css"> <title>操作失败提示</title> </head> <body> 操作失败! <a href="javascript:history.back()">返回</a> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="css/style.css"> <title>添加学生信息</title> </head> <body> <script type="text/javascript"> function checkform(form) { if(form.id.value=="") { alert("学号不能为空"); form.name.focus(); return false; } if(form.name.value=="") { alert("姓名不能为空"); form.name.focus(); return false; } if(form.sex.value=="") { alert("性别不能为空"); form.name.focus(); return false; } if(form.birth.value=="") { alert("出生年月不能为空"); form.name.focus(); return false; } if(form.address.value=="") { alert("住址不能为空"); form.name.focus(); return false; } return true; } </script> <form action="add.do" method="post" onsubmit="return checkform(this);"> <table width="50%" border="1" align="center"> <caption>添加学生信息</caption> <tr> <th width="30%">学号:</th> <td width="70%"><input name="id" type="text"></td> </tr> <tr> <th>姓名:</th> <td><input name="name" type="text"></td> </tr> <tr> <th>性别:</th> <td><input name="sex" type="text">元</td> </tr> <tr> <th>出生年月:</th> <td><input name="birth" type="text">元</td> </tr> <tr> <th>住址:</th> <td><input name="address" type="text">元</td> </tr> <tr> <th colspan="2"><input type="submit" value="添加"> <input type="reset" value="重置"></th> </tr> </table> </form> </body> </html>
<%@ page import="student.bean.StudentInfo" pageEncoding="utf-8"%> <html> <head> <title>修改学生信息</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <% StudentInfo bi = (StudentInfo) request.getAttribute("bi"); %> <form action="update.do" method="post"> <table style="width: 50%"> <caption>修改学生信息</caption> <tr> <th width="30%">学号:</th> <td width="70%"><input name="id" type="hidden" value="<%=bi.getId()%>"></td> </tr> <tr> <th>姓名:</th> <td><input name="name" type="text" value="<%=bi.getName()%>"></td> </tr> <tr> <th>性别:</th> <td><input name="sex" type="text" value="<%=bi.getSex()%>"></td> </tr> <tr> <th>出生年月:</th> <td><input name="birth" type="text" value="<%=bi.getBirth()%>"></td> </tr> <tr> <th>住址:</th> <td><input name="address" type="text" value="<%=bi.getAddress()%>"></td> </tr> <tr> <th colspan="2"><input type="submit" value="修改"> <input type="reset" value="重置"></th> </tr> </table> </form> </body> </html>
package student.bean; import java.sql.*; import java.util.*; public class StudentInfo { private String id; private String name; private String sex; private String birth; private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getBirth() { return birth; } public void setBirth(String birth) { this.birth = birth; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } /** * 从StudentInfo表中获取所有的图书信息 * * @return StudentInfo的数组 */ public static ArrayList<StudentInfo> getStudentList() { ArrayList<StudentInfo> list = new ArrayList<StudentInfo>(); String sql = "select * from studentinfo"; DBBean jdbc = new DBBean(); ResultSet rs = jdbc.executeQuery(sql); try { while (rs.next()) { StudentInfo bi = new StudentInfo(); bi.setId(rs.getString("id")); bi.setName(rs.getString("name")); bi.setSex(rs.getString("sex")); bi.setBirth(rs.getString("birth")); bi.setAddress(rs.getString("address")); list.add(bi); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } jdbc.close(); return list; } /** * 获取指定id的学生信息 * * @param id 学生id * @return 一个StudentInfo对象 */ public static StudentInfo getStudentById(String id) { String sql = "select * from studentinfo where id=" + id; DBBean jdbc = new DBBean(); ResultSet rs = jdbc.executeQuery(sql); StudentInfo si = new StudentInfo(); try { if (rs.next()) { si.setId(rs.getString("id")); si.setName(rs.getString("name")); si.setSex(rs.getString("sex")); si.setBirth(rs.getString("birth")); si.setAddress(rs.getString("address")); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } jdbc.close(); return si; } /** * 更新指定id的学生信息 * * @param si 要更新的学生的对象 * @return 修改的结果:1代表成功,0代表没有更新 */ public static int updateStudent(StudentInfo si) { int result = 0; String sql = "update studentinfo set name='" + si.getName() + "',sex='" + si.getSex() + "',birth='" + si.getBirth() +"',address='"+si.getAddress() + "' where id=" + si.getId(); DBBean jdbc = new DBBean(); result = jdbc.executeUpdate(sql); jdbc.close(); return result; } /** * 删除指定id的学生信息 * * @param id 学生id * @return 删除的结果:1代表成功,0代表没有删除 */ public static int deleteStudent(String id) { int result = 0; String sql = "delete from studentinfo where id=" + id; DBBean jdbc = new DBBean(); result = jdbc.executeUpdate(sql); jdbc.close(); return result; } /** * 增加一本图书 * * @param bi 图书对象 * @return 新增的结果:1代表成功,0代表没有增加 */ public static int addStudent(StudentInfo si) { int result = 0; String sql = "insert into studentinfo values('"+si.getId()+"','"+si.getName() + "','" + si.getSex() + "','" + si.getBirth()+"','"+si.getAddress() + "')"; DBBean jdbc = new DBBean(); result = jdbc.executeUpdate(sql); jdbc.close(); return result; } }
package student.bean; import java.sql.*; /** * 完成与数据库的连接和数据的访问 */ public class DBBean { private String driverStr = "com.mysql.jdbc.Driver"; private String connStr = "jdbc:mysql://localhost:3306/student?useSSL=false&useUnicode=true&characterEncoding=utf-8"; private String dbusername = "root"; private String dbpassword = "123456"; private Connection conn = null; private Statement stmt = null; public DBBean() { try { Class.forName(driverStr); conn = DriverManager.getConnection(connStr, dbusername, dbpassword); stmt = conn.createStatement(); } catch (Exception ex) { System.out.println("数据库连接失败!"); } } /** * 执行更新操作 * @param s * SQL语句 * @return * 更新操作的结果 */ public int executeUpdate(String s) { int result = 0; try { result = stmt.executeUpdate(s); } catch (Exception ex) { System.out.println("更新出现异常!"); } return result; } /** * 执行查询操作 * @param s * SQL语句 * @return * 查询结果 */ public ResultSet executeQuery(String s) { ResultSet rs = null; try { rs = stmt.executeQuery(s); } catch (Exception ex) { System.out.println("查询出现异常!");
package servlets; import java.io.IOException; import java.util.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.*; import student.bean.StudentInfo; /** * 接受客户端后缀为action的请求,并进行处理,并返回响应 * * */ @WebServlet("*.action") public class AjaxController extends HttpServlet { private static final long serialVersionUID = 1L; public AjaxController() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String actionUrl = request.getServletPath(); // 获取客户端的访问URL地址信息 if (actionUrl.equals("/list.action")) { // 查询所有图书 ArrayList<StudentInfo> list = StudentInfo.getStudentList(); // 调用StudentInfo的getStudentList方法完成 // 使用JSONArray对象将结果构建为json对象并输出到客户端 JSONArray jsonArray = new JSONArray(); for (int i = 0; i < list.size(); i++) { StudentInfo student = list.get(i); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", student.getId()); map.put("name", student.getName()); map.put("sex", student.getSex()); map.put("birth", student.getBirth()); map.put("address", student.getAddress()); JSONObject StudentObj = new JSONObject(map); jsonArray.put(StudentObj); } // 向客户端返回json结果 response.getWriter().print(jsonArray.toString()); } else if (actionUrl.equals("/add.action")) { // 增加学生信息操作 StudentInfo si = new StudentInfo(); si.setId(request.getParameter("id")); si.setName(request.getParameter("name")); si.setSex(request.getParameter("sex")); si.setBirth(request.getParameter("birth")); si.setAddress(request.getParameter("address")); int r = StudentInfo.addStudent(si); // 调用StudentInfo的addStudent方法完成 // 向客户端返回结果 response.getWriter().print(r); } else if (actionUrl.equals("/edit.action")) { // 编辑学生信息操作 String id = request.getParameter("id"); StudentInfo si = StudentInfo.getStudentById(id); // 调用StudentInfo的getStudentById方法完成 // 将该对象构建为json数据 Map<String, Object> map = new HashMap<String, Object>(); map.put("id", si.getId()); map.put("name", si.getName()); map.put("sex", si.getSex()); map.put("birth", si.getBirth()); map.put("address", si.getAddress()); JSONObject StudentObj = new JSONObject(map); // 向客户端返回结果 response.getWriter().print(StudentObj.toString()); } else if (actionUrl.equals("/update.action")) { // 更新学生信息操作 StudentInfo si = new StudentInfo(); si.setId(request.getParameter("id")); si.setName(request.getParameter("name")); si.setSex(request.getParameter("sex")); si.setBirth(request.getParameter("birth")); si.setAddress(request.getParameter("address")); int r = StudentInfo.updateStudent(si);// 调用StudentInfo的updateStudent方法完成 response.getWriter().print(r); // 向客户端返回结果 } else if (actionUrl.equals("/delete.action")) { // 删除学生信息操作 String id = request.getParameter("id"); int r = StudentInfo.deleteStudent(id); // 调用StudentInfo的deleteStudent方法完成 response.getWriter().print(r); // 向客户端返回结果 } } }
package servlets; import java.io.IOException; import java.util.ArrayList; import javax.servlet.*; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import student.bean.StudentInfo; /** * 用来接收客户端的后缀为do的请求 * * @author Leiyu * @version 1.0 * */ @WebServlet("*.do") public class StudentController extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String actionUrl = request.getServletPath(); // 获取客户请求的Servlet地址 if (actionUrl.equals("/index.do")) { // 查询所有学生信息 ArrayList<StudentInfo> list = StudentInfo.getStudentList(); // 调用StudentInfo的getStudentList方法查询所有学生信息,赋值给list request.setAttribute("list", list); // 在request增加属性list,其结果为list对象 request.getRequestDispatcher("/index.jsp").forward(request, response);// 重定向至index.jsp进行显示 } else if (actionUrl.equals("/addview.do")) { // 新增学生信息显示页面 request.getRequestDispatcher("add.html").forward(request, response); } else if (actionUrl.equals("/add.do")) { // 新增学生信息 StudentInfo si = new StudentInfo(); si.setId(request.getParameter("id")); si.setName(request.getParameter("name")); si.setSex(request.getParameter("sex")); si.setBirth(request.getParameter("birth")); si.setAddress(request.getParameter("address")); int r = StudentInfo.addStudent(si); // 调用StudentInfor的addStudent方法完成 if (r == 1) request.getRequestDispatcher("success.html").forward(request, response); // 成功的话重定向至success.html else request.getRequestDispatcher("failure.html").forward(request, response); // 失败的话重定向至failure.html } else if (actionUrl.equals("/edit.do")) { // 客户端要对指定id的学生信息进行修改 String id = request.getParameter("id"); StudentInfo si = StudentInfo.getStudentById(id); // 调用StudentInfo的getStudentById方法获取学生信息,赋值给si对象 request.setAttribute("si", si); // 将si对象增加到request的属性中 request.getRequestDispatcher("/edit.jsp").forward(request, response);// 重定向至edit.jsp进行显示 } else if (actionUrl.equals("/update.do")) { // 用户输入要修改的学生的信息之后需要保存到数据库 StudentInfo si = new StudentInfo(); si.setId(request.getParameter("id")); si.setName(request.getParameter("name")); si.setSex(request.getParameter("sex")); si.setBirth(request.getParameter("birth")); si.setAddress(request.getParameter("address")); int r = StudentInfo.updateStudent(si);// 调用StudentInfo的updateStudent方法实现 if (r == 1) request.getRequestDispatcher("/success.html").forward(request, response);// 成功的话重定向至success.html else request.getRequestDispatcher("/failure.html").forward(request, response);// 失败的话重定向至failure.html } else if (actionUrl.equals("/delete.do")) { // 用户需要删除指定id的学生信息 String id = request.getParameter("id"); int r = StudentInfo.deleteStudent(id); // 调用StudentInfo的deleteStudent方法实现 if (r == 1) request.getRequestDispatcher("/success.html").forward(request, response);// 成功的话重定向至success.html else request.getRequestDispatcher("/failure.html").forward(request, response);// 失败的话重定向至failure.html } } }
} return rs; } /** * 关闭数据库 */ public void close() { try { stmt.close(); conn.close(); } catch (Exception e) { } } }