jsp+servlet实现最简单的增删改查代码分享(2)

package ceet.ac.cn.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import ceet.ac.cn.dao.AdminDao; public class DeleteServlet extends HttpServlet{ //删除数据 private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String idStr = req.getParameter("id"); //删除数据的ID,根据ID删除 if(idStr != null && !idStr.equals("")){ int id = Integer.valueOf(idStr); AdminDao dao = new AdminDao(); dao.deleteAdmin(id); } req.getRequestDispatcher("ShowServlet").forward(req, resp); } }

package ceet.ac.cn.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import ceet.ac.cn.dao.AdminDao; import ceet.ac.cn.model.Admin; public class ShowServlet extends HttpServlet{ //显示全部数据 private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { AdminDao dao = new AdminDao(); List<Admin> list = dao.getAllAdmin(); req.setAttribute("list", list); req.getRequestDispatcher("index.jsp").forward(req, resp); } }

package ceet.ac.cn.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import ceet.ac.cn.dao.AdminDao; import ceet.ac.cn.model.Admin; public class UpdateServlet extends HttpServlet{ //修改 private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //查询到选中ID的值所对应的数据 String idStr = req.getParameter("id"); if(idStr != null && !idStr.equals("")){ int id = Integer.valueOf(idStr); AdminDao dao = new AdminDao(); Admin admin = dao.selectAdminById(id); req.setAttribute("admin", admin); } req.getRequestDispatcher("update.jsp").forward(req, resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //根据此ID对数据的值进行修改 String username = req.getParameter("username"); String userpwd = req.getParameter("userpwd"); String idStr = req.getParameter("id"); Admin admin = new Admin(); admin.setId(Integer.valueOf(idStr)); admin.setUsername(new String(username.getBytes("ISO-8859-1"),"UTF-8")); admin.setUserpwd(new String(userpwd.getBytes("ISO-8859-1"),"UTF-8")); AdminDao dao = new AdminDao(); dao.updateAdmin(admin); req.getRequestDispatcher("ShowServlet").forward(req, resp); } }

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>添加</title> <link href="https://www.jb51.net/css/index.css" type="text/css" /> </head> <body> <form action="AddServlet" method="post"> <table> <tr> <td colspan="2"><h1>添加管理员</h1></td> </tr> <tr> <td>管理员帐号:</td> <td><input type="text"/></td> </tr> <tr> <td>管理员密码:</td> <td><input type="password"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"/> <input type="reset" value="清空"/> </td> </tr> </table> </form> </body> </html>

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!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"> <title>显示</title> <style type="text/css"> table { border: 1px solid pink; margin: 0 auto; } td{ width: 150px; border: 1px solid pink; text-align: center; } </style> </head> <body> <table> <tr> <td>编号</td> <td>帐号</td> <td>密码</td> <td>操作</td> </tr> <c:forEach items="${list}" var="item"> <tr> <td>${item.id }</td> <td>${item.username }</td> <td>${item.userpwd }</td> <td><a href="DeleteServlet?id=${item.id }">删除</a>|<a href="UpdateServlet?id=${item.id }">修改</a></td> </tr> </c:forEach> <tr> <td colspan="6"><a href="https://www.jb51.net/add.jsp">添加管理员</a></td> </tr> </table> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjxgg.html