You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
text1/src/web/servlet/notify/DeleteNotifyServlet.java

50 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package web.servlet.notify;
import domain.Student;
import service.NotifyService;
import service.impl.NotifyServiceImpl;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
// 使用WebServlet注解将该类映射到"/deleteNotifyServlet"这个URL路径表明这是一个用于处理对应网络请求的Servlet类
// 从类名可以推断出其主要功能是处理删除通知相关的业务逻辑。
@WebServlet("/deleteNotifyServlet")
public class DeleteNotifyServlet extends HttpServlet {
// 重写doPost方法用于处理HTTP POST请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置请求的字符编码为"utf-8",确保能正确解析包含中文等多字节字符的请求参数,
// 虽然此处不一定涉及中文参数,但保持良好的编码设置习惯有助于避免潜在的乱码问题。
request.setCharacterEncoding("utf-8");
// 获取当前请求对应的HttpSession对象HttpSession用于在多个请求之间保存用户相关的会话信息
// 不过在这段代码中暂时未看到对会话信息有进一步的操作,可根据实际业务需求后续考虑是否添加相关逻辑。
HttpSession session = request.getSession();
// 从请求中获取名为"id"的参数值该参数应该是用于标识要删除的通知的唯一ID
// 通常在前端页面如通知列表页面点击删除操作时会将对应通知的ID传递过来。
String notifyid = request.getParameter("id");
// 创建NotifyService的实现类实例用于调用业务逻辑层中与删除通知相关的方法
// 这个实现类会负责根据传入的通知ID在数据库等存储介质中执行删除对应通知记录的操作。
NotifyService service = new NotifyServiceImpl();
// 调用业务逻辑层的deleteNotifyById方法传入获取到的通知IDnotifyid执行删除通知的操作将对应的通知记录从数据库中移除。
service.deleteNotifyById(notifyid);
// 将当前请求转发到"/notifyListServlet"通常这个Servlet是用于展示通知列表的
// 这样在删除通知成功后,可以直接跳转到通知列表页面,展示最新的通知列表情况(已删除指定通知后的列表)。
request.getRequestDispatcher("/notifyListServlet").forward(request, response);
}
// 重写doGet方法在这个类中直接调用doPost方法来处理GET请求意味着GET请求和POST请求在本Servlet中的处理逻辑是一样的
// 即当接收到GET请求时同样会执行上述doPost方法中的删除通知相关逻辑操作。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}