当前位置:   article > 正文

JavaWeb项目:航班信息管理系统(tomcat+jsp)_javaweb航空管理系统

javaweb航空管理系统

一:项目业务需求

航班信息管理系统是学习Javaweb的一个小项目,首先对该项目的业务需求进行分析,根据项目文档知它的主要实现技术为 SERVLET、JSP、MVC 架构、JDBC 和 MySQL。该项目着重学生的实际应用场景来设计,模拟 机场中的航班系统的业务实现以及扩展,能够实现航班信息管理的的所有功能,包括航班信息,航 班目的地查询等功能的实现;

本项目共实现八大功能模块

  注册模块:用户输入用户名,真实姓名,密码进行注册,注册验证通过后,将用户存储到数据库中,如果数据库中已有相同用户名,则需要重新注册。

登录模块:对管理员输入的用户名,密码进行验证,验证通过后,管理员可以使用航班信息管 理系统中所有权限的功能,否则重新登录该系统。

航班信息管理功能:管理员登录成功,可以查询所有航班信息。 .

新增航班信息功能:管理员登录成功,可以新增航班信息,新增完毕以后跳转到主页面显示所

以航班信息

删除航班信息功能:管理员登录成功,可以删除航班信息,删除完毕以后跳转到主页面显示所 以航班信息

修改航班信息功能:管理员登录成功,可以修改航班信息,修改完毕以后跳转到主页面显示所 以航班信息

根据目的地查询功能:管理员登录成功,可以根据目的地对航班信息进行查询。

根据起飞时间查询功能:管理员登录成功,可以根据起飞时间对航班信息进行查询。

二:项目逻辑实现

 在实现逻辑之前,先看一下项目演示

航班信息管理系统演示

项目演示完毕,我们分模块进行代码实现

首先,先看一下项目架构

后端

后端工程采用mvc架构,分别是bean(实体类)dao(处理类)service(服务类)servlet(启动类),util(工具类)以及一个jdbc连接数据库的测试类。

前端

前端页面采用jsp页面和css样式

其中在WEB-INF文件夹中创建一个lib目录并引入三个jar包,右键lib目录点击 add libary

下面,我们开始写代码

首先创建实体类

User

  1. package com.ambow.bean;
  2. public class User {
  3. private int userId;
  4. private String userName;
  5. private String password;
  6. private String realName;
  7. public int getUserId() {
  8. return userId;
  9. }
  10. public void setUserId(int userId) {
  11. this.userId = userId;
  12. }
  13. public String getUserName() {
  14. return userName;
  15. }
  16. public void setUserName(String userName) {
  17. this.userName = userName;
  18. }
  19. public String getPassword() {
  20. return password;
  21. }
  22. public void setPassword(String password) {
  23. this.password = password;
  24. }
  25. public String getRealName() {
  26. return realName;
  27. }
  28. public void setRealName(String realName) {
  29. this.realName = realName;
  30. }
  31. }

Flight

  1. package com.ambow.bean;
  2. import java.util.Date;
  3. public class Flight {
  4. private int idFlight; //航班编号
  5. private int flightId; //航班号
  6. private String destination; //目的地
  7. private Date takeName; //起飞时间
  8. public int getIdFlight() {
  9. return idFlight;
  10. }
  11. public void setIdFlight(int idFlight) {
  12. this.idFlight = idFlight;
  13. }
  14. public int getFlightId() {
  15. return flightId;
  16. }
  17. public void setFlightId(int flightId) {
  18. this.flightId = flightId;
  19. }
  20. public String getDestination() {
  21. return destination;
  22. }
  23. public void setDestination(String destination) {
  24. this.destination = destination;
  25. }
  26. public Date getTakeName() {
  27. return takeName;
  28. }
  29. public void setTakeName(Date takeName) {
  30. this.takeName = takeName;
  31. }
  32. }

实体类写完,我们开始写Dao层,写Dao层的类需要连接数据库

