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.
GenFlightRec/src/servlet/search_fly.java

116 lines
4.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 servlet;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javabean.db_conn;
import javabean.flight;
public class search_fly extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
String url=null;
if(session.getAttribute("url")!=null) {
url=session.getAttribute("url").toString();
}else {
url="default/index.jsp";
}
resp.sendRedirect(url);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String url = "default/index.jsp";
String departure = req.getParameter("departure");
String destination = req.getParameter("destination");
String departureYear = req.getParameter("departureYear");
String departureMonth = req.getParameter("departureMonth");
String departureDay = req.getParameter("departureDay");
String date=req.getParameter("date");
String sql = null;
/*
if (departure.isEmpty() || destination.isEmpty() || departureYear.isEmpty() || departureMonth.isEmpty() || departureDay.isEmpty()) {
// 濡傛灉鎵<E78189>鏈夊瓧娈甸兘涓虹┖锛岄噸瀹氬悜鍒板師椤甸潰
resp.sendRedirect(url);
} else {
sql = "SELECT * FROM flight WHERE 1=1";
if (!departure.isEmpty()) {
sql += " AND f_s_p='" + departure + "'";
}
if (!destination.isEmpty()) {
sql += " AND f_a_p='" + destination + "'";
}
if (!departureYear.isEmpty() && !departureMonth.isEmpty() && !departureDay.isEmpty()) {
// 楠岃瘉鏃ユ湡鏍煎紡
try {
int year = Integer.parseInt(departureYear);
int month = Integer.parseInt(departureMonth);
int day = Integer.parseInt(departureDay);
if (year >= 1900 && year <= 9999 && month >= 1 && month <= 12 && day >= 1 && day <= 31) {
String departureDate = String.format("%04d-%02d-%02d", year, month, day);
sql += " AND DATE(f_Date)='" + departureDate + "'";
} else {
req.setAttribute("errorMsg", "鏃ユ湡鏍煎紡涓嶆纭紝璇疯緭鍏ユ湁鏁堢殑骞淬<E9AA9E>佹湀銆佹棩");
}
} catch (NumberFormatException e) {
req.setAttribute("errorMsg", "鏃ユ湡鏍煎紡涓嶆纭紝璇疯緭鍏ユ暟瀛<E69A9F>");
}
}
*/
if (date == null || date.isEmpty()) {
// 如果日期为空则重定向到某个URL这里假设是url变量
resp.sendRedirect(url);
} else {
// 构建SQL查询语句
sql = "SELECT * FROM flight WHERE 1=1";
sql += " AND DATE(f_Date)='" + date + "'";
db_conn conn = new db_conn();
ArrayList<flight> flightlist = new ArrayList<flight>();
ResultSet res = conn.executeQuery(sql);
try {
while (res.next()) {
flight flight_info = new flight();
flight_info.setF_n(res.getString(1));
flight_info.setF_s_p(res.getString(2));
flight_info.setF_a_p(res.getString(3));
flight_info.setF_s_a(res.getString(4));
flight_info.setF_a_a(res.getString(5));
flight_info.setF_s_t(res.getString(6));
flight_info.setF_a_t(res.getString(7));
flight_info.setF_p(res.getString(8));
flight_info.setF_d(res.getString(9));
flight_info.setF_delay(res.getString(10));
flight_info.setF_food(res.getString(11));
flight_info.setF_wide(res.getString(12));
flightlist.add(flight_info);
}
req.setAttribute("date", date);
req.setAttribute("departure", departure);
req.setAttribute("destination", destination);
req.setAttribute("flightlist", flightlist);
} catch (SQLException e) {
System.out.println("閿欒淇℃伅锛<E4BC85>" + e);
} finally {
conn.closeDB();
}
req.getRequestDispatcher("default/search.jsp").forward(req, resp);
}
}
}