一:項目業(yè)務(wù)需求
航班信息管理系統(tǒng)是學(xué)習(xí)Javaweb的一個小項目,首先對該項目的業(yè)務(wù)需求進(jìn)行分析,根據(jù)項目文檔知它的主要實現(xiàn)技術(shù)為 SERVLET、JSP、MVC 架構(gòu)、JDBC 和 MySQL。該項目著重學(xué)生的實際應(yīng)用場景來設(shè)計,模擬 機(jī)場中的航班系統(tǒng)的業(yè)務(wù)實現(xiàn)以及擴(kuò)展,能夠?qū)崿F(xiàn)航班信息管理的的所有功能,包括航班信息,航 班目的地查詢等功能的實現(xiàn);
本項目共實現(xiàn)八大功能模塊
? 注冊模塊:用戶輸入用戶名,真實姓名,密碼進(jìn)行注冊,注冊驗證通過后,將用戶存儲到數(shù)據(jù)庫中,如果數(shù)據(jù)庫中已有相同用戶名,則需要重新注冊。
登錄模塊:對管理員輸入的用戶名,密碼進(jìn)行驗證,驗證通過后,管理員可以使用航班信息管 理系統(tǒng)中所有權(quán)限的功能,否則重新登錄該系統(tǒng)。
航班信息管理功能:管理員登錄成功,可以查詢所有航班信息。 .
新增航班信息功能:管理員登錄成功,可以新增航班信息,新增完畢以后跳轉(zhuǎn)到主頁面顯示所
以航班信息
刪除航班信息功能:管理員登錄成功,可以刪除航班信息,刪除完畢以后跳轉(zhuǎn)到主頁面顯示所 以航班信息
修改航班信息功能:管理員登錄成功,可以修改航班信息,修改完畢以后跳轉(zhuǎn)到主頁面顯示所 以航班信息
根據(jù)目的地查詢功能:管理員登錄成功,可以根據(jù)目的地對航班信息進(jìn)行查詢。
根據(jù)起飛時間查詢功能:管理員登錄成功,可以根據(jù)起飛時間對航班信息進(jìn)行查詢。
二:項目邏輯實現(xiàn)
?在實現(xiàn)邏輯之前,先看一下項目演示
航班信息管理系統(tǒng)演示
項目演示完畢,我們分模塊進(jìn)行代碼實現(xiàn)
首先,先看一下項目架構(gòu)
后端
后端工程采用mvc架構(gòu),分別是bean(實體類)dao(處理類)service(服務(wù)類)servlet(啟動類),util(工具類)以及一個jdbc連接數(shù)據(jù)庫的測試類。
前端
前端頁面采用jsp頁面和css樣式
其中在WEB-INF文件夾中創(chuàng)建一個lib目錄并引入三個jar包,右鍵lib目錄點擊 add libary
下面,我們開始寫代碼
首先創(chuàng)建實體類
User
package com.ambow.bean;
public class User {
private int userId;
private String userName;
private String password;
private String realName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
}
Flight
package com.ambow.bean;
import java.util.Date;
public class Flight {
private int idFlight; //航班編號
private int flightId; //航班號
private String destination; //目的地
private Date takeName; //起飛時間
public int getIdFlight() {
return idFlight;
}
public void setIdFlight(int idFlight) {
this.idFlight = idFlight;
}
public int getFlightId() {
return flightId;
}
public void setFlightId(int flightId) {
this.flightId = flightId;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public Date getTakeName() {
return takeName;
}
public void setTakeName(Date takeName) {
this.takeName = takeName;
}
}
實體類寫完,我們開始寫Dao層,寫Dao層的類需要連接數(shù)據(jù)庫
我們先來測試一下數(shù)據(jù)庫是否能連接成功,輸入以下代碼測試數(shù)據(jù)庫連接是否成功
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class jdbcTest {
public static void conn(){
Connection connection;
String url = "jdbc:mysql://localhost:3306/flightinformationmanagesystem"; //數(shù)據(jù)庫連接的url地址
String user = "root"; //數(shù)據(jù)庫用戶名
String password = "123456"; //數(shù)據(jù)庫密碼
try {
//加載數(shù)據(jù)庫驅(qū)動
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("數(shù)據(jù)庫驅(qū)動加載成功!");
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
try{
//通過DriverManager獲取數(shù)據(jù)庫連接
connection = DriverManager.getConnection(url,user,password);
System.out.println("數(shù)據(jù)庫連接成功!");
}catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
jdbcTest.conn();
}
}
輸出
說明數(shù)據(jù)庫連接成功
下面我們開始寫Dao層代碼
首先,創(chuàng)建一個連接數(shù)據(jù)庫的工具類,將數(shù)據(jù)庫連接封裝到一個工具類中
package com.ambow.util;
import java.sql.*;
/*
封裝數(shù)據(jù)庫
*/
public class DBConnection {
private static final String username = "root";
private static final String password = "123456";
private static final String url = "jdbc:mysql://localhost:3306/flightinformationmanagesystem";
//加載數(shù)據(jù)庫驅(qū)動
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
//獲取數(shù)據(jù)庫連接方法
public static Connection getConnection(){
Connection connection = null;
try {
connection = DriverManager.getConnection(url,username,password);
}catch (SQLException e){
e.printStackTrace();
}
return connection;
}
//數(shù)據(jù)庫釋放三個資源
public static void closeAll(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
try{
if (connection != null){
connection.close();
}
if (preparedStatement != null){
preparedStatement.close();
}
if (resultSet != null){
resultSet.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
//數(shù)據(jù)庫釋放兩個資源
public static void closeTwo(Connection connection, PreparedStatement preparedStatement){
try{
if (connection != null){
connection.close();
}
if (preparedStatement != null){
preparedStatement.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
}
當(dāng)連接數(shù)據(jù)庫時,我們調(diào)用工具類進(jìn)行連接
先寫RegisterDao
package com.ambow.dao;
import com.ambow.util.DBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class RegisterDao {
//注冊功能
public boolean register(String username,String password, String realname){
String sql = "insert into flightinformationmanagesystem.user(loginname, password, realname) values (?,?,?)";
Connection connection = null;
PreparedStatement preparedStatement = null;
try{
connection = DBConnection.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
preparedStatement.setString(3,realname);
int resultSet = preparedStatement.executeUpdate();
return resultSet > 0;
}catch (SQLException e){
e.printStackTrace();
}finally {
DBConnection.closeTwo(connection,preparedStatement);
}
return false;
}
}
然后是UserDao
package com.ambow.dao;
import com.ambow.bean.User;
import com.ambow.util.DBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
//登錄功能
public User selectByNameAndPassword(String loginname, String password){
User user = new User();
String sql = "select * from flightinformationmanagesystem.user where loginname=? and password=?";
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try{
//獲取數(shù)據(jù)庫連接對象
connection = DBConnection.getConnection();
//預(yù)編譯
preparedStatement = connection.prepareStatement(sql);
//給參數(shù)賦值
preparedStatement.setString(1,loginname);
preparedStatement.setString(2,password);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()){
//創(chuàng)建用戶對象
user.setUserId(resultSet.getInt(1)); //1代表數(shù)據(jù)庫中的第一列
user.setUserName(resultSet.getString(2)); //2代表第二列
user.setPassword(resultSet.getString(3));
user.setRealName(resultSet.getString(4));
}
}catch (SQLException e){
e.printStackTrace();
}
finally {
DBConnection.closeAll(connection,preparedStatement,resultSet);
}
return user;
}
}
FlightDao
package com.ambow.dao;
import com.ambow.bean.Flight;
import com.ambow.util.DBConnection;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class FlightDao {
// 查找全部航班信息
public static List<Flight> selectAll() {
String sql = "SELECT * FROM flightinformationmanagesystem.flight";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Flight> flightList = new ArrayList<>();
try {
// 獲取數(shù)據(jù)庫連接對象
conn = DBConnection.getConnection();
// 預(yù)編譯并執(zhí)行查詢語句
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
// 處理查詢結(jié)果
while (rs.next()) {
Flight flight = new Flight();
flight.setIdFlight(rs.getInt(1));
flight.setFlightId(rs.getInt(2));
flight.setDestination(rs.getString(3));
flight.setTakeName(rs.getDate(4));
flightList.add(flight);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
// 關(guān)閉資源
DBConnection.closeAll(conn, ps, rs);
}
return flightList;
}
//按目的地查詢航班信息
public static List<Flight> selectAllByDestination(Flight flight){
String sql = "select * from flightinformationmanagesystem.flight where destination=?";
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<Flight> flightList = new ArrayList<>();
try{
//獲取數(shù)據(jù)庫連接對象
connection = DBConnection.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, flight.getDestination());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
flight.setIdFlight(resultSet.getInt("id"));
flight.setFlightId(resultSet.getInt("flightid"));
flight.setDestination(resultSet.getString("destination"));
flight.setTakeName(resultSet.getDate("taketime"));
flightList.add(flight); // 將新對象添加到列表
}
}catch (SQLException e){
e.printStackTrace();
}finally {
DBConnection.closeAll(connection,preparedStatement,resultSet);
}
return flightList;
}
//按起飛時間查詢航班信息
public static List<Flight> selectAllByTaketime(Flight flight){
String sql = "select * from flightinformationmanagesystem.flight where taketime=?";
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<Flight> flightList = new ArrayList<>();
try{
//獲取數(shù)據(jù)庫連接對象
connection = DBConnection.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setDate(1, new java.sql.Date(flight.getTakeName().getTime()));
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
flight.setIdFlight(resultSet.getInt("id"));
flight.setFlightId(resultSet.getInt("flightid"));
flight.setDestination(resultSet.getString("destination"));
flight.setTakeName(resultSet.getDate("taketime"));
flightList.add(flight); // 將新對象添加到列表
}
}catch (SQLException e){
e.printStackTrace();
}finally {
DBConnection.closeAll(connection,preparedStatement,resultSet);
}
return flightList;
}
//按ID查詢航班信息
public Flight selectById(int flightId){
String sql = "select * from flightinformationmanagesystem.flight where id=?";
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Flight flight = null;
try{
//獲取數(shù)據(jù)庫連接對象
connection = DBConnection.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, flightId);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
flight = new Flight();
flight.setFlightId(resultSet.getInt("flightid"));
flight.setDestination(resultSet.getString("destination"));
flight.setTakeName(resultSet.getDate("taketime"));
}
}catch (SQLException e){
e.printStackTrace();
}finally {
DBConnection.closeAll(connection,preparedStatement,resultSet);
}
return flight;
}
//增加航班
public static int add(Flight flight){
String sql = "insert into flightinformationmanagesystem.flight(flightid, destination, taketime) values(?,?,?)";
Connection connection = null;
PreparedStatement preparedStatement = null;
int flag = 0;
try{
//獲取數(shù)據(jù)庫連接對象
connection = DBConnection.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, flight.getFlightId());
preparedStatement.setString(2, flight.getDestination());
preparedStatement.setDate(3,new java.sql.Date(flight.getTakeName().getTime()));
flag = preparedStatement.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}finally {
DBConnection.closeTwo(connection,preparedStatement);
}
return flag;
}
//修改航班
public static int update(Flight flight){
String sql = "update flightinformationmanagesystem.flight SET destination=?,taketime=? where flightid=?";
Connection connection = null;
PreparedStatement preparedStatement = null;
int flag = 0;
try{
connection = DBConnection.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, flight.getDestination());
preparedStatement.setDate(2,new java.sql.Date(flight.getTakeName().getTime()));
preparedStatement.setInt(3, flight.getFlightId());
flag = preparedStatement.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}finally {
DBConnection.closeTwo(connection,preparedStatement);
}
return flag;
}
//刪除航班
public static int delete(Flight flight){
String sql = "delete from flightinformationmanagesystem.flight where id=?";
Connection connection = null;
PreparedStatement preparedStatement = null;
int flag = 0;
try{
connection = DBConnection.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, flight.getIdFlight());
flag = preparedStatement.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}finally {
DBConnection.closeTwo(connection,preparedStatement);
}
return flag;
}
}
下面寫Service層
首先是RegisterService
package com.ambow.service;
import com.ambow.bean.User;
import com.ambow.dao.RegisterDao;
import com.ambow.util.DBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class RegisterService {
//判斷用戶是否存在
public User userIsExist(String username){
String sql = "select loginname from flightinformationmanagesystem.user where loginname =?";
User user = null;
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet = null;
try{
connection = DBConnection.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,username);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
user = new User();
user.setUserName(resultSet.getString("loginname"));
}
}catch (SQLException e){
e.printStackTrace();
}
finally {
DBConnection.closeAll(connection,preparedStatement,resultSet);
}
return user;
}
//調(diào)用RegisterDao類
RegisterDao registerDao = new RegisterDao();
public Boolean Register(String username,String password,String realname){
boolean isSuccess = registerDao.register(username,password,realname);
if (isSuccess){
return isSuccess;
}
return null;
}
}
UserService
package com.ambow.service;
import com.ambow.bean.User;
import com.ambow.dao.UserDao;
import com.ambow.util.DBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserService {
public User userIsExist(String username){
String sql = "select loginname from flightinformationmanagesystem.user where loginname =?";
User user = null;
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet = null;
try{
connection = DBConnection.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,username);
preparedStatement.executeQuery();
while (resultSet.next()){
user = new User();
user.setUserName(resultSet.getString("loginname"));
}
}catch (SQLException e){
e.printStackTrace();
}
finally {
DBConnection.closeAll(connection,preparedStatement,resultSet);
}
return user;
}
//調(diào)用UserDao類
UserDao userDao = new UserDao();
//service層的selectByNameAndPassword方法
public User selectByNameAndPassword(String userName, String Password){
//調(diào)用UserDao類的selectByNameAndPassword方法
User isSuccess = userDao.selectByNameAndPassword(userName,Password);
//如果isSuccess不為空且密碼相等,返回應(yīng)該isSuccess
if (isSuccess != null && Password.equals(isSuccess.getPassword())){
return isSuccess;
}
return null;
}
}
FlightService
package com.ambow.service;
import com.ambow.bean.Flight;
import com.ambow.dao.FlightDao;
import java.util.Date;
import java.util.List;
public class FlightService {
FlightDao flightDao = new FlightDao();
//查詢所有航班信息
public List<Flight> selectAll(){
List<Flight> flightList = FlightDao.selectAll();
return flightList;
}
//通過id查詢航班信息
public Flight selectById(int flightId){
Flight flight = flightDao.selectById(flightId);
return flight;
}
//通過目的地查詢航班信息
public List<Flight> selectByDestination(String destination){
Flight flight =new Flight();
flight.setDestination(destination);
List<Flight> flightList = FlightDao.selectAllByDestination(flight);
return flightList;
}
//通過起飛時間查詢航班信息
public List<Flight> selectByTaketime(Flight flight){
List<Flight> flightList = FlightDao.selectAllByTaketime(flight);
return flightList;
}
//增加航班信息
public boolean add(Flight flight){
int flag = FlightDao.add(flight);
if(flag == 1){
return true;
}else {
return false;
}
}
//修改航班信息
public boolean update(Flight flight){
int flag = FlightDao.update(flight);
if(flag == 1){
return true;
}else {
return false;
}
}
//刪除航班信息
public boolean delete(Flight flight){
int flag = FlightDao.delete(flight);
if(flag == 1){
return true;
}else {
return false;
}
}
}
?然后是servlet層
servlet層代碼較多,我們慢慢來
RegisterServlet
package com.ambow.servlet;
import com.ambow.bean.User;
import com.ambow.service.RegisterService;
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 java.io.IOException;
@WebServlet(name = "RegisterServlet",urlPatterns = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設(shè)置字符編碼,防止亂碼
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//獲取表單信息
String username = request.getParameter("username");
String realname = request.getParameter("fullname");
String password = request.getParameter("password");
//檢查用戶名和密碼是否為空
if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
String scr ="<script>alert('用戶名和密碼不能為空!');window.location.href='register.jsp'</script>";
response.getWriter().write(scr);
}else{
//調(diào)用Register類進(jìn)行注冊
RegisterService registerService = new RegisterService();
//檢查用戶名是否已經(jīng)存在
User userIsExist = registerService.userIsExist(username);
if (userIsExist != null){
String scrs = "<script>alert('用戶名已經(jīng)存在,請選擇其他用戶名!');window.location.href='register.jsp'</script>";
response.getWriter().write(scrs);
}else {
//注冊用戶
Boolean register = registerService.Register(username, password, realname);
if (register){
String scri ="<script>alert('注冊成功!');window.location.href='login.jsp'</script>";
response.getWriter().write(scri);
}else {
String scrip ="<script>alert('注冊失敗!');window.location.href='register.jsp'</script>";
response.getWriter().write(scrip);
}
}
}
}
}
UerServlet
package com.ambow.servlet;
import com.ambow.bean.User;
import com.ambow.service.UserService;
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 java.io.IOException;
@WebServlet(name = "LoginServlet",urlPatterns = "/LoginServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設(shè)置中文字符,防止亂碼
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//提交客戶端信息
String userName = request.getParameter("username");
String password = request.getParameter("password");
// 檢查用戶名和密碼是否為空
if (userName == null || userName.isEmpty() || password == null || password.isEmpty()){
String scr ="<script>alert('用戶名或密碼為空!');window.location.href='login.jsp'</script>";
response.getWriter().write(scr);
}else {
//調(diào)用UserService
UserService userService = new UserService();
User user = userService.selectByNameAndPassword(userName,password);
if(user != null){
// request.setAttribute("user",userName);
// response.sendRedirect("main.jsp");
//設(shè)置登錄成功的標(biāo)志
request.setAttribute("loginSuccessful", true);
request.getSession().setAttribute("user", user); // 將用戶存儲到 session 中,以便在后續(xù)請求中使用
response.sendRedirect(request.getContextPath() + "/FlightServlet"); // 執(zhí)行重定向到 /FlightServlet 路徑
}else {
String scr ="<script>alert('用戶名或密碼錯誤,請仔細(xì)檢查后登錄!');window.location.href='login.jsp'</script>";
response.getWriter().write(scr);
}
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
LoginOutServlet
package com.ambow.servlet;
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;
@WebServlet(name = "LogoutServlet", urlPatterns = "/LogoutServlet")
public class LoginOutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
performLogout(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
performLogout(request,response);
}
private void performLogout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
//清除 session 中的用戶信息
session.removeAttribute("user");
// // 或者直接使整個 session 失效
// session.invalidate();
// 重定向到登錄頁面或其他頁面
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
}
?AddServlet
package com.ambow.servlet;
import com.ambow.bean.Flight;
import com.ambow.service.FlightService;
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 java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@WebServlet(name = "AddFlightServlet",urlPatterns = "/AddFlightServlet")
public class AddFlightServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設(shè)置編碼格式
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//獲取客戶端信息
String flightid = request.getParameter("flightId");
String destination = request.getParameter("destination");
String taketime = request.getParameter("takeoffTime");
//實例化Flight對象并設(shè)置屬性
Flight flight = new Flight();
//將字符串類型轉(zhuǎn)換成整形
flight.setFlightId(Integer.parseInt(flightid));
flight.setDestination(destination);
//將字符串類型轉(zhuǎn)換成Date類型
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date taketimes =null;
try{
taketimes = simpleDateFormat.parse(taketime);
}catch (ParseException e){
e.printStackTrace();
}
flight.setTakeName(taketimes);
//調(diào)用FlightService類
FlightService flightService = new FlightService();
boolean isAddSuccess = flightService.add(flight);
if (isAddSuccess){
// 重定向到航班信息頁面
response.sendRedirect(request.getContextPath() + "/FlightServlet");
}else {
String scr = "<script>alert('航班信息添加失敗,請重試!');window.location.href='AddFlight.jsp'</script>";
response.getWriter().write(scr);
}
// //獲取返回主頁面按鈕
// String btn = request.getParameter("back");
// if ("back".equals(btn)){
// // 重定向到航班信息頁面
// response.sendRedirect(request.getContextPath() + "/FlightServlet");
// }
}
}
?DeleteServlet
package com.ambow.servlet;
import com.ambow.bean.Flight;
import com.ambow.service.FlightService;
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 java.io.IOException;
@WebServlet(name = "DeleteFlightServlet", urlPatterns = "/DeleteFlightServlet")
public class DeleteFlightServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取要刪除的航班id
int idFlight = Integer.parseInt(request.getParameter("flightId"));
//調(diào)用FlightService類中的delete方法進(jìn)行刪除
FlightService flightService = new FlightService();
Flight flight = new Flight();
flight.setIdFlight(idFlight);
boolean isDeleteSuccess = flightService.delete(flight);
if (isDeleteSuccess) {
// 刪除成功,重定向到顯示航班信息的頁面
response.sendRedirect(request.getContextPath() + "/FlightServlet");
} else {
// 刪除失敗,返回錯誤頁面或其他處理
String scr = "<script>alert('航班信息刪除失敗,請重試!');window.location.href='main.jsp'</script>";
response.getWriter().write(scr);
}
}
}
?EditFlightServlet
package com.ambow.servlet;
import com.ambow.bean.Flight;
import com.ambow.service.FlightService;
import javax.servlet.RequestDispatcher;
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 java.io.IOException;
import java.util.List;
@WebServlet(name = "EditFlightServlet", urlPatterns = "/EditFlightServlet")
public class EditFlightServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取用戶表單信息
String flightIdParam = request.getParameter("id");
if (flightIdParam != null && !flightIdParam.isEmpty()) {
int Idflight = Integer.parseInt(flightIdParam);
//查詢數(shù)據(jù)庫,獲取航班信息
FlightService flightService = new FlightService();
Flight flights = flightService.selectById(Idflight);
//將航班信息存儲到 request 屬性中
request.setAttribute("flights", flights);
request.getRequestDispatcher("UpdateFlight.jsp").forward(request, response);
}else {
response.sendRedirect(request.getContextPath() + "/FlightServlet");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
FlightServlet
package com.ambow.servlet;
import com.ambow.bean.Flight;
import com.ambow.bean.User;
import com.ambow.service.FlightService;
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 java.io.IOException;
import java.util.List;
@WebServlet("/FlightServlet")
public class FlightServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
//創(chuàng)建service對象,調(diào)用查詢所有的方法
FlightService fs = new FlightService();
//調(diào)用查詢所有的方法
List<Flight> flights = fs.selectAll();
//將信息儲存到作用域中
request.setAttribute("flights",flights);
//轉(zhuǎn)發(fā)到FlightInfo.jsp頁面
request.getRequestDispatcher("main.jsp").forward(request, response);
}
}
?SelectByDestinationServlet
package com.ambow.servlet;
import com.ambow.bean.Flight;
import com.ambow.service.FlightService;
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 java.io.IOException;
import java.util.List;
@WebServlet(name = "SelectByDestination",urlPatterns = "/SelectByDestination")
public class SelectByDestinationServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設(shè)置編碼格式
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
// 獲取客戶端信息
String destination = request.getParameter("destination");
// 調(diào)用 FlightService 類
FlightService flightService = new FlightService();
List<Flight> flights = flightService.selectByDestination(destination);
if (flights != null && !flights.isEmpty()) {
// 將信息儲存到作用域中
request.setAttribute("flights", flights);
// 轉(zhuǎn)發(fā)到 main.jsp 頁面
request.getRequestDispatcher("main.jsp").forward(request, response);
} else {
// 目的地找不到的處理邏輯
String scr = "<script>alert('該目的地找不到!');window.location.href='SelectByDestination.jsp'</script>";
response.getWriter().write(scr);
}
}
}
?SelectByTaketimeServlet
package com.ambow.servlet;
import com.ambow.bean.Flight;
import com.ambow.service.FlightService;
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 java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@WebServlet(name = "SelectByTaketime",urlPatterns = "/SelectByTaketime")
public class SelectByTaketimeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設(shè)置編碼格式
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
// 獲取客戶端信息
String taketime = request.getParameter("taketime");
//實例化Flight對象并設(shè)置屬性
Flight flight = new Flight();
//將字符串類型轉(zhuǎn)換成Date類型
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date taketimes =null;
try{
taketimes = simpleDateFormat.parse(taketime);
}catch (ParseException e){
e.printStackTrace();
}
flight.setTakeName(taketimes);
// 調(diào)用 FlightService 類
FlightService flightService = new FlightService();
List<Flight> flights = flightService.selectByTaketime(flight);
if (flights != null && !flights.isEmpty()) {
// 將信息儲存到作用域中
request.setAttribute("flights", flights);
// 轉(zhuǎn)發(fā)到 main.jsp 頁面
request.getRequestDispatcher("main.jsp").forward(request, response);
} else {
// 目的地找不到的處理邏輯
String scr = "<script>alert('該起飛時間找不到!');window.location.href='SelectByTaketime.jsp'</script>";
response.getWriter().write(scr);
}
}
}
?UpdateFlightServlet
package com.ambow.servlet;
import com.ambow.bean.Flight;
import com.ambow.service.FlightService;
import com.sun.xml.internal.ws.api.ha.HaInfo;
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 java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@WebServlet(name = "UpdateFlightServlet",urlPatterns = "/UpdateFlightServlet")
public class UpdateFlightServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設(shè)置編碼格式
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//獲取客戶端信息
String flightid = request.getParameter("flightId");
String destination = request.getParameter("destination");
String taketime = request.getParameter("takeoffTime");
//調(diào)用Flight類
Flight flight = new Flight();
flight.setFlightId(Integer.parseInt(flightid));
flight.setDestination(destination);
//將字符串類型轉(zhuǎn)換成Date類型
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date taketimes =null;
try{
taketimes = simpleDateFormat.parse(taketime);
}catch (ParseException e){
e.printStackTrace();
}
flight.setTakeName(taketimes);
//調(diào)用FlightService類中的update方法
FlightService flightService =new FlightService();
boolean isUpdateSuccess = flightService.update(flight);
if (isUpdateSuccess){
// 重定向到航班信息頁面
response.sendRedirect(request.getContextPath() + "/FlightServlet");
}else {
String scr = "<script>alert('航班信息修改失敗,請重試!');window.location.href='UpdateFlight.jsp'</script>";
response.getWriter().write(scr);
}
}
}
三:前端JSP頁面
JSP是原生的HTML頁面添入Java代碼
下面開始編寫JSP代碼
AddFlight.jsp
<%--
Created by IntelliJ IDEA.
User: 29988
Date: 2024/1/4
Time: 14:17
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>增加航班信息</title>
<style type="text/css">
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
display: flex;
}
.sidebar {
background-color: #333;
color: #fff;
width: 200px;
padding: 20px;
box-sizing: border-box;
}
.nav-link {
text-decoration: none;
color: #fff;
font-family: 微軟雅黑, serif;
display: block;
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
transition: background-color 0.3s;
}
.nav-link:hover {
background-color: #555;
}
.logout-link {
margin-top: 20px;
position: fixed;
right: 3%;
top: 3%;
}
.loginout{
text-decoration: none;
color: #4caf50;
}
.user{
margin-top: 20px;
position: fixed;
right: 8%;
top: 3%;
}
.content {
margin-top: 100px;
margin-left: 450px;
padding: 20px;
}
.form-container {
width: 400px;
margin: 20px;
padding: 20px;
box-sizing: border-box;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
.form-group input {
width: 100%;
padding: 8px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 4px;
}
.form-group button {
background-color: #4caf50;
color: #fff;
padding: 10px;
margin: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
}
.form-group button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="sidebar">
<h2>系統(tǒng)</h2>
<a class="nav-link" onclick="redirectToMainPage()">主頁</a>
<a href="SelectByDestination.jsp" class="nav-link">按目的地查詢</a>
<a href="SelectByTaketime.jsp" class="nav-link">按起飛時間查詢</a>
</div>
<c:if test="${not empty sessionScope.user}">
<div class="user">
<p><span>你好, ${sessionScope.user.userName}</span></p>
</div>
<div class="logout-link">
<p><a href="LogoutServlet" class="loginout">注銷</a></p>
</div>
</c:if>
<c:if test="${empty sessionScope.user}">
<div>
<p><a href="login.jsp">登錄</a></p>
</div>
<div>
<p><a href="register.jsp">注冊</a></p>
</div>
</c:if>
<div class="content">
<div class="form-container">
<h2>新增航班信息</h2>
<form action="AddFlightServlet" method="post">
<div class="form-group">
<label for="flightId">航班號:</label>
<input type="text" id="flightId" name="flightId" required>
</div>
<div class="form-group">
<label for="destination">目的地:</label>
<input type="text" id="destination" name="destination" required>
</div>
<div class="form-group">
<label for="takeoffTime">起飛時間:</label>
<input type="date" id="takeoffTime" name="takeoffTime" required>
</div>
<div class="form-group">
<button type="submit">保存</button>
<button type="button" onclick="redirectToMainPage()">返回主頁面</button>
</div>
</form>
</div>
</div>
<script>
function redirectToMainPage() {
window.location.href = "${pageContext.request.contextPath}/FlightServlet";
}
</script>
</body>
</html>
login.jsp
<%--
Created by IntelliJ IDEA.
User: 29988
Date: 2024/1/2
Time: 8:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<style type="text/css">
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.login-container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 400px;
height:450px;
}
.login-container h2 {
text-align: center;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
.form-group input {
width: 100%;
padding: 8px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 4px;
}
.form-group button {
background-color: #4caf50;
color: #fff;
padding: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
}
.form-group button:hover {
background-color: #45a049;
}
a{
text-decoration: none;
color: #fff;
}
.h2s{
position: fixed;
top: 7%;
left: 42%;
font-family: 幼圓, serif ;
font-size: 35px;
}
</style>
<head>
<title>登錄</title>
<link rel="shortcut icon" href="#"/>
</head>
<body>
<h2 class="h2s">航班信息管理系統(tǒng)</h2>
<div class="login-container">
<h2>用戶登錄</h2>
<c:if test="${not empty loginSuccessful}">
<c:redirect url="/FlightServlet"/>
</c:if>
<form action="LoginServlet" method="post">
<div class="form-group">
<label for="username">用戶名:</label>
<input type="text" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">密碼:</label>
<input type="password" id="password" name="password" required>
</div>
<div class="form-group">
<button type="submit">登錄</button>
</div>
<div class="form-group">
<button><a href="register.jsp">注冊</a></button>
</div>
</form>
</div>
</body>
</html>
?main.jsp
<%--
Created by IntelliJ IDEA.
User: 29988
Date: 2024/1/2
Time: 19:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>航班信息管理系統(tǒng)</title>
<link rel="shortcut icon" href="#"/>
<style type="text/css">
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
display: flex;
}
.sidebar {
background-color: #333;
color: #fff;
width: 200px;
padding: 20px;
box-sizing: border-box;
}
.nav-link {
text-decoration: none;
color: #fff;
font-family: 微軟雅黑, serif;
display: block;
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
transition: background-color 0.3s;
}
.nav-link:hover {
background-color: #555;
}
.main-content {
flex: 1;
padding: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #333;
color: #fff;
}
.logout-link {
margin-top: 20px;
position: fixed;
right: 3%;
top: 3%;
}
.loginout{
text-decoration: none;
color: #4caf50;
}
.user{
margin-top: 20px;
position: fixed;
right: 8%;
top: 3%;
}
.update{
text-decoration: none;
color: black;
}
</style>
</head>
<body>
<div class="sidebar">
<h2>系統(tǒng)</h2>
<a class="nav-link" onclick="redirectToMainPage()">主頁</a>
<a href="AddFlight.jsp" class="nav-link">新增</a>
<a href="SelectByDestination.jsp" class="nav-link">按目的地查詢</a>
<a href="SelectByTaketime.jsp" class="nav-link">按起飛時間查詢</a>
</div>
<c:if test="${not empty sessionScope.user}">
<div class="user">
<p><span>你好, ${sessionScope.user.userName}</span></p>
</div>
<div class="logout-link">
<p><a href="LogoutServlet" class="loginout">注銷</a></p>
</div>
</c:if>
<c:if test="${empty sessionScope.user}">
<div>
<p><a href="login.jsp">登錄</a></p>
</div>
<div>
<p><a href="register.jsp">注冊</a></p>
</div>
</c:if>
<div class="main-content">
<h1>航班信息管理系統(tǒng)</h1>
<table>
<thead>
<tr>
<th>編號</th>
<th>航班號</th>
<th>目的地</th>
<th>起飛時間</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="flights" items="${flights}">
<tr>
<td>${flights.idFlight}</td>
<td>${flights.flightId}</td>
<td>${flights.destination}</td>
<td>${flights.takeName}</td>
<td>
<div style="display: inline-block;">
<input type="hidden" name="flightId" value="${flights.idFlight}">
<button type="button"><a href="EditFlightServlet?id=${flights.idFlight}" class="update" onclick="return confirm('是否修改?')">修改</a></button>
</div>
<div style="display: inline-block;">
<form action="DeleteFlightServlet" method="post">
<input type="hidden" name="flightId" value="${flights.idFlight}">
<button type="submit" onclick="return confirm('是否刪除?')">刪除</button>
</form>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<script>
function redirectToMainPage() {
window.location.href = "${pageContext.request.contextPath}/FlightServlet";
}
</script>
</body>
</html>
register.jsp
<%--
Created by IntelliJ IDEA.
User: 29988
Date: 2024/1/2
Time: 20:59
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注冊</title>
</head>
<style type="text/css">
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.form-container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 400px;
}
.form-container h2 {
text-align: center;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
.form-group input {
width: 100%;
padding: 8px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 4px;
}
.form-group button {
background-color: #4caf50;
color: #fff;
padding: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
}
.form-group button:hover {
background-color: #45a049;
}
.form-group .login-link {
text-align: center;
margin-top: 10px;
}
.h2s{
position: fixed;
top: 7%;
left: 42%;
font-family: 幼圓, serif ;
font-size: 35px;
}
</style>
<body>
<h2 class="h2s">航班信息管理系統(tǒng)</h2>
<div class="form-container">
<h2>用戶注冊</h2>
<form action="RegisterServlet" method="post">
<div class="form-group">
<label for="username">用戶名:</label>
<input type="text" id="username" name="username" required>
</div>
<div class="form-group">
<label for="fullname">真實姓名:</label>
<input type="text" id="fullname" name="fullname" required>
</div>
<div class="form-group">
<label for="password">密碼:</label>
<input type="password" id="password" name="password" required>
</div>
<div class="form-group">
<button type="submit">注冊</button>
</div>
<div class="form-group login-link">
<p>已經(jīng)有賬號? <a href="login.jsp">點擊登錄</a></p>
</div>
</form>
</div>
</body>
</html>
SelectByDestination.jsp
<%--
Created by IntelliJ IDEA.
User: 29988
Date: 2024/1/5
Time: 9:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>通過目的地查詢</title>
<style type="text/css">
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
display: flex;
}
.sidebar {
background-color: #333;
color: #fff;
width: 200px;
padding: 20px;
box-sizing: border-box;
}
.nav-link {
text-decoration: none;
color: #fff;
font-family: 微軟雅黑, serif;
display: block;
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
transition: background-color 0.3s;
}
.nav-link:hover {
background-color: #555;
}
.logout-link {
margin-top: 20px;
position: fixed;
right: 3%;
top: 3%;
}
.loginout{
text-decoration: none;
color: #4caf50;
}
.user{
margin-top: 20px;
position: fixed;
right: 8%;
top: 3%;
}
.form-container {
background-color: #fff;
padding: 20px;
border-radius: 4px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
height: 350px;
width: 400px;
margin-left: 450px;
margin-top: 200px;
}
.form-container h2 {
margin-bottom: 20px;
}
.form-container label {
display: block;
margin-bottom: 10px;
}
.form-container input {
width: 100%;
padding: 10px;
margin-bottom: 20px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 4px;
}
.form-container button {
background-color: #4caf50;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
.form-container button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="sidebar">
<h2>系統(tǒng)</h2>
<a class="nav-link" onclick="redirectToMainPage()">主頁</a>
<a href="AddFlight.jsp" class="nav-link">新增</a>
<a href="SelectByTaketime.jsp" class="nav-link">按起飛時間查詢</a>
</div>
<c:if test="${not empty sessionScope.user}">
<div class="user">
<p><span>你好, ${sessionScope.user.userName}</span></p>
</div>
<div class="logout-link">
<p><a href="LogoutServlet" class="loginout">注銷</a></p>
</div>
</c:if>
<div class="form-container">
<h2>按目的地查詢</h2>
<form action="SelectByDestination" method="post">
<label for="destination">目的地:</label>
<input type="text" id="destination" name="destination" required>
<br>
<button type="submit">查詢</button>
</form>
</div>
<script>
function redirectToMainPage() {
window.location.href = "${pageContext.request.contextPath}/FlightServlet";
}
</script>
</body>
</html>
SelectByTaketime.jsp
<%--
Created by IntelliJ IDEA.
User: 29988
Date: 2024/1/5
Time: 11:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>通過起飛時間查詢</title>
<style type="text/css">
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
display: flex;
}
.sidebar {
background-color: #333;
color: #fff;
width: 200px;
padding: 20px;
box-sizing: border-box;
}
.nav-link {
text-decoration: none;
color: #fff;
font-family: 微軟雅黑, serif;
display: block;
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
transition: background-color 0.3s;
}
.nav-link:hover {
background-color: #555;
}
.logout-link {
margin-top: 20px;
position: fixed;
right: 3%;
top: 3%;
}
.loginout{
text-decoration: none;
color: #4caf50;
}
.user{
margin-top: 20px;
position: fixed;
right: 8%;
top: 3%;
}
.form-container {
background-color: #fff;
padding: 20px;
border-radius: 4px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
height: 350px;
width: 400px;
margin-left: 450px;
margin-top: 200px;
}
.form-container h2 {
margin-bottom: 20px;
}
.form-container label {
display: block;
margin-bottom: 10px;
}
.form-container input {
width: 100%;
padding: 10px;
margin-bottom: 20px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 4px;
}
.form-container button {
background-color: #4caf50;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
.form-container button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="sidebar">
<h2>系統(tǒng)</h2>
<a class="nav-link" onclick="redirectToMainPage()">主頁</a>
<a href="AddFlight.jsp" class="nav-link">新增</a>
<a href="SelectByDestination.jsp" class="nav-link">按目的地查詢</a>
</div>
<c:if test="${not empty sessionScope.user}">
<div class="user">
<p><span>你好, ${sessionScope.user.userName}</span></p>
</div>
<div class="logout-link">
<p><a href="LogoutServlet" class="loginout">注銷</a></p>
</div>
</c:if>
<div class="form-container">
<h2>按起飛時間查詢</h2>
<form action="SelectByTaketime" method="post">
<label for="taketime">起飛時間:</label>
<input type="date" id="taketime" name="taketime" required>
<br>
<button type="submit">查詢</button>
</form>
</div>
<script>
function redirectToMainPage() {
window.location.href = "${pageContext.request.contextPath}/FlightServlet";
}
</script>
</body>
</html>
UpdateFlight.jsp
<%--
Created by IntelliJ IDEA.
User: 29988
Date: 2024/1/4
Time: 16:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>修改航班信息</title>
<style type="text/css">
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
display: flex;
}
.sidebar {
background-color: #333;
color: #fff;
width: 200px;
padding: 20px;
box-sizing: border-box;
}
.nav-link {
text-decoration: none;
color: #fff;
font-family: 微軟雅黑, serif;
display: block;
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
transition: background-color 0.3s;
}
.nav-link:hover {
background-color: #555;
}
.logout-link {
margin-top: 20px;
position: fixed;
right: 3%;
top: 3%;
}
.loginout{
text-decoration: none;
color: #4caf50;
}
.user{
margin-top: 20px;
position: fixed;
right: 8%;
top: 3%;
}
.content {
margin-left: 450px;
margin-top: 150px;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 350px;
height: 450px;
text-align: center;
}
form {
margin-top: 20px;
}
label {
display: block;
margin-bottom: 8px;
margin-left: 0;
}
input {
width: 100%;
padding: 8px;
margin-bottom: 12px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
background-color: #4caf50;
color: #fff;
padding: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="sidebar">
<h2>系統(tǒng)</h2>
<a class="nav-link" onclick="redirectToMainPage()">主頁</a>
<a href="AddFlight.jsp" class="nav-link">新增</a>
<a href="SelectByDestination.jsp" class="nav-link">按目的地查詢</a>
<a href="SelectByTaketime.jsp" class="nav-link">按起飛時間查詢</a>
</div>
<c:if test="${not empty sessionScope.user}">
<div class="user">
<p><span>你好, ${sessionScope.user.userName}</span></p>
</div>
<div class="logout-link">
<p><a href="LogoutServlet" class="loginout">注銷</a></p>
</div>
</c:if>
<c:if test="${empty sessionScope.user}">
<div>
<p><a href="login.jsp">登錄</a></p>
</div>
<div>
<p><a href="register.jsp">注冊</a></p>
</div>
</c:if>
<div class="content">
<h2 align="left">編輯航班信息</h2>
<form action="UpdateFlightServlet" method="post">
<input type="hidden" name="idFlight" value="${flights.idFlight}">
<label for="flightId">航班號:</label>
<input type="text" id="flightId" name="flightId" value="${flights.flightId}" required>
<br>
<label for="destination">目的地:</label>
<input type="text" id="destination" name="destination" value="${flights.destination}" required>
<br>
<label for="takeoffTime">起飛時間:</label>
<input type="text" id="takeoffTime" name="takeoffTime" value="${flights.takeName}" required>
<br>
<button type="submit">保存修改</button>
<button type="button" onclick="redirectToMainPage()">返回主頁面</button>
</form>
</div>
<script>
function redirectToMainPage() {
window.location.href = "${pageContext.request.contextPath}/FlightServlet";
}
</script>
</body>
</html>
四:SQL語句
根據(jù)大家的需求,sql語句代碼放在下面
本項目一共使用兩個表,一個是user表,用來儲存用戶,一個flight表,用來存儲航班信息
首先是user表
CREATE TABLE `user` (
`userid` int NOT NULL AUTO_INCREMENT,
`loginname` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`password` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`realname` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`userid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 16 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
然后是flight表
CREATE TABLE `flight` (
`id` int NOT NULL AUTO_INCREMENT,
`flightid` int NOT NULL,
`destination` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`taketime` date NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 27 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
?項目到此基本完成了,如果中間有問題請在評論區(qū)評論!文章來源:http://www.zghlxwxcb.cn/news/detail-848784.html
項目中涉及到的jar包我放在百度網(wǎng)盤了,有需要的大家自行提取
https://pan.baidu.com/s/1LHeAnZ6TsExQCYKEMNqOlg
提取碼:zoyc文章來源地址http://www.zghlxwxcb.cn/news/detail-848784.html
到了這里,關(guān)于JavaWeb項目:航班信息管理系統(tǒng)(tomcat+jsp)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!