我们先来测试一下数据库是否能连接成功,输入以下代码测试数据库连接是否成功

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. public class jdbcTest {
  5. public static void conn(){
  6. Connection connection;
  7. String url = "jdbc:mysql://localhost:3306/flightinformationmanagesystem"; //数据库连接的url地址
  8. String user = "root"; //数据库用户名
  9. String password = "123456"; //数据库密码
  10. try {
  11. //加载数据库驱动
  12. Class.forName("com.mysql.cj.jdbc.Driver");
  13. System.out.println("数据库驱动加载成功!");
  14. }catch (ClassNotFoundException e) {
  15. e.printStackTrace();
  16. }
  17. try{
  18. //通过DriverManager获取数据库连接
  19. connection = DriverManager.getConnection(url,user,password);
  20. System.out.println("数据库连接成功!");
  21. }catch (SQLException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. public static void main(String[] args) {
  26. jdbcTest.conn();
  27. }
  28. }

输出

说明数据库连接成功

下面我们开始写Dao层代码

首先,创建一个连接数据库的工具类,将数据库连接封装到一个工具类中

  1. package com.ambow.util;
  2. import java.sql.*;
  3. /*
  4. 封装数据库
  5. */
  6. public class DBConnection {
  7. private static final String username = "root";
  8. private static final String password = "123456";
  9. private static final String url = "jdbc:mysql://localhost:3306/flightinformationmanagesystem";
  10. //加载数据库驱动
  11. public static void main(String[] args) {
  12. try{
  13. Class.forName("com.mysql.jdbc.Driver");
  14. }catch (ClassNotFoundException e){
  15. e.printStackTrace();
  16. }
  17. }
  18. //获取数据库连接方法
  19. public static Connection getConnection(){
  20. Connection connection = null;
  21. try {
  22. connection = DriverManager.getConnection(url,username,password);
  23. }catch (SQLException e){
  24. e.printStackTrace();
  25. }
  26. return connection;
  27. }
  28. //数据库释放三个资源
  29. public static void closeAll(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
  30. try{
  31. if (connection != null){
  32. connection.close();
  33. }
  34. if (preparedStatement != null){
  35. preparedStatement.close();
  36. }
  37. if (resultSet != null){
  38. resultSet.close();
  39. }
  40. }catch (SQLException e){
  41. e.printStackTrace();
  42. }
  43. }
  44. //数据库释放两个资源
  45. public static void closeTwo(Connection connection, PreparedStatement preparedStatement){
  46. try{
  47. if (connection != null){
  48. connection.close();
  49. }
  50. if (preparedStatement != null){
  51. preparedStatement.close();
  52. }
  53. }catch (SQLException e){
  54. e.printStackTrace();
  55. }
  56. }
  57. }

当连接数据库时,我们调用工具类进行连接

先写RegisterDao

  1. package com.ambow.dao;
  2. import com.ambow.util.DBConnection;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.SQLException;
  6. public class RegisterDao {
  7. //注册功能
  8. public boolean register(String username,String password, String realname){
  9. String sql = "insert into flightinformationmanagesystem.user(loginname, password, realname) values (?,?,?)";
  10. Connection connection = null;
  11. PreparedStatement preparedStatement = null;
  12. try{
  13. connection = DBConnection.getConnection();
  14. preparedStatement = connection.prepareStatement(sql);
  15. preparedStatement.setString(1,username);
  16. preparedStatement.setString(2,password);
  17. preparedStatement.setString(3,realname);
  18. int resultSet = preparedStatement.executeUpdate();
  19. return resultSet > 0;
  20. }catch (SQLException e){
  21. e.printStackTrace();
  22. }finally {
  23. DBConnection.closeTwo(connection,preparedStatement);
  24. }
  25. return false;
  26. }
  27. }

然后是UserDao

  1. package com.ambow.dao;
  2. import com.ambow.bean.User;
  3. import com.ambow.util.DBConnection;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. public class UserDao {
  9. //登录功能
  10. public User selectByNameAndPassword(String loginname, String password){
  11. User user = new User();
  12. String sql = "select * from flightinformationmanagesystem.user where loginname=? and password=?";
  13. Connection connection = null;
  14. PreparedStatement preparedStatement = null;
  15. ResultSet resultSet = null;
  16. try{
  17. //获取数据库连接对象
  18. connection = DBConnection.getConnection();
  19. //预编译
  20. preparedStatement = connection.prepareStatement(sql);
  21. //给参数赋值
  22. preparedStatement.setString(1,loginname);
  23. preparedStatement.setString(2,password);
  24. resultSet = preparedStatement.executeQuery();
  25. if (resultSet.next()){
  26. //创建用户对象
  27. user.setUserId(resultSet.getInt(1)); //1代表数据库中的第一列
  28. user.setUserName(resultSet.getString(2)); //2代表第二列
  29. user.setPassword(resultSet.getString(3));
  30. user.setRealName(resultSet.getString(4));
  31. }
  32. }catch (SQLException e){
  33. e.printStackTrace();
  34. }
  35. finally {
  36. DBConnection.closeAll(connection,preparedStatement,resultSet);
  37. }
  38. return user;
  39. }
  40. }

FlightDao

  1. package com.ambow.dao;
  2. import com.ambow.bean.Flight;
  3. import com.ambow.util.DBConnection;
  4. import java.sql.*;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class FlightDao {
  8. // 查找全部航班信息
  9. public static List<Flight> selectAll() {
  10. String sql = "SELECT * FROM flightinformationmanagesystem.flight";
  11. Connection conn = null;
  12. PreparedStatement ps = null;
  13. ResultSet rs = null;
  14. List<Flight> flightList = new ArrayList<>();
  15. try {
  16. // 获取数据库连接对象
  17. conn = DBConnection.getConnection();
  18. // 预编译并执行查询语句
  19. ps = conn.prepareStatement(sql);
  20. rs = ps.executeQuery();
  21. // 处理查询结果
  22. while (rs.next()) {
  23. Flight flight = new Flight();
  24. flight.setIdFlight(rs.getInt(1));
  25. flight.setFlightId(rs.getInt(2));
  26. flight.setDestination(rs.getString(3));
  27. flight.setTakeName(rs.getDate(4));
  28. flightList.add(flight);
  29. }
  30. } catch (SQLException e) {
  31. throw new RuntimeException(e);
  32. } finally {
  33. // 关闭资源
  34. DBConnection.closeAll(conn, ps, rs);
  35. }
  36. return flightList;
  37. }
  38. //按目的地查询航班信息
  39. public static List<Flight> selectAllByDestination(Flight flight){
  40. String sql = "select * from flightinformationmanagesystem.flight where destination=?";
  41. Connection connection = null;
  42. PreparedStatement preparedStatement = null;
  43. ResultSet resultSet = null;
  44. List<Flight> flightList = new ArrayList<>();
  45. try{
  46. //获取数据库连接对象
  47. connection = DBConnection.getConnection();
  48. preparedStatement = connection.prepareStatement(sql);
  49. preparedStatement.setString(1, flight.getDestination());
  50. resultSet = preparedStatement.executeQuery();
  51. while (resultSet.next()){
  52. flight.setIdFlight(resultSet.getInt("id"));
  53. flight.setFlightId(resultSet.getInt("flightid"));
  54. flight.setDestination(resultSet.getString("destination"));
  55. flight.setTakeName(resultSet.getDate("taketime"));
  56. flightList.add(flight); // 将新对象添加到列表
  57. }
  58. }catch (SQLException e){
  59. e.printStackTrace();
  60. }finally {
  61. DBConnection.closeAll(connection,preparedStatement,resultSet);
  62. }
  63. return flightList;
  64. }
  65. //按起飞时间查询航班信息
  66. public static List<Flight> selectAllByTaketime(Flight flight){
  67. String sql = "select * from flightinformationmanagesystem.flight where taketime=?";
  68. Connection connection = null;
  69. PreparedStatement preparedStatement = null;
  70. ResultSet resultSet = null;
  71. List<Flight> flightList = new ArrayList<>();
  72. try{
  73. //获取数据库连接对象
  74. connection = DBConnection.getConnection();
  75. preparedStatement = connection.prepareStatement(sql);
  76. preparedStatement.setDate(1, new java.sql.Date(flight.getTakeName().getTime()));
  77. resultSet = preparedStatement.executeQuery();
  78. while (resultSet.next()){
  79. flight.setIdFlight(resultSet.getInt("id"));
  80. flight.setFlightId(resultSet.getInt("flightid"));
  81. flight.setDestination(resultSet.getString("destination"));
  82. flight.setTakeName(resultSet.getDate("taketime"));
  83. flightList.add(flight); // 将新对象添加到列表
  84. }
  85. }catch (SQLException e){
  86. e.printStackTrace();
  87. }finally {
  88. DBConnection.closeAll(connection,preparedStatement,resultSet);
  89. }
  90. return flightList;
  91. }
  92. //按ID查询航班信息
  93. public Flight selectById(int flightId){
  94. String sql = "select * from flightinformationmanagesystem.flight where id=?";
  95. Connection connection = null;
  96. PreparedStatement preparedStatement = null;
  97. ResultSet resultSet = null;
  98. Flight flight = null;
  99. try{
  100. //获取数据库连接对象
  101. connection = DBConnection.getConnection();
  102. preparedStatement = connection.prepareStatement(sql);
  103. preparedStatement.setInt(1, flightId);
  104. resultSet = preparedStatement.executeQuery();
  105. while (resultSet.next()){
  106. flight = new Flight();
  107. flight.setFlightId(resultSet.getInt("flightid"));
  108. flight.setDestination(resultSet.getString("destination"));
  109. flight.setTakeName(resultSet.getDate("taketime"));
  110. }
  111. }catch (SQLException e){
  112. e.printStackTrace();
  113. }finally {
  114. DBConnection.closeAll(connection,preparedStatement,resultSet);
  115. }
  116. return flight;
  117. }
  118. //增加航班
  119. public static int add(Flight flight){
  120. String sql = "insert into flightinformationmanagesystem.flight(flightid, destination, taketime) values(?,?,?)";
  121. Connection connection = null;
  122. PreparedStatement preparedStatement = null;
  123. int flag = 0;
  124. try{
  125. //获取数据库连接对象
  126. connection = DBConnection.getConnection();
  127. preparedStatement = connection.prepareStatement(sql);
  128. preparedStatement.setInt(1, flight.getFlightId());
  129. preparedStatement.setString(2, flight.getDestination());
  130. preparedStatement.setDate(3,new java.sql.Date(flight.getTakeName().getTime()));
  131. flag = preparedStatement.executeUpdate();
  132. }catch (SQLException e){
  133. e.printStackTrace();
  134. }finally {
  135. DBConnection.closeTwo(connection,preparedStatement);
  136. }
  137. return flag;
  138. }
  139. //修改航班
  140. public static int update(Flight flight){
  141. String sql = "update flightinformationmanagesystem.flight SET destination=?,taketime=? where flightid=?";
  142. Connection connection = null;
  143. PreparedStatement preparedStatement = null;
  144. int flag = 0;
  145. try{
  146. connection = DBConnection.getConnection();
  147. preparedStatement = connection.prepareStatement(sql);
  148. preparedStatement.setString(1, flight.getDestination());
  149. preparedStatement.setDate(2,new java.sql.Date(flight.getTakeName().getTime()));
  150. preparedStatement.setInt(3, flight.getFlightId());
  151. flag = preparedStatement.executeUpdate();
  152. }catch (SQLException e){
  153. e.printStackTrace();
  154. }finally {
  155. DBConnection.closeTwo(connection,preparedStatement);
  156. }
  157. return flag;
  158. }
  159. //删除航班
  160. public static int delete(Flight flight){
  161. String sql = "delete from flightinformationmanagesystem.flight where id=?";
  162. Connection connection = null;
  163. PreparedStatement preparedStatement = null;
  164. int flag = 0;
  165. try{
  166. connection = DBConnection.getConnection();
  167. preparedStatement = connection.prepareStatement(sql);
  168. preparedStatement.setInt(1, flight.getIdFlight());
  169. flag = preparedStatement.executeUpdate();
  170. }catch (SQLException e){
  171. e.printStackTrace();
  172. }finally {
  173. DBConnection.closeTwo(connection,preparedStatement);
  174. }
  175. return flag;
  176. }
  177. }

下面写Service层

首先是RegisterService

  1. package com.ambow.service;
  2. import com.ambow.bean.User;
  3. import com.ambow.dao.RegisterDao;
  4. import com.ambow.util.DBConnection;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. public class RegisterService {
  10. //判断用户是否存在
  11. public User userIsExist(String username){
  12. String sql = "select loginname from flightinformationmanagesystem.user where loginname =?";
  13. User user = null;
  14. Connection connection = null;
  15. PreparedStatement preparedStatement =null;
  16. ResultSet resultSet = null;
  17. try{
  18. connection = DBConnection.getConnection();
  19. preparedStatement = connection.prepareStatement(sql);
  20. preparedStatement.setString(1,username);
  21. resultSet = preparedStatement.executeQuery();
  22. while (resultSet.next()){
  23. user = new User();
  24. user.setUserName(resultSet.getString("loginname"));
  25. }
  26. }catch (SQLException e){
  27. e.printStackTrace();
  28. }
  29. finally {
  30. DBConnection.closeAll(connection,preparedStatement,resultSet);
  31. }
  32. return user;
  33. }
  34. //调用RegisterDao类
  35. RegisterDao registerDao = new RegisterDao();
  36. public Boolean Register(String username,String password,String realname){
  37. boolean isSuccess = registerDao.register(username,password,realname);
  38. if (isSuccess){
  39. return isSuccess;
  40. }
  41. return null;
  42. }
  43. }

UserService

  1. package com.ambow.service;
  2. import com.ambow.bean.User;
  3. import com.ambow.dao.UserDao;
  4. import com.ambow.util.DBConnection;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. public class UserService {
  10. public User userIsExist(String username){
  11. String sql = "select loginname from flightinformationmanagesystem.user where loginname =?";
  12. User user = null;
  13. Connection connection = null;
  14. PreparedStatement preparedStatement =null;
  15. ResultSet resultSet = null;
  16. try{
  17. connection = DBConnection.getConnection();
  18. preparedStatement = connection.prepareStatement(sql);
  19. preparedStatement.setString(1,username);
  20. preparedStatement.executeQuery();
  21. while (resultSet.next()){
  22. user = new User();
  23. user.setUserName(resultSet.getString("loginname"));
  24. }
  25. }catch (SQLException e){
  26. e.printStackTrace();
  27. }
  28. finally {
  29. DBConnection.closeAll(connection,preparedStatement,resultSet);
  30. }
  31. return user;
  32. }
  33. //调用UserDao类
  34. UserDao userDao = new UserDao();
  35. //service层的selectByNameAndPassword方法
  36. public User selectByNameAndPassword(String userName, String Password){
  37. //调用UserDao类的selectByNameAndPassword方法
  38. User isSuccess = userDao.selectByNameAndPassword(userName,Password);
  39. //如果isSuccess不为空且密码相等,返回应该isSuccess
  40. if (isSuccess != null && Password.equals(isSuccess.getPassword())){
  41. return isSuccess;
  42. }
  43. return null;
  44. }
  45. }

FlightService

  1. package com.ambow.service;
  2. import com.ambow.bean.Flight;
  3. import com.ambow.dao.FlightDao;
  4. import java.util.Date;
  5. import java.util.List;
  6. public class FlightService {
  7. FlightDao flightDao = new FlightDao();
  8. //查询所有航班信息
  9. public List<Flight> selectAll(){
  10. List<Flight> flightList = FlightDao.selectAll();
  11. return flightList;
  12. }
  13. //通过id查询航班信息
  14. public Flight selectById(int flightId){
  15. Flight flight = flightDao.selectById(flightId);
  16. return flight;
  17. }
  18. //通过目的地查询航班信息
  19. public List<Flight> selectByDestination(String destination){
  20. Flight flight =new Flight();
  21. flight.setDestination(destination);
  22. List<Flight> flightList = FlightDao.selectAllByDestination(flight);
  23. return flightList;
  24. }
  25. //通过起飞时间查询航班信息
  26. public List<Flight> selectByTaketime(Flight flight){
  27. List<Flight> flightList = FlightDao.selectAllByTaketime(flight);
  28. return flightList;
  29. }
  30. //增加航班信息
  31. public boolean add(Flight flight){
  32. int flag = FlightDao.add(flight);
  33. if(flag == 1){
  34. return true;
  35. }else {
  36. return false;
  37. }
  38. }
  39. //修改航班信息
  40. public boolean update(Flight flight){
  41. int flag = FlightDao.update(flight);
  42. if(flag == 1){
  43. return true;
  44. }else {
  45. return false;
  46. }
  47. }
  48. //删除航班信息
  49. public boolean delete(Flight flight){
  50. int flag = FlightDao.delete(flight);
  51. if(flag == 1){
  52. return true;
  53. }else {
  54. return false;
  55. }
  56. }
  57. }

 然后是servlet层

servlet层代码较多,我们慢慢来

RegisterServlet

  1. package com.ambow.servlet;
  2. import com.ambow.bean.User;
  3. import com.ambow.service.RegisterService;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. @WebServlet(name = "RegisterServlet",urlPatterns = "/RegisterServlet")
  11. public class RegisterServlet extends HttpServlet {
  12. @Override
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. }
  15. @Override
  16. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  17. //设置字符编码,防止乱码
  18. request.setCharacterEncoding("UTF-8");
  19. response.setContentType("text/html;charset=UTF-8");
  20. //获取表单信息
  21. String username = request.getParameter("username");
  22. String realname = request.getParameter("fullname");
  23. String password = request.getParameter("password");
  24. //检查用户名和密码是否为空
  25. if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
  26. String scr ="<script>alert('用户名和密码不能为空!');window.location.href='register.jsp'</script>";
  27. response.getWriter().write(scr);
  28. }else{
  29. //调用Register类进行注册
  30. RegisterService registerService = new RegisterService();
  31. //检查用户名是否已经存在
  32. User userIsExist = registerService.userIsExist(username);
  33. if (userIsExist != null){
  34. String scrs = "<script>alert('用户名已经存在,请选择其他用户名!');window.location.href='register.jsp'</script>";
  35. response.getWriter().write(scrs);
  36. }else {
  37. //注册用户
  38. Boolean register = registerService.Register(username, password, realname);
  39. if (register){
  40. String scri ="<script>alert('注册成功!');window.location.href='login.jsp'</script>";
  41. response.getWriter().write(scri);
  42. }else {
  43. String scrip ="<script>alert('注册失败!');window.location.href='register.jsp'</script>";
  44. response.getWriter().write(scrip);
  45. }
  46. }
  47. }
  48. }
  49. }

