实现用户修改[较为繁琐]
UpdateQueryServlet
package Servlet; import Bean.User; import Dao.UserDao; 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 java.io.IOException; @WebServlet("/UpdateQueryServlet") public class UpdateQueryServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1、获取数据 String id = request.getParameter("uid"); User user=new UserDao().getByID(Integer.parseInt(id)); // 2、并把数据存入域中 request.setAttribute("user",user); // 3、跳转至修改页面update.jsp request.getRequestDispatcher("/update.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
updata.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/4/27 Time: 8:57 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/UpdateSubmitServlet" method="post"> <input type="hidden" name="id"value="${user.id}"/><br> 用户名:<input type="text" name="username" value="${user.username}" /><br> 密码:<input type="password" name="password" value="${user.password}" /><br> 邮箱:<input type="text" name="email" value="${user.email}" /><br> 生日:<input type="date" name="birthday" value="${user.birthday}" /><br> <input type="submit" value="修改"> </form> </body> </html>
UpdateSubmitServlet
package Servlet; import Bean.User; import Dao.UserDao; 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 java.io.IOException; import java.io.PrintWriter; import java.text.ParseException; import java.text.SimpleDateFormat; @WebServlet("/UpdateSubmitServlet") public class UpdateSubmitServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); // =====分割线===== // 获取参数值 String id=request.getParameter("id"); String name=request.getParameter("username"); String password=request.getParameter("password"); String email=request.getParameter("email"); String birthday=request.getParameter("birthday"); // 封装Bean对象 User user = new User(); user.setId(Integer.parseInt(id)); user.setName(name); user.setPassword(password); user.setEmail(email); try{ user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday)); }catch (ParseException e){ e.printStackTrace(); } // 修改数据库中的该条数据 boolean f = new UserDao().update(user); if(f){ out.print("<script>alert('修改成功');document.location.href='/QueryAllServlet'</script>"); } else{ out.print("<script>alert('修改失败');document.location.href='/QueryAllServlet'</script>"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
修改流程:在queryall.jsp中点击修改按钮跳转到UpdateQueryServlet去获取要修改的数据并存入域中,updata.jsp去获取存入域中的数据并显示在页面上,当修改完成之后点击确定按钮提交给UpdateSubmitServlet