1、创建【UpdateByIdServlet.java】
package com.item.servlet; import com.item.dao.UserInfoDAO; import com.item.daoimpl.UserInfoDAOImpl; import com.item.pojo.UserInfo; 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("/UpdateByIdServlet") public class UpdateByIdServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); String id=req.getParameter("id"); int i = Integer.parseInt(id); UserInfoDAO db=new UserInfoDAOImpl(); UserInfo userInfo = db.SelectById(i); req.setAttribute("userInfo",userInfo); req.getRequestDispatcher("UpdateServlet.jsp").forward(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } }
2、创建【UpdateServlet.jsp】
<%@ page import="com.item.pojo.UserInfo" %><%-- Created by IntelliJ IDEA. User: 红目香薰 Date: 2022/2/26 Time: 13:30 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改页面</title> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <form action="/UpdateServlet" method="post"> <% UserInfo userInfo=(UserInfo)request.getAttribute("userInfo"); %> <p> <input type="text" name="id" value="<%= userInfo.getId()%>" class="form-control" readonly required/> </p> <p> 创建时间:<%= userInfo.getCreateDate()%> </p> <p> <input type="text" placeholder="请输入用户名" value="<%= userInfo.getUserName()%>" name="userName" class="form-control" required/> </p> <p> <input type="text" placeholder="请输入爱好" value="<%= userInfo.getIntroduce()%>" name="introduce" class="form-control" required/> </p> <p> <input type="submit" value="修改" class="btn btn-primary"/> </p> </form> </body> </html>
3、创建【UpdateServlet.jsp】
package com.item.servlet; import com.item.dao.UserInfoDAO; import com.item.daoimpl.UserInfoDAOImpl; import com.item.pojo.UserInfo; 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; @WebServlet("/UpdateServlet") public class UpdateServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); String id = req.getParameter("id"); String userName = req.getParameter("userName"); String introduce = req.getParameter("introduce"); PrintWriter out = resp.getWriter(); if (id == null || userName == null || introduce == null) { out.println("<script>alert('参数不允许为空。')</script>"); } UserInfoDAO db = new UserInfoDAOImpl(); UserInfo userInfo = db.SelectById(Integer.parseInt(id)); userInfo.setUserName(userName); userInfo.setIntroduce(introduce); boolean isf = db.UpdateById(userInfo); if(isf){ out.println("<script>alert('修改成功');window.location.href='/index.jsp';</script>"); }else{ out.println("<script>alert('修改失败');window.location.href='/index.jsp';</script>"); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } }
4、执行测试、
测试成功。
整个Servlet完成,希望能给大家带来一定的帮助。