UerServlet

  1. package com.ambow.servlet;
  2. import com.ambow.bean.User;
  3. import com.ambow.service.UserService;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. @WebServlet(name = "LoginServlet",urlPatterns = "/LoginServlet")
  11. public class UserServlet extends HttpServlet {
  12. @Override
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. //设置中文字符,防止乱码
  15. request.setCharacterEncoding("UTF-8");
  16. response.setContentType("text/html;charset=utf-8");
  17. //提交客户端信息
  18. String userName = request.getParameter("username");
  19. String password = request.getParameter("password");
  20. // 检查用户名和密码是否为空
  21. if (userName == null || userName.isEmpty() || password == null || password.isEmpty()){
  22. String scr ="<script>alert('用户名或密码为空!');window.location.href='login.jsp'</script>";
  23. response.getWriter().write(scr);
  24. }else {
  25. //调用UserService
  26. UserService userService = new UserService();
  27. User user = userService.selectByNameAndPassword(userName,password);
  28. if(user != null){
  29. // request.setAttribute("user",userName);
  30. // response.sendRedirect("main.jsp");
  31. //设置登录成功的标志
  32. request.setAttribute("loginSuccessful", true);
  33. request.getSession().setAttribute("user", user); // 将用户存储到 session 中,以便在后续请求中使用
  34. response.sendRedirect(request.getContextPath() + "/FlightServlet"); // 执行重定向到 /FlightServlet 路径
  35. }else {
  36. String scr ="<script>alert('用户名或密码错误,请仔细检查后登录!');window.location.href='login.jsp'</script>";
  37. response.getWriter().write(scr);
  38. }
  39. }
  40. }
  41. @Override
  42. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  43. doGet(request,response);
  44. }
  45. }

