Wangyanyixiang_branch
Lin 2 months ago
parent 4166439970
commit 26612cb0f4

@ -10,7 +10,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import javabean.db_conn; import javabean.db_conn;
import javabean.flight; import javabean.flight;
@ -32,28 +31,44 @@ public class search_fly extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8"); req.setCharacterEncoding("utf-8");
String url=null; String url = "default/index.jsp";
url="default/index.jsp";
String departure=null; String departure = req.getParameter("departure");
String destination=null; String destination = req.getParameter("destination");
departure=req.getParameter("departure"); String departureYear = req.getParameter("departureYear");
destination=req.getParameter("destination"); String departureMonth = req.getParameter("departureMonth");
String departureDay = req.getParameter("departureDay");
String sql = null; String sql = null;
if (departure.isEmpty() && destination.isEmpty() && departureYear.isEmpty() && departureMonth.isEmpty() && departureDay.isEmpty()) {
if(departure==""&&destination=="") { // 如果所有字段都为空,重定向到原页面
//如果始发地和目的地都为空则跳转回原页面
resp.sendRedirect(url); resp.sendRedirect(url);
}else if (departure!=""&&destination=="") { } else {
sql="select * from flight where f_s_p='"+departure+"'"; sql = "SELECT * FROM flight WHERE 1=1";
}else if (departure==""&&destination!="") {
sql="select * from flight where f_a_p='"+destination+"'"; if (!departure.isEmpty()) {
}else if (departure!=""&&destination!="") { sql += " AND f_s_p='" + departure + "'";
sql="select * from flight where f_s_p='"+departure+"' and f_a_p='"+destination+"'"; }
if (!destination.isEmpty()) {
sql += " AND f_a_p='" + destination + "'";
} }
if(departure==""&&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 { } else {
req.setAttribute("errorMsg", "日期格式不正确,请输入有效的年、月、日");
}
} catch (NumberFormatException e) {
req.setAttribute("errorMsg", "日期格式不正确,请输入数字");
}
}
db_conn conn = new db_conn(); db_conn conn = new db_conn();
ArrayList<flight> flightlist = new ArrayList<flight>(); ArrayList<flight> flightlist = new ArrayList<flight>();
@ -70,16 +85,14 @@ public class search_fly extends HttpServlet {
flight_info.setF_a_t(res.getString(7)); flight_info.setF_a_t(res.getString(7));
flight_info.setF_p(res.getString(8)); flight_info.setF_p(res.getString(8));
flightlist.add(flight_info); flightlist.add(flight_info);
} }
req.setAttribute("flightlist", flightlist); req.setAttribute("flightlist", flightlist);
} catch (SQLException e) { } catch (SQLException e) {
System.out.println("错误信息:"+e); System.out.println("错误信息:" + e);
} finally { } finally {
conn.closeDB(); conn.closeDB();
} }
req.getRequestDispatcher("default/search.jsp").forward(req, resp); req.getRequestDispatcher("default/search.jsp").forward(req, resp);
} }
} }
} }

Loading…
Cancel
Save