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