LoginOutServlet

  1. package com.ambow.servlet;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import java.io.IOException;
  9. @WebServlet(name = "LogoutServlet", urlPatterns = "/LogoutServlet")
  10. public class LoginOutServlet extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. performLogout(request,response);
  14. }
  15. @Override
  16. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  17. performLogout(request,response);
  18. }
  19. private void performLogout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  20. HttpSession session = request.getSession();
  21. //清除 session 中的用户信息
  22. session.removeAttribute("user");
  23. // // 或者直接使整个 session 失效
  24. // session.invalidate();
  25. // 重定向到登录页面或其他页面
  26. response.sendRedirect(request.getContextPath() + "/login.jsp");
  27. }
  28. }

 AddServlet

  1. package com.ambow.servlet;
  2. import com.ambow.bean.Flight;
  3. import com.ambow.service.FlightService;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.text.ParseException;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Date;
  13. @WebServlet(name = "AddFlightServlet",urlPatterns = "/AddFlightServlet")
  14. public class AddFlightServlet extends HttpServlet {
  15. @Override
  16. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  17. }
  18. @Override
  19. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  20. //设置编码格式
  21. request.setCharacterEncoding("UTF-8");
  22. response.setContentType("text/html;charset=utf-8");
  23. //获取客户端信息
  24. String flightid = request.getParameter("flightId");
  25. String destination = request.getParameter("destination");
  26. String taketime = request.getParameter("takeoffTime");
  27. //实例化Flight对象并设置属性
  28. Flight flight = new Flight();
  29. //将字符串类型转换成整形
  30. flight.setFlightId(Integer.parseInt(flightid));
  31. flight.setDestination(destination);
  32. //将字符串类型转换成Date类型
  33. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  34. Date taketimes =null;
  35. try{
  36. taketimes = simpleDateFormat.parse(taketime);
  37. }catch (ParseException e){
  38. e.printStackTrace();
  39. }
  40. flight.setTakeName(taketimes);
  41. //调用FlightService类
  42. FlightService flightService = new FlightService();
  43. boolean isAddSuccess = flightService.add(flight);
  44. if (isAddSuccess){
  45. // 重定向到航班信息页面
  46. response.sendRedirect(request.getContextPath() + "/FlightServlet");
  47. }else {
  48. String scr = "<script>alert('航班信息添加失败,请重试!');window.location.href='AddFlight.jsp'</script>";
  49. response.getWriter().write(scr);
  50. }
  51. // //获取返回主页面按钮
  52. // String btn = request.getParameter("back");
  53. // if ("back".equals(btn)){
  54. // // 重定向到航班信息页面
  55. // response.sendRedirect(request.getContextPath() + "/FlightServlet");
  56. // }
  57. }
  58. }

 DeleteServlet

  1. package com.ambow.servlet;
  2. import com.ambow.bean.Flight;
  3. import com.ambow.service.FlightService;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. @WebServlet(name = "DeleteFlightServlet", urlPatterns = "/DeleteFlightServlet")
  11. public class DeleteFlightServlet extends HttpServlet {
  12. @Override
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. }
  15. @Override
  16. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  17. //获取要删除的航班id
  18. int idFlight = Integer.parseInt(request.getParameter("flightId"));
  19. //调用FlightService类中的delete方法进行删除
  20. FlightService flightService = new FlightService();
  21. Flight flight = new Flight();
  22. flight.setIdFlight(idFlight);
  23. boolean isDeleteSuccess = flightService.delete(flight);
  24. if (isDeleteSuccess) {
  25. // 删除成功,重定向到显示航班信息的页面
  26. response.sendRedirect(request.getContextPath() + "/FlightServlet");
  27. } else {
  28. // 删除失败,返回错误页面或其他处理
  29. String scr = "<script>alert('航班信息删除失败,请重试!');window.location.href='main.jsp'</script>";
  30. response.getWriter().write(scr);
  31. }
  32. }
  33. }

 EditFlightServlet

  1. package com.ambow.servlet;
  2. import com.ambow.bean.Flight;
  3. import com.ambow.service.FlightService;
  4. import javax.servlet.RequestDispatcher;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11. import java.util.List;
  12. @WebServlet(name = "EditFlightServlet", urlPatterns = "/EditFlightServlet")
  13. public class EditFlightServlet extends HttpServlet {
  14. @Override
  15. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16. //获取用户表单信息
  17. String flightIdParam = request.getParameter("id");
  18. if (flightIdParam != null && !flightIdParam.isEmpty()) {
  19. int Idflight = Integer.parseInt(flightIdParam);
  20. //查询数据库,获取航班信息
  21. FlightService flightService = new FlightService();
  22. Flight flights = flightService.selectById(Idflight);
  23. //将航班信息存储到 request 属性中
  24. request.setAttribute("flights", flights);
  25. request.getRequestDispatcher("UpdateFlight.jsp").forward(request, response);
  26. }else {
  27. response.sendRedirect(request.getContextPath() + "/FlightServlet");
  28. }
  29. }
  30. @Override
  31. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  32. }
  33. }

