ajax技术实现javaweb
项目结构图:
1.DBBean.java
package bean; import java.sql.*; /** * 完成与数据库的连接和数据的访问 * @author vangogh * @version 1.0 * */ public class DBBean { private String driverStr = "com.mysql.jdbc.Driver"; private String connStr = "jdbc:mysql://127.0.0.1:3306/web3?useSSL=false&useUnicode=true&characterEncoding=utf-8"; private String dbusername = "root"; private String dbpassword = "lyf123456"; 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("查询出现异常!"); } return rs; } /** * 关闭数据库 */ public void close() { try { stmt.close(); conn.close(); } catch (Exception e) { } } }
2.Info.java
package bean; import java.util.*; import java.sql.*; public class Info { private String id;//学号 private String name;//姓名 private String sex;//性别 private String birth;//生日 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 Info(){}; public Info(String id, String name, String sex, String birth) { this.id = id; this.name = name; this.sex = sex; this.birth = birth; } /** * 从info表中获取所有的信息 * * @return info的数组 */ public static ArrayList<Info> getInfoList() { ArrayList<Info> list = new ArrayList<Info>(); String sql = "select * from info"; DBBean jdbc = new DBBean(); ResultSet rs = jdbc.executeQuery(sql); try { while (rs.next()) { Info bi = new Info(); bi.setId(rs.getString("id")); bi.setName(rs.getString("name")); bi.setSex(rs.getString("sex")); bi.setBirth(rs.getString("birth")); list.add(bi); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } jdbc.close(); return list; } /** * 获取指定id的信息 * * @param id id * @return 一个Info对象 */ public static Info getInfoById(String id) { String sql = "select * from info where id=" + id; DBBean jdbc = new DBBean(); ResultSet rs = jdbc.executeQuery(sql); Info bi = new Info(); try { if (rs.next()) { bi.setId(rs.getString("id")); bi.setName(rs.getString("name")); bi.setSex(rs.getString("sex")); bi.setBirth(rs.getString("birth")); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } jdbc.close(); return bi; } /** * 更新指定id的信息 * * @param bi 要更新的对象 * @return 修改的结果:1代表成功,0代表没有更新 */ public static int updateInfo(Info bi) { int result = 0; String sql = "update info set name='" + bi.getName() + "',sex='" + bi.getSex() + "',birth=" + bi.getBirth() + " where id=" + bi.getId(); DBBean jdbc = new DBBean(); result = jdbc.executeUpdate(sql); jdbc.close(); return result; } /** * 删除指定id的学生 * * @param id id * @return 删除的结果:1代表成功,0代表没有删除 */ public static int deleteInfo(String id) { int result = 0; String sql = "delete from info where id=" + id; DBBean jdbc = new DBBean(); result = jdbc.executeUpdate(sql); jdbc.close(); return result; } /** * 增加一个学生 * * @param bi 对象 * @return 新增的结果:1代表成功,0代表没有增加 */ public static int addInfo(Info bi) { int result = 0; String sql = "insert into info values('" + bi.getId() + "','" + bi.getName() + "','" + bi.getSex() + "'," + bi.getBirth() + ")"; DBBean jdbc = new DBBean(); result = jdbc.executeUpdate(sql); jdbc.close(); return result; } }
3.AjaxController.java
package servlet; 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 bean.Info; /** * 接受客户端后缀为action的请求,并进行处理,并返回响应 * * @author vangogh * @version 1.0 * */ @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("/index.action")) { // 查询所有 ArrayList<Info> list = Info.getInfoList(); // 使用JSONArray对象将结果构建为json对象并输出到客户端 JSONArray jsonArray = new JSONArray(); for (int i = 0; i < list.size(); i++) { Info info = list.get(i); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", info.getId()); map.put("name", info.getName()); map.put("sex", info.getSex()); map.put("birth", info.getBirth()); JSONObject InfoObj = new JSONObject(map); jsonArray.put(InfoObj); } // 向客户端返回json结果 response.getWriter().print(jsonArray.toString()); } else if (actionUrl.equals("/add.action")) { // 增加操作 Info bi = new Info(); bi.setId(request.getParameter("id")); bi.setName(request.getParameter("name")); bi.setSex(request.getParameter("sex")); bi.setBirth(request.getParameter("birth")); int r = Info.addInfo(bi); // 向客户端返回结果 response.getWriter().print(r); } else if (actionUrl.equals("/edit.action")) { // 编辑操作 String id = request.getParameter("id"); Info bi = Info.getInfoById(id); // 调用Info的getInfoById方法完成 // 将该对象构建为json数据 Map<String, Object> map = new HashMap<String, Object>(); map.put("id", bi.getId()); map.put("name", bi.getName()); map.put("sex", bi.getSex()); map.put("birth", bi.getBirth()); JSONObject InfoObj = new JSONObject(map); // 向客户端返回结果 response.getWriter().print(InfoObj.toString()); } else if (actionUrl.equals("/update.action")) { // 更新操作 Info bi = new Info(); bi.setId(request.getParameter("id")); bi.setName(request.getParameter("name")); bi.setSex(request.getParameter("sex")); bi.setBirth(request.getParameter("birth")); int r = Info.updateInfo(bi);// 调用Info的updateInfo方法完成 response.getWriter().print(r); // 向客户端返回结果 } else if (actionUrl.equals("/delete.action")) { // 删除操作 String id = request.getParameter("id"); int r = Info.deleteInfo(id); // 调用Info的deleteInfo方法完成 response.getWriter().print(r); // 向客户端返回结果 } } }
4.InfoController.java
package servlet; import java.io.IOException; import java.util.ArrayList; import javax.servlet.*; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import bean.Info; /** * 用来接收客户端的后缀为do的请求 * * @author vangogh * @version 1.0 * */ @WebServlet("*.do") public class InfoController 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<Info> list = Info.getInfoList(); // 调用Info的getInfoList方法查询所有图书,赋值给list request.setAttribute("list", list); // 在request增加属性list,其结果为list对象 request.getRequestDispatcher("list.jsp").forward(request, response);// 重定向至list.jsp进行显示 } else if (actionUrl.equals("/adview.do")) { // 新增学生显示页面 request.getRequestDispatcher("add.html").forward(request, response); } else if (actionUrl.equals("/add.do")) { // 新增学生 Info bi = new Info(); bi.setId(request.getParameter("id")); bi.setName(request.getParameter("name")); bi.setSex(request.getParameter("sex")); bi.setBirth(request.getParameter("birth")); int r = Info.addInfo(bi); // 调用Info的addInfo方法完成 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"); Info bi = Info.getInfoById(id); // 调用Info的getInfoById方法获取info信息,赋值给bi对象 request.setAttribute("bi", bi); // 将bi对象增加到request的属性中 request.getRequestDispatcher("edit.jsp").forward(request, response);// 重定向至edit.jsp进行显示 } else if (actionUrl.equals("/update.do")) { // 用户输入要修改的info的信息之后需要保存到数据库 Info bi = new Info(); bi.setId(request.getParameter("id")); bi.setName(request.getParameter("name")); bi.setSex(request.getParameter("sex")); bi.setBirth(request.getParameter("birth")); int r = Info.updateInfo(bi);// 调用Info的updateInfo方法实现 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的info String id = request.getParameter("id"); int r = Info.deleteInfo(id); // 调用Info的deleteInfo方法实现 if (r == 1) request.getRequestDispatcher("success.html").forward(request, response);// 成功的话重定向至success.html else request.getRequestDispatcher("failure.html").forward(request, response);// 失败的话重定向至failure.html } } }
前端jsp后续有需要会给