diff --git a/ticketing-master/src/com/cn/servlet/GetByStartEndStationServlet.java b/ticketing-master/src/com/cn/servlet/GetByStartEndStationServlet.java index 3885ef1..d8ad259 100644 --- a/ticketing-master/src/com/cn/servlet/GetByStartEndStationServlet.java +++ b/ticketing-master/src/com/cn/servlet/GetByStartEndStationServlet.java @@ -21,108 +21,115 @@ import com.cn.service.impl.TrainServiceImpl; * @date: 2019年9月11日 下午4:41:33 */ public class GetByStartEndStationServlet extends HttpServlet { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; /** - * @see HttpServlet#HttpServlet() + * HttpServlet的构造函数。 */ public GetByStartEndStationServlet() { super(); - // TODO Auto-generated constructor stub + // 构造函数中的代码,通常不需要自定义操作,因为父类已经处理 } - /** - * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) - */ - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - - /** - * 先执行,将所有车次信息展示到界面 - */ - TrainService trainService = new TrainServiceImpl(); - List startStationList = trainService.getAllStartStation(); - if(startStationList != null) { - request.setAttribute("startStationList", startStationList); - }else { - request.setAttribute("msg", "初始站站点为空"); - } - - List endStationList = trainService.getAllEndStation(); - if(endStationList != null) { - request.setAttribute("endStationList", endStationList); - }else { - request.setAttribute("msg", "终点站站点为空"); - } - - List trainList = trainService.getAll(); - if(trainList != null) { - request.setAttribute("trainList", trainList); - }else { - request.setAttribute("msg", "没有任何站点信息"); - } - - String msg = (String) request.getSession().getAttribute("msg"); - request.setAttribute("msg", msg); - - request.getRequestDispatcher("pages/user/getByStartEndStation.jsp").forward(request, response); - - } - - /** - * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) - */ - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - - /** - * 后执行,处理会员查询车次信息业务,将查询结果展示到界面 - */ - String startStation = request.getParameter("startStation"); - String endStation = request.getParameter("endStation"); - String startTime = request.getParameter("startTime"); - - TrainService trainService = new TrainServiceImpl(); - List startStationList = trainService.getAllStartStation(); - - - - // 将站点列表传到界面,否则下拉框将没有站点 - if(startStationList != null) { - request.setAttribute("startStationList", startStationList); - }else { - //request.setAttribute("msg", "初始站站点为空"); - request.getSession().setAttribute("msg", "初始站站点为空"); - } - - List endStationList = trainService.getAllEndStation(); - if(endStationList != null) { - request.setAttribute("endStationList", endStationList); - }else { - //request.setAttribute("msg", "终点站站点为空"); - request.getSession().setAttribute("msg", "终点站站点为空"); - } - - - List list = trainService.getByStartEndStation(startStation, endStation, startTime); - - PrintWriter out = response.getWriter(); - - // 判断是否查询到车次 - if(list!=null && list.size()>0) { - // 查到了,将结果转发到界面 - request.setAttribute("trainList", list); - request.getRequestDispatcher("pages/user/getByStartEndStation.jsp").forward(request, response); - } else { - // 没有查到 - String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/"; - - String path = ""; - - out.write(path); - } - - out.close(); - - } + /** + * 处理GET请求的方法。 + * 当客户端发送GET请求时,这个方法会被调用。 + * @param request HttpServletRequest对象,包含客户端请求信息。 + * @param response HttpServletResponse对象,用于发送响应到客户端。 + * @throws ServletException 可能抛出的Servlet异常。 + * @throws IOException 可能抛出的IO异常。 + */ + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 创建TrainService的实现类对象,用于访问车次相关的业务逻辑 + TrainService trainService = new TrainServiceImpl(); + // 获取所有起始站信息 + List startStationList = trainService.getAllStartStation(); + if(startStationList != null) { + request.setAttribute("startStationList", startStationList); + } else { + request.setAttribute("msg", "初始站站点为空"); + } + + // 获取所有终点站信息 + List endStationList = trainService.getAllEndStation(); + if(endStationList != null) { + request.setAttribute("endStationList", endStationList); + } else { + request.setAttribute("msg", "终点站站点为空"); + } + + // 获取所有车次信息 + List trainList = trainService.getAll(); + if(trainList != null) { + request.setAttribute("trainList", trainList); + } else { + request.setAttribute("msg", "没有任何站点信息"); + } + + // 从session中获取消息,并设置到request属性中 + String msg = (String) request.getSession().getAttribute("msg"); + request.setAttribute("msg", msg); + + // 转发请求到JSP页面 + request.getRequestDispatcher("pages/user/getByStartEndStation.jsp").forward(request, response); + } -} + /** + * 处理POST请求的方法。 + * 当客户端发送POST请求时,这个方法会被调用。 + * @param request HttpServletRequest对象,包含客户端请求信息。 + * @param response HttpServletResponse对象,用于发送响应到客户端。 + * @throws ServletException 可能抛出的Servlet异常。 + * @throws IOException 可能抛出的IO异常。 + */ + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 获取请求参数 + String startStation = request.getParameter("startStation"); + String endStation = request.getParameter("endStation"); + String startTime = request.getParameter("startTime"); + + // 创建TrainService的实现类对象 + TrainService trainService = new TrainServiceImpl(); + // 获取所有起始站信息 + List startStationList = trainService.getAllStartStation(); + + // 将站点列表传到界面 + if(startStationList != null) { + request.setAttribute("startStationList", startStationList); + } else { + // 将消息设置到session中 + request.getSession().setAttribute("msg", "初始站站点为空"); + } + + // 获取所有终点站信息 + List endStationList = trainService.getAllEndStation(); + if(endStationList != null) { + request.setAttribute("endStationList", endStationList); + } else { + // 将消息设置到session中 + request.getSession().setAttribute("msg", "终点站站点为空"); + } + + // 根据起始站、终点站和起始时间查询车次 + List list = trainService.getByStartEndStation(startStation, endStation, startTime); + + // 获取PrintWriter对象用于输出JavaScript代码 + PrintWriter out = response.getWriter(); + + // 判断是否查询到车次 + if(list!=null && list.size()>0) { + // 查到了,将结果转发到界面 + request.setAttribute("trainList", list); + request.getRequestDispatcher("pages/user/getByStartEndStation.jsp").forward(request, response); + } else { + // 没有查到,输出JavaScript代码提示用户并重定向 + String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/"; + String path = ""; + out.write(path); + } + + // 关闭PrintWriter对象 + out.close(); + } +} \ No newline at end of file