FlightServlet

  1. package com.ambow.servlet;
  2. import com.ambow.bean.Flight;
  3. import com.ambow.bean.User;
  4. import com.ambow.service.FlightService;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11. import java.util.List;
  12. @WebServlet("/FlightServlet")
  13. public class FlightServlet extends HttpServlet {
  14. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  15. throws IOException, ServletException {
  16. //创建service对象,调用查询所有的方法
  17. FlightService fs = new FlightService();
  18. //调用查询所有的方法
  19. List<Flight> flights = fs.selectAll();
  20. //将信息储存到作用域中
  21. request.setAttribute("flights",flights);
  22. //转发到FlightInfo.jsp页面
  23. request.getRequestDispatcher("main.jsp").forward(request, response);
  24. }
  25. }

 SelectByDestinationServlet

  1. package com.ambow.servlet;
  2. import com.ambow.bean.Flight;
  3. import com.ambow.service.FlightService;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.util.List;
  11. @WebServlet(name = "SelectByDestination",urlPatterns = "/SelectByDestination")
  12. public class SelectByDestinationServlet extends HttpServlet {
  13. @Override
  14. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15. //设置编码格式
  16. request.setCharacterEncoding("UTF-8");
  17. response.setContentType("text/html;charset=utf-8");
  18. // 获取客户端信息
  19. String destination = request.getParameter("destination");
  20. // 调用 FlightService 类
  21. FlightService flightService = new FlightService();
  22. List<Flight> flights = flightService.selectByDestination(destination);
  23. if (flights != null && !flights.isEmpty()) {
  24. // 将信息储存到作用域中
  25. request.setAttribute("flights", flights);
  26. // 转发到 main.jsp 页面
  27. request.getRequestDispatcher("main.jsp").forward(request, response);
  28. } else {
  29. // 目的地找不到的处理逻辑
  30. String scr = "<script>alert('该目的地找不到!');window.location.href='SelectByDestination.jsp'</script>";
  31. response.getWriter().write(scr);
  32. }
  33. }
  34. }

 SelectByTaketimeServlet

  1. package com.ambow.servlet;
  2. import com.ambow.bean.Flight;
  3. import com.ambow.service.FlightService;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.text.ParseException;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Date;
  13. import java.util.List;
  14. @WebServlet(name = "SelectByTaketime",urlPatterns = "/SelectByTaketime")
  15. public class SelectByTaketimeServlet extends HttpServlet {
  16. @Override
  17. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  18. }
  19. @Override
  20. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  21. //设置编码格式
  22. request.setCharacterEncoding("UTF-8");
  23. response.setContentType("text/html;charset=utf-8");
  24. // 获取客户端信息
  25. String taketime = request.getParameter("taketime");
  26. //实例化Flight对象并设置属性
  27. Flight flight = new Flight();
  28. //将字符串类型转换成Date类型
  29. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  30. Date taketimes =null;
  31. try{
  32. taketimes = simpleDateFormat.parse(taketime);
  33. }catch (ParseException e){
  34. e.printStackTrace();
  35. }
  36. flight.setTakeName(taketimes);
  37. // 调用 FlightService 类
  38. FlightService flightService = new FlightService();
  39. List<Flight> flights = flightService.selectByTaketime(flight);
  40. if (flights != null && !flights.isEmpty()) {
  41. // 将信息储存到作用域中
  42. request.setAttribute("flights", flights);
  43. // 转发到 main.jsp 页面
  44. request.getRequestDispatcher("main.jsp").forward(request, response);
  45. } else {
  46. // 目的地找不到的处理逻辑
  47. String scr = "<script>alert('该起飞时间找不到!');window.location.href='SelectByTaketime.jsp'</script>";
  48. response.getWriter().write(scr);
  49. }
  50. }
  51. }

 UpdateFlightServlet

  1. package com.ambow.servlet;
  2. import com.ambow.bean.Flight;
  3. import com.ambow.service.FlightService;
  4. import com.sun.xml.internal.ws.api.ha.HaInfo;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11. import java.text.ParseException;
  12. import java.text.SimpleDateFormat;
  13. import java.util.Date;
  14. @WebServlet(name = "UpdateFlightServlet",urlPatterns = "/UpdateFlightServlet")
  15. public class UpdateFlightServlet extends HttpServlet {
  16. @Override
  17. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  18. }
  19. @Override
  20. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  21. //设置编码格式
  22. request.setCharacterEncoding("UTF-8");
  23. response.setContentType("text/html;charset=utf-8");
  24. //获取客户端信息
  25. String flightid = request.getParameter("flightId");
  26. String destination = request.getParameter("destination");
  27. String taketime = request.getParameter("takeoffTime");
  28. //调用Flight类
  29. Flight flight = new Flight();
  30. flight.setFlightId(Integer.parseInt(flightid));
  31. flight.setDestination(destination);
  32. //将字符串类型转换成Date类型
  33. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  34. Date taketimes =null;
  35. try{
  36. taketimes = simpleDateFormat.parse(taketime);
  37. }catch (ParseException e){
  38. e.printStackTrace();
  39. }
  40. flight.setTakeName(taketimes);
  41. //调用FlightService类中的update方法
  42. FlightService flightService =new FlightService();
  43. boolean isUpdateSuccess = flightService.update(flight);
  44. if (isUpdateSuccess){
  45. // 重定向到航班信息页面
  46. response.sendRedirect(request.getContextPath() + "/FlightServlet");
  47. }else {
  48. String scr = "<script>alert('航班信息修改失败,请重试!');window.location.href='UpdateFlight.jsp'</script>";
  49. response.getWriter().write(scr);
  50. }
  51. }
  52. }

三:前端JSP页面

JSP是原生的HTML页面添入Java代码

下面开始编写JSP代码

AddFlight.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: 29988
  4. Date: 2024/1/4
  5. Time: 14:17
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  10. <html>
  11. <head>
  12. <title>增加航班信息</title>
  13. <style type="text/css">
  14. body {
  15. font-family: Arial, sans-serif;
  16. background-color: #f4f4f4;
  17. margin: 0;
  18. display: flex;
  19. }
  20. .sidebar {
  21. background-color: #333;
  22. color: #fff;
  23. width: 200px;
  24. padding: 20px;
  25. box-sizing: border-box;
  26. }
  27. .nav-link {
  28. text-decoration: none;
  29. color: #fff;
  30. font-family: 微软雅黑, serif;
  31. display: block;
  32. padding: 10px;
  33. margin-bottom: 10px;
  34. border-radius: 4px;
  35. transition: background-color 0.3s;
  36. }
  37. .nav-link:hover {
  38. background-color: #555;
  39. }
  40. .logout-link {
  41. margin-top: 20px;
  42. position: fixed;
  43. right: 3%;
  44. top: 3%;
  45. }
  46. .loginout{
  47. text-decoration: none;
  48. color: #4caf50;
  49. }
  50. .user{
  51. margin-top: 20px;
  52. position: fixed;
  53. right: 8%;
  54. top: 3%;
  55. }
  56. .content {
  57. margin-top: 100px;
  58. margin-left: 450px;
  59. padding: 20px;
  60. }
  61. .form-container {
  62. width: 400px;
  63. margin: 20px;
  64. padding: 20px;
  65. box-sizing: border-box;
  66. background-color: #fff;
  67. border-radius: 8px;
  68. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  69. }
  70. .form-group {
  71. margin-bottom: 15px;
  72. }
  73. .form-group label {
  74. display: block;
  75. font-weight: bold;
  76. margin-bottom: 5px;
  77. }
  78. .form-group input {
  79. width: 100%;
  80. padding: 8px;
  81. box-sizing: border-box;
  82. border: 1px solid #ccc;
  83. border-radius: 4px;
  84. }
  85. .form-group button {
  86. background-color: #4caf50;
  87. color: #fff;
  88. padding: 10px;
  89. margin: 10px;
  90. border: none;
  91. border-radius: 4px;
  92. cursor: pointer;
  93. width: 100%;
  94. }
  95. .form-group button:hover {
  96. background-color: #45a049;
  97. }
  98. </style>
  99. </head>
  100. <body>
  101. <div class="sidebar">
  102. <h2>系统</h2>
  103. <a class="nav-link" onclick="redirectToMainPage()">主页</a>
  104. <a href="SelectByDestination.jsp" class="nav-link">按目的地查询</a>
  105. <a href="SelectByTaketime.jsp" class="nav-link">按起飞时间查询</a>
  106. </div>
  107. <c:if test="${not empty sessionScope.user}">
  108. <div class="user">
  109. <p><span>你好, ${sessionScope.user.userName}</span></p>
  110. </div>
  111. <div class="logout-link">
  112. <p><a href="LogoutServlet" class="loginout">注销</a></p>
  113. </div>
  114. </c:if>
  115. <c:if test="${empty sessionScope.user}">
  116. <div>
  117. <p><a href="login.jsp">登录</a></p>
  118. </div>
  119. <div>
  120. <p><a href="register.jsp">注册</a></p>
  121. </div>
  122. </c:if>
  123. <div class="content">
  124. <div class="form-container">
  125. <h2>新增航班信息</h2>
  126. <form action="AddFlightServlet" method="post">
  127. <div class="form-group">
  128. <label for="flightId">航班号:</label>
  129. <input type="text" id="flightId" name="flightId" required>
  130. </div>
  131. <div class="form-group">
  132. <label for="destination">目的地:</label>
  133. <input type="text" id="destination" name="destination" required>
  134. </div>
  135. <div class="form-group">
  136. <label for="takeoffTime">起飞时间:</label>
  137. <input type="date" id="takeoffTime" name="takeoffTime" required>
  138. </div>
  139. <div class="form-group">
  140. <button type="submit">保存</button>
  141. <button type="button" onclick="redirectToMainPage()">返回主页面</button>
  142. </div>
  143. </form>
  144. </div>
  145. </div>
  146. <script>
  147. function redirectToMainPage() {
  148. window.location.href = "${pageContext.request.contextPath}/FlightServlet";
  149. }
  150. </script>
  151. </body>
  152. </html>

