JavaWeb课程复习资料(九)——删除功能

2022-11-29 20:31:47 浏览数 (1)

承接上文:JavaWeb课程复习资料(八)——添加功能

1、创建【DeleteByIdServlet.jsp】

2、添加【DeleteByIdServlet.jsp】编码

代码语言:javascript复制
package com.item.servlet;

import com.item.dao.UserInfoDAO;
import com.item.daoimpl.UserInfoDAOImpl;

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("/DeleteByIdServlet")
public class DeleteByIdServlet 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");
        UserInfoDAO db =new UserInfoDAOImpl();
        boolean isf = db.DeleteById(Integer.parseInt(id));
        PrintWriter out = resp.getWriter();
        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);
    }
}

3、启动测试

删除完成

再删几个试试:

 没删除一个都会有提示:

0 人点赞