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.
text/src/web/servlet/notify/NotifyListToServlet.java

110 lines
4.9 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.Admin;
import domain.Notify;
import domain.Student;
import domain.Teacher;
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;
import java.util.List;
@WebServlet("/notifyListToServlet")
public class NotifyListToServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
NotifyService service = new NotifyServiceImpl();
List<Notify> notifys = service.findAll();
request.setAttribute("notifys",notifys);
// 获取当前会话
HttpSession session = request.getSession();
// 从会话中获取学生对象
Student student= (Student)session.getAttribute("student");
// 从会话中获取教师对象
Teacher teacher= (Teacher)session.getAttribute("teacher");
// 如果会话中存在学生对象且不存在教师对象,则转发到学生通知列表页面
if (student != null && teacher == null) {
request.getRequestDispatcher("/WEB-INF/notify/notifyListToStudent.jsp").forward(request,response);
// 如果会话中存在教师对象且不存在学生对象,则转发到教师通知列表页面
} else if (teacher != null && student == null) {
request.getRequestDispatcher("/WEB-INF/notify/notifyListToTeacher.jsp").forward(request,response);
// 如果会话中既不存在学生对象也不存在教师对象,则转发到错误页面
} else {
request.getRequestDispatcher("error.jsp").forward(request, response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
//package web.servlet.notify;
//
//import domain.Admin;
//import domain.Notify;
//import domain.Student;
//import domain.Teacher;
//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;
//import java.util.List;
///**
// * 转发到通知列表的Servlet
// */
//@WebServlet("/notifyListToServlet")
//public class NotifyListToServlet extends HttpServlet {
// /**
// * 处理POST请求获取所有通知并转发到通知列表页面。
// *
// * @param request HTTP请求对象包含客户端发送的数据。
// * @param response HTTP响应对象用于生成返回给客户端的响应。
// * @throws ServletException 如果处理请求时发生错误。
// * @throws IOException 如果输入或输出异常发生。
// */
// protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// // 设置请求的字符编码为UTF-8以支持中文字符的正确处理
// request.setCharacterEncoding("utf-8");
// // 创建通知服务的实例,以便获取通知数据
// NotifyService service = new NotifyServiceImpl();
// List<Notify> notifys = service.findAll();
// request.setAttribute("notifys",notifys);
//
// // 获取当前会话
// HttpSession session = request.getSession();
// // 从会话中获取学生对象
// Student student= (Student)session.getAttribute("student");
// // 从会话中获取教师对象
// Teacher teacher= (Teacher)session.getAttribute("teacher");
// // 如果会话中存在学生对象且不存在教师对象,则转发到学生通知列表页面
// if (student != null && teacher == null) {
// request.getRequestDispatcher("/WEB-INF/notify/notifyListToStudent.jsp").forward(request,response);
// // 如果会话中存在教师对象且不存在学生对象,则转发到教师通知列表页面
// } else if (teacher != null && student == null) {
// request.getRequestDispatcher("/WEB-INF/notify/notifyListToTeacher.jsp").forward(request,response);
// // 如果会话中既不存在学生对象也不存在教师对象,则转发到错误页面
// } else {
// request.getRequestDispatcher("error.jsp").forward(request, response);
// }
//
// }
//
// protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// doPost(request,response);
// }
//}