login.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: 29988
  4. Date: 2024/1/2
  5. Time: 8:42
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  10. <html>
  11. <style type="text/css">
  12. body {
  13. font-family: Arial, sans-serif;
  14. background-color: #f4f4f4;
  15. margin: 0;
  16. display: flex;
  17. align-items: center;
  18. justify-content: center;
  19. height: 100vh;
  20. }
  21. .login-container {
  22. background-color: #fff;
  23. padding: 20px;
  24. border-radius: 8px;
  25. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  26. width: 400px;
  27. height:450px;
  28. }
  29. .login-container h2 {
  30. text-align: center;
  31. }
  32. .form-group {
  33. margin-bottom: 15px;
  34. }
  35. .form-group label {
  36. display: block;
  37. font-weight: bold;
  38. margin-bottom: 5px;
  39. }
  40. .form-group input {
  41. width: 100%;
  42. padding: 8px;
  43. box-sizing: border-box;
  44. border: 1px solid #ccc;
  45. border-radius: 4px;
  46. }
  47. .form-group button {
  48. background-color: #4caf50;
  49. color: #fff;
  50. padding: 10px;
  51. border: none;
  52. border-radius: 4px;
  53. cursor: pointer;
  54. width: 100%;
  55. }
  56. .form-group button:hover {
  57. background-color: #45a049;
  58. }
  59. a{
  60. text-decoration: none;
  61. color: #fff;
  62. }
  63. .h2s{
  64. position: fixed;
  65. top: 7%;
  66. left: 42%;
  67. font-family: 幼圆, serif ;
  68. font-size: 35px;
  69. }
  70. </style>
  71. <head>
  72. <title>登录</title>
  73. <link rel="shortcut icon" href="#"/>
  74. </head>
  75. <body>
  76. <h2 class="h2s">航班信息管理系统</h2>
  77. <div class="login-container">
  78. <h2>用户登录</h2>
  79. <c:if test="${not empty loginSuccessful}">
  80. <c:redirect url="/FlightServlet"/>
  81. </c:if>
  82. <form action="LoginServlet" method="post">
  83. <div class="form-group">
  84. <label for="username">用户名:</label>
  85. <input type="text" id="username" name="username" required>
  86. </div>
  87. <div class="form-group">
  88. <label for="password">密码:</label>
  89. <input type="password" id="password" name="password" required>
  90. </div>
  91. <div class="form-group">
  92. <button type="submit">登录</button>
  93. </div>
  94. <div class="form-group">
  95. <button><a href="register.jsp">注册</a></button>
  96. </div>
  97. </form>
  98. </div>
  99. </body>
  100. </html>

 main.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: 29988
  4. Date: 2024/1/2
  5. Time: 19:15
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  10. <html>
  11. <head>
  12. <title>航班信息管理系统</title>
  13. <link rel="shortcut icon" href="#"/>
  14. <style type="text/css">
  15. body {
  16. font-family: Arial, sans-serif;
  17. background-color: #f4f4f4;
  18. margin: 0;
  19. display: flex;
  20. }
  21. .sidebar {
  22. background-color: #333;
  23. color: #fff;
  24. width: 200px;
  25. padding: 20px;
  26. box-sizing: border-box;
  27. }
  28. .nav-link {
  29. text-decoration: none;
  30. color: #fff;
  31. font-family: 微软雅黑, serif;
  32. display: block;
  33. padding: 10px;
  34. margin-bottom: 10px;
  35. border-radius: 4px;
  36. transition: background-color 0.3s;
  37. }
  38. .nav-link:hover {
  39. background-color: #555;
  40. }
  41. .main-content {
  42. flex: 1;
  43. padding: 20px;
  44. }
  45. table {
  46. width: 100%;
  47. border-collapse: collapse;
  48. margin-top: 20px;
  49. }
  50. th, td {
  51. border: 1px solid #ddd;
  52. padding: 8px;
  53. text-align: left;
  54. }
  55. th {
  56. background-color: #333;
  57. color: #fff;
  58. }
  59. .logout-link {
  60. margin-top: 20px;
  61. position: fixed;
  62. right: 3%;
  63. top: 3%;
  64. }
  65. .loginout{
  66. text-decoration: none;
  67. color: #4caf50;
  68. }
  69. .user{
  70. margin-top: 20px;
  71. position: fixed;
  72. right: 8%;
  73. top: 3%;
  74. }
  75. .update{
  76. text-decoration: none;
  77. color: black;
  78. }
  79. </style>
  80. </head>
  81. <body>
  82. <div class="sidebar">
  83. <h2>系统</h2>
  84. <a class="nav-link" onclick="redirectToMainPage()">主页</a>
  85. <a href="AddFlight.jsp" class="nav-link">新增</a>
  86. <a href="SelectByDestination.jsp" class="nav-link">按目的地查询</a>
  87. <a href="SelectByTaketime.jsp" class="nav-link">按起飞时间查询</a>
  88. </div>
  89. <c:if test="${not empty sessionScope.user}">
  90. <div class="user">
  91. <p><span>你好, ${sessionScope.user.userName}</span></p>
  92. </div>
  93. <div class="logout-link">
  94. <p><a href="LogoutServlet" class="loginout">注销</a></p>
  95. </div>
  96. </c:if>
  97. <c:if test="${empty sessionScope.user}">
  98. <div>
  99. <p><a href="login.jsp">登录</a></p>
  100. </div>
  101. <div>
  102. <p><a href="register.jsp">注册</a></p>
  103. </div>
  104. </c:if>
  105. <div class="main-content">
  106. <h1>航班信息管理系统</h1>
  107. <table>
  108. <thead>
  109. <tr>
  110. <th>编号</th>
  111. <th>航班号</th>
  112. <th>目的地</th>
  113. <th>起飞时间</th>
  114. <th>操作</th>
  115. </tr>
  116. </thead>
  117. <tbody>
  118. <c:forEach var="flights" items="${flights}">
  119. <tr>
  120. <td>${flights.idFlight}</td>
  121. <td>${flights.flightId}</td>
  122. <td>${flights.destination}</td>
  123. <td>${flights.takeName}</td>
  124. <td>
  125. <div style="display: inline-block;">
  126. <input type="hidden" name="flightId" value="${flights.idFlight}">
  127. <button type="button"><a href="EditFlightServlet?id=${flights.idFlight}" class="update" onclick="return confirm('是否修改?')">修改</a></button>
  128. </div>
  129. <div style="display: inline-block;">
  130. <form action="DeleteFlightServlet" method="post">
  131. <input type="hidden" name="flightId" value="${flights.idFlight}">
  132. <button type="submit" onclick="return confirm('是否删除?')">删除</button>
  133. </form>
  134. </td>
  135. </tr>
  136. </c:forEach>
  137. </tbody>
  138. </table>
  139. </div>
  140. <script>
  141. function redirectToMainPage() {
  142. window.location.href = "${pageContext.request.contextPath}/FlightServlet";
  143. }
  144. </script>
  145. </body>
  146. </html>

register.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: 29988
  4. Date: 2024/1/2
  5. Time: 20:59
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10. <head>
  11. <title>注册</title>
  12. </head>
  13. <style type="text/css">
  14. body {
  15. font-family: Arial, sans-serif;
  16. background-color: #f4f4f4;
  17. margin: 0;
  18. display: flex;
  19. align-items: center;
  20. justify-content: center;
  21. height: 100vh;
  22. }
  23. .form-container {
  24. background-color: #fff;
  25. padding: 20px;
  26. border-radius: 8px;
  27. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  28. width: 400px;
  29. }
  30. .form-container h2 {
  31. text-align: center;
  32. }
  33. .form-group {
  34. margin-bottom: 15px;
  35. }
  36. .form-group label {
  37. display: block;
  38. font-weight: bold;
  39. margin-bottom: 5px;
  40. }
  41. .form-group input {
  42. width: 100%;
  43. padding: 8px;
  44. box-sizing: border-box;
  45. border: 1px solid #ccc;
  46. border-radius: 4px;
  47. }
  48. .form-group button {
  49. background-color: #4caf50;
  50. color: #fff;
  51. padding: 10px;
  52. border: none;
  53. border-radius: 4px;
  54. cursor: pointer;
  55. width: 100%;
  56. }
  57. .form-group button:hover {
  58. background-color: #45a049;
  59. }
  60. .form-group .login-link {
  61. text-align: center;
  62. margin-top: 10px;
  63. }
  64. .h2s{
  65. position: fixed;
  66. top: 7%;
  67. left: 42%;
  68. font-family: 幼圆, serif ;
  69. font-size: 35px;
  70. }
  71. </style>
  72. <body>
  73. <h2 class="h2s">航班信息管理系统</h2>
  74. <div class="form-container">
  75. <h2>用户注册</h2>
  76. <form action="RegisterServlet" method="post">
  77. <div class="form-group">
  78. <label for="username">用户名:</label>
  79. <input type="text" id="username" name="username" required>
  80. </div>
  81. <div class="form-group">
  82. <label for="fullname">真实姓名:</label>
  83. <input type="text" id="fullname" name="fullname" required>
  84. </div>
  85. <div class="form-group">
  86. <label for="password">密码:</label>
  87. <input type="password" id="password" name="password" required>
  88. </div>
  89. <div class="form-group">
  90. <button type="submit">注册</button>
  91. </div>
  92. <div class="form-group login-link">
  93. <p>已经有账号? <a href="login.jsp">点击登录</a></p>
  94. </div>
  95. </form>
  96. </div>
  97. </body>
  98. </html>

SelectByDestination.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: 29988
  4. Date: 2024/1/5
  5. Time: 9:51
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10. <head>
  11. <title>通过目的地查询</title>
  12. <style type="text/css">
  13. body {
  14. font-family: Arial, sans-serif;
  15. background-color: #f4f4f4;
  16. margin: 0;
  17. display: flex;
  18. }
  19. .sidebar {
  20. background-color: #333;
  21. color: #fff;
  22. width: 200px;
  23. padding: 20px;
  24. box-sizing: border-box;
  25. }
  26. .nav-link {
  27. text-decoration: none;
  28. color: #fff;
  29. font-family: 微软雅黑, serif;
  30. display: block;
  31. padding: 10px;
  32. margin-bottom: 10px;
  33. border-radius: 4px;
  34. transition: background-color 0.3s;
  35. }
  36. .nav-link:hover {
  37. background-color: #555;
  38. }
  39. .logout-link {
  40. margin-top: 20px;
  41. position: fixed;
  42. right: 3%;
  43. top: 3%;
  44. }
  45. .loginout{
  46. text-decoration: none;
  47. color: #4caf50;
  48. }
  49. .user{
  50. margin-top: 20px;
  51. position: fixed;
  52. right: 8%;
  53. top: 3%;
  54. }
  55. .form-container {
  56. background-color: #fff;
  57. padding: 20px;
  58. border-radius: 4px;
  59. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  60. text-align: center;
  61. height: 350px;
  62. width: 400px;
  63. margin-left: 450px;
  64. margin-top: 200px;
  65. }
  66. .form-container h2 {
  67. margin-bottom: 20px;
  68. }
  69. .form-container label {
  70. display: block;
  71. margin-bottom: 10px;
  72. }
  73. .form-container input {
  74. width: 100%;
  75. padding: 10px;
  76. margin-bottom: 20px;
  77. box-sizing: border-box;
  78. border: 1px solid #ccc;
  79. border-radius: 4px;
  80. }
  81. .form-container button {
  82. background-color: #4caf50;
  83. color: #fff;
  84. padding: 10px 20px;
  85. border: none;
  86. border-radius: 4px;
  87. cursor: pointer;
  88. transition: background-color 0.3s;
  89. }
  90. .form-container button:hover {
  91. background-color: #45a049;
  92. }
  93. </style>
  94. </head>
  95. <body>
  96. <div class="sidebar">
  97. <h2>系统</h2>
  98. <a class="nav-link" onclick="redirectToMainPage()">主页</a>
  99. <a href="AddFlight.jsp" class="nav-link">新增</a>
  100. <a href="SelectByTaketime.jsp" class="nav-link">按起飞时间查询</a>
  101. </div>
  102. <c:if test="${not empty sessionScope.user}">
  103. <div class="user">
  104. <p><span>你好, ${sessionScope.user.userName}</span></p>
  105. </div>
  106. <div class="logout-link">
  107. <p><a href="LogoutServlet" class="loginout">注销</a></p>
  108. </div>
  109. </c:if>
  110. <div class="form-container">
  111. <h2>按目的地查询</h2>
  112. <form action="SelectByDestination" method="post">
  113. <label for="destination">目的地:</label>
  114. <input type="text" id="destination" name="destination" required>
  115. <br>
  116. <button type="submit">查询</button>
  117. </form>
  118. </div>
  119. <script>
  120. function redirectToMainPage() {
  121. window.location.href = "${pageContext.request.contextPath}/FlightServlet";
  122. }
  123. </script>
  124. </body>
  125. </html>

SelectByTaketime.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: 29988
  4. Date: 2024/1/5
  5. Time: 11:11
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10. <head>
  11. <title>通过起飞时间查询</title>
  12. <style type="text/css">
  13. body {
  14. font-family: Arial, sans-serif;
  15. background-color: #f4f4f4;
  16. margin: 0;
  17. display: flex;
  18. }
  19. .sidebar {
  20. background-color: #333;
  21. color: #fff;
  22. width: 200px;
  23. padding: 20px;
  24. box-sizing: border-box;
  25. }
  26. .nav-link {
  27. text-decoration: none;
  28. color: #fff;
  29. font-family: 微软雅黑, serif;
  30. display: block;
  31. padding: 10px;
  32. margin-bottom: 10px;
  33. border-radius: 4px;
  34. transition: background-color 0.3s;
  35. }
  36. .nav-link:hover {
  37. background-color: #555;
  38. }
  39. .logout-link {
  40. margin-top: 20px;
  41. position: fixed;
  42. right: 3%;
  43. top: 3%;
  44. }
  45. .loginout{
  46. text-decoration: none;
  47. color: #4caf50;
  48. }
  49. .user{
  50. margin-top: 20px;
  51. position: fixed;
  52. right: 8%;
  53. top: 3%;
  54. }
  55. .form-container {
  56. background-color: #fff;
  57. padding: 20px;
  58. border-radius: 4px;
  59. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  60. text-align: center;
  61. height: 350px;
  62. width: 400px;
  63. margin-left: 450px;
  64. margin-top: 200px;
  65. }
  66. .form-container h2 {
  67. margin-bottom: 20px;
  68. }
  69. .form-container label {
  70. display: block;
  71. margin-bottom: 10px;
  72. }
  73. .form-container input {
  74. width: 100%;
  75. padding: 10px;
  76. margin-bottom: 20px;
  77. box-sizing: border-box;
  78. border: 1px solid #ccc;
  79. border-radius: 4px;
  80. }
  81. .form-container button {
  82. background-color: #4caf50;
  83. color: #fff;
  84. padding: 10px 20px;
  85. border: none;
  86. border-radius: 4px;
  87. cursor: pointer;
  88. transition: background-color 0.3s;
  89. }
  90. .form-container button:hover {
  91. background-color: #45a049;
  92. }
  93. </style>
  94. </head>
  95. <body>
  96. <div class="sidebar">
  97. <h2>系统</h2>
  98. <a class="nav-link" onclick="redirectToMainPage()">主页</a>
  99. <a href="AddFlight.jsp" class="nav-link">新增</a>
  100. <a href="SelectByDestination.jsp" class="nav-link">按目的地查询</a>
  101. </div>
  102. <c:if test="${not empty sessionScope.user}">
  103. <div class="user">
  104. <p><span>你好, ${sessionScope.user.userName}</span></p>
  105. </div>
  106. <div class="logout-link">
  107. <p><a href="LogoutServlet" class="loginout">注销</a></p>
  108. </div>
  109. </c:if>
  110. <div class="form-container">
  111. <h2>按起飞时间查询</h2>
  112. <form action="SelectByTaketime" method="post">
  113. <label for="taketime">起飞时间:</label>
  114. <input type="date" id="taketime" name="taketime" required>
  115. <br>
  116. <button type="submit">查询</button>
  117. </form>
  118. </div>
  119. <script>
  120. function redirectToMainPage() {
  121. window.location.href = "${pageContext.request.contextPath}/FlightServlet";
  122. }
  123. </script>
  124. </body>
  125. </html>

UpdateFlight.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: 29988
  4. Date: 2024/1/4
  5. Time: 16:19
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  10. <html>
  11. <head>
  12. <title>修改航班信息</title>
  13. <style type="text/css">
  14. body {
  15. font-family: Arial, sans-serif;
  16. background-color: #f4f4f4;
  17. margin: 0;
  18. display: flex;
  19. }
  20. .sidebar {
  21. background-color: #333;
  22. color: #fff;
  23. width: 200px;
  24. padding: 20px;
  25. box-sizing: border-box;
  26. }
  27. .nav-link {
  28. text-decoration: none;
  29. color: #fff;
  30. font-family: 微软雅黑, serif;
  31. display: block;
  32. padding: 10px;
  33. margin-bottom: 10px;
  34. border-radius: 4px;
  35. transition: background-color 0.3s;
  36. }
  37. .nav-link:hover {
  38. background-color: #555;
  39. }
  40. .logout-link {
  41. margin-top: 20px;
  42. position: fixed;
  43. right: 3%;
  44. top: 3%;
  45. }
  46. .loginout{
  47. text-decoration: none;
  48. color: #4caf50;
  49. }
  50. .user{
  51. margin-top: 20px;
  52. position: fixed;
  53. right: 8%;
  54. top: 3%;
  55. }
  56. .content {
  57. margin-left: 450px;
  58. margin-top: 150px;
  59. background-color: #fff;
  60. padding: 20px;
  61. border-radius: 8px;
  62. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  63. width: 350px;
  64. height: 450px;
  65. text-align: center;
  66. }
  67. form {
  68. margin-top: 20px;
  69. }
  70. label {
  71. display: block;
  72. margin-bottom: 8px;
  73. margin-left: 0;
  74. }
  75. input {
  76. width: 100%;
  77. padding: 8px;
  78. margin-bottom: 12px;
  79. box-sizing: border-box;
  80. border: 1px solid #ccc;
  81. border-radius: 4px;
  82. }
  83. button {
  84. background-color: #4caf50;
  85. color: #fff;
  86. padding: 10px;
  87. border: none;
  88. border-radius: 4px;
  89. cursor: pointer;
  90. }
  91. button:hover {
  92. background-color: #45a049;
  93. }
  94. </style>
  95. </head>
  96. <body>
  97. <div class="sidebar">
  98. <h2>系统</h2>
  99. <a class="nav-link" onclick="redirectToMainPage()">主页</a>
  100. <a href="AddFlight.jsp" class="nav-link">新增</a>
  101. <a href="SelectByDestination.jsp" class="nav-link">按目的地查询</a>
  102. <a href="SelectByTaketime.jsp" class="nav-link">按起飞时间查询</a>
  103. </div>
  104. <c:if test="${not empty sessionScope.user}">
  105. <div class="user">
  106. <p><span>你好, ${sessionScope.user.userName}</span></p>
  107. </div>
  108. <div class="logout-link">
  109. <p><a href="LogoutServlet" class="loginout">注销</a></p>
  110. </div>
  111. </c:if>
  112. <c:if test="${empty sessionScope.user}">
  113. <div>
  114. <p><a href="login.jsp">登录</a></p>
  115. </div>
  116. <div>
  117. <p><a href="register.jsp">注册</a></p>
  118. </div>
  119. </c:if>
  120. <div class="content">
  121. <h2 align="left">编辑航班信息</h2>
  122. <form action="UpdateFlightServlet" method="post">
  123. <input type="hidden" name="idFlight" value="${flights.idFlight}">
  124. <label for="flightId">航班号:</label>
  125. <input type="text" id="flightId" name="flightId" value="${flights.flightId}" required>
  126. <br>
  127. <label for="destination">目的地:</label>
  128. <input type="text" id="destination" name="destination" value="${flights.destination}" required>
  129. <br>
  130. <label for="takeoffTime">起飞时间:</label>
  131. <input type="text" id="takeoffTime" name="takeoffTime" value="${flights.takeName}" required>
  132. <br>
  133. <button type="submit">保存修改</button>
  134. <button type="button" onclick="redirectToMainPage()">返回主页面</button>
  135. </form>
  136. </div>
  137. <script>
  138. function redirectToMainPage() {
  139. window.location.href = "${pageContext.request.contextPath}/FlightServlet";
  140. }
  141. </script>
  142. </body>
  143. </html>

四:SQL语句

根据大家的需求,sql语句代码放在下面

本项目一共使用两个表,一个是user表,用来储存用户,一个flight表,用来存储航班信息

首先是user表

  1. CREATE TABLE `user` (
  2. `userid` int NOT NULL AUTO_INCREMENT,
  3. `loginname` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  4. `password` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  5. `realname` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  6. PRIMARY KEY (`userid`) USING BTREE
  7. ) ENGINE = InnoDB AUTO_INCREMENT = 16 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

然后是flight表

  1. CREATE TABLE `flight` (
  2. `id` int NOT NULL AUTO_INCREMENT,
  3. `flightid` int NOT NULL,
  4. `destination` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  5. `taketime` date NULL DEFAULT NULL,
  6. PRIMARY KEY (`id`) USING BTREE
  7. ) ENGINE = InnoDB AUTO_INCREMENT = 27 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

 项目到此基本完成了,如果中间有问题请在评论区评论!

项目中涉及到的jar包我放在百度网盘了,有需要的大家自行提取
https://pan.baidu.com/s/1LHeAnZ6TsExQCYKEMNqOlg
提取码:zoyc

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/480821
推荐阅读
相关标签
  

闽ICP备14008679号