基于JSP+Servlet的在线旅游路线销售平台 - 源码深度解析

JavaJavaScriptHTMLCSSMySQLJSP+Servlet
2026-03-113 浏览

文章摘要

本项目是一个基于JSP与Servlet技术栈开发的在线旅游路线销售平台,旨在为旅游服务商提供一个功能完整、稳定可靠的数字化产品直销渠道。其核心业务价值在于解决了传统旅行社产品展示方式单一、信息更新不及时、客户预订流程繁琐等核心痛点。通过将丰富的旅游路线产品(包括详细行程、费用说明、景点图片等)进行线...

在传统旅游行业数字化转型的浪潮中,一个高效、直观的在线销售平台已成为旅行社提升竞争力的核心工具。本项目采用经典的JSP与Servlet技术栈,构建了一个功能完备的“智游在线”旅游路线销售系统。该系统旨在打通旅游产品从后台管理、前端展示到用户预订的全流程,为中小型旅游服务商提供一个稳定可靠、自主可控的数字化解决方案。

系统采用浏览器/服务器架构,严格遵循MVC设计模式。Servlet作为控制器层,负责处理所有业务逻辑和请求路由;JSP结合JSTL标签库实现视图渲染,确保前后端分离;数据持久层通过JDBC与MySQL数据库交互。这种分层架构不仅保证了代码的可维护性,还为系统功能的横向扩展奠定了坚实基础。

系统架构与技术栈解析

模型层 由八个核心数据表构成,通过实体类与数据库表结构映射。所有数据库操作均通过自定义的DBUtil工具类进行封装,采用数据库连接池技术管理连接资源,有效避免频繁创建和关闭连接带来的性能开销。关键的数据查询操作使用PreparedStatement接口,防止SQL注入攻击,确保系统安全。

视图层 使用JSP动态页面技术,结合EL表达式和JSTL标准标签库替代传统的Scriptlet代码,实现了清晰的代码分离。页面布局采用DIV+CSS进行设计,保证在不同浏览器下的兼容性和响应式显示。对于需要动态交互的功能,如购物车商品数量更新、路线筛选等,引入了轻量级的JavaScript框架进行前端验证和异步数据处理。

控制层 是系统的调度中心,基于Servlet API实现。每个功能模块对应一个或多个Servlet控制器,通过web.xml配置文件或注解方式定义请求映射。控制器负责接收用户请求、调用相应的业务逻辑处理、封装响应数据并转发至JSP页面。这种设计使得业务逻辑变更不会直接影响前端展示,提高了系统的可维护性。

数据库设计深度剖析

系统数据库包含用户管理、产品分类、旅游路线、订单管理、消息反馈等八个核心表,下表重点分析三个关键表的设计亮点:

1. 旅游路线表设计

CREATE TABLE tour_route (
    route_id INT PRIMARY KEY AUTO_INCREMENT,
    route_name VARCHAR(100) NOT NULL,
    category_id INT NOT NULL,
    price DECIMAL(10,2) NOT NULL,
    discount_rate DECIMAL(3,2) DEFAULT 1.00,
    departure_city VARCHAR(50),
    destination VARCHAR(100) NOT NULL,
    duration_days INT NOT NULL,
    feature_description TEXT,
    itinerary_details LONGTEXT,
    include_services TEXT,
    exclude_services TEXT,
    booking_notes TEXT,
    main_image_url VARCHAR(255),
    image_gallery JSON,
    stock_quantity INT DEFAULT 0,
    is_active BOOLEAN DEFAULT TRUE,
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_time DATETIME ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (category_id) REFERENCES route_category(category_id)
);

该表设计体现了旅游产品的复杂属性:duration_days字段明确行程时长;itinerary_details使用LONGTEXT类型存储详细的日程安排;include_servicesexclude_services字段清晰界定费用包含与不包含项目,避免消费纠纷;image_gallery采用JSON格式存储多张景点图片,适应现代前端框架的数据交换需求;is_active字段实现软删除功能,保留历史数据的同时隐藏下架产品。

2. 订单表设计

CREATE TABLE customer_order (
    order_id VARCHAR(32) PRIMARY KEY,
    user_id INT NOT NULL,
    route_id INT NOT NULL,
    adult_count INT DEFAULT 1,
    child_count INT DEFAULT 0,
    total_amount DECIMAL(10,2) NOT NULL,
    order_status ENUM('pending','confirmed','paid','completed','cancelled') DEFAULT 'pending',
    contact_name VARCHAR(50) NOT NULL,
    contact_phone VARCHAR(20) NOT NULL,
    id_card_number VARCHAR(18),
    special_requirements TEXT,
    payment_method ENUM('alipay','wechat','bank_transfer'),
    payment_time DATETIME,
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES user(user_id),
    FOREIGN KEY (route_id) REFERENCES tour_route(route_id)
);

订单表设计考虑了旅游业务的特殊性:order_id采用32位字符串而非自增整数,增强业务安全性;adult_countchild_count分别记录成人与儿童数量,支持差异化定价;order_status使用枚举类型明确订单生命周期;id_card_number字段存储游客身份证信息,满足旅游行业实名制要求;special_requirements字段记录客户的特殊需求,提升服务质量。

3. 用户表设计

CREATE TABLE user (
    user_id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(50) UNIQUE NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    real_name VARCHAR(50),
    phone_number VARCHAR(20),
    gender ENUM('male','female','unknown') DEFAULT 'unknown',
    birth_date DATE,
    membership_level ENUM('standard','premium','vip') DEFAULT 'standard',
    registration_time DATETIME DEFAULT CURRENT_TIMESTAMP,
    last_login_time DATETIME,
    is_locked BOOLEAN DEFAULT FALSE
);

用户表设计注重安全性与扩展性:password_hash字段存储加密后的密码,而非明文;membership_level支持会员等级体系,为未来实施差异化营销预留接口;last_login_time记录用户活跃度,支持用户行为分析;is_locked字段实现账户锁定功能,增强系统安全性。

核心功能实现详解

1. 旅游路线智能展示与筛选

系统首页采用分类导航与条件筛选相结合的方式展示旅游产品。用户可按目的地、价格区间、行程天数等多维度筛选路线,后端Servlet通过动态构建SQL查询条件实现精准匹配。

// RouteQueryServlet 中处理多条件查询的核心代码
public class RouteQueryServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        String categoryId = request.getParameter("categoryId");
        String minPrice = request.getParameter("minPrice");
        String maxPrice = request.getParameter("maxPrice");
        String destination = request.getParameter("destination");
        String sortBy = request.getParameter("sortBy");
        
        // 构建动态查询SQL
        StringBuilder sql = new StringBuilder(
            "SELECT * FROM tour_route WHERE is_active = TRUE");
        List<Object> params = new ArrayList<>();
        
        if (categoryId != null && !categoryId.isEmpty()) {
            sql.append(" AND category_id = ?");
            params.add(Integer.parseInt(categoryId));
        }
        if (minPrice != null && !minPrice.isEmpty()) {
            sql.append(" AND price >= ?");
            params.add(new BigDecimal(minPrice));
        }
        if (maxPrice != null && !maxPrice.isEmpty()) {
            sql.append(" AND price <= ?");
            params.add(new BigDecimal(maxPrice));
        }
        if (destination != null && !destination.isEmpty()) {
            sql.append(" AND destination LIKE ?");
            params.add("%" + destination + "%");
        }
        
        // 排序处理
        if ("price_asc".equals(sortBy)) {
            sql.append(" ORDER BY price ASC");
        } else if ("price_desc".equals(sortBy)) {
            sql.append(" ORDER BY price DESC");
        } else {
            sql.append(" ORDER BY created_time DESC");
        }
        
        List<TourRoute> routes = routeService.queryRoutes(sql.toString(), params);
        request.setAttribute("routeList", routes);
        request.getRequestDispatcher("/route-list.jsp").forward(request, response);
    }
}

路线分类浏览

2. 购物车与订单生成机制

用户可将心仪路线加入购物车,系统实时计算总金额并支持批量下单。购物车数据存储在用户会话中,保证用户体验的连贯性。

// CartServlet 中处理购物车操作的核心逻辑
public class CartServlet extends HttpServlet {
    private OrderService orderService = new OrderService();
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        String action = request.getParameter("action");
        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        
        if (cart == null) {
            cart = new Cart();
            session.setAttribute("cart", cart);
        }
        
        if ("add".equals(action)) {
            int routeId = Integer.parseInt(request.getParameter("routeId"));
            int adultCount = Integer.parseInt(request.getParameter("adultCount"));
            int childCount = Integer.parseInt(request.getParameter("childCount"));
            
            TourRoute route = routeService.getRouteById(routeId);
            CartItem item = new CartItem(route, adultCount, childCount);
            cart.addItem(item);
            
            // 返回JSON格式的购物车信息
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            out.print("{\"success\":true, \"totalCount\":" + cart.getTotalCount() + 
                     ", \"totalAmount\":" + cart.getTotalAmount() + "}");
            
        } else if ("checkout".equals(action)) {
            // 生成订单
            User user = (User) session.getAttribute("currentUser");
            if (user == null) {
                response.sendRedirect("login.jsp");
                return;
            }
            
            String orderId = orderService.createOrder(user, cart);
            session.removeAttribute("cart"); // 清空购物车
            
            response.sendRedirect("order-confirm.jsp?orderId=" + orderId);
        }
    }
}

添加到购物车

3. 管理员路线管理功能

后台管理系统提供完整的旅游路线CRUD操作,支持富文本编辑器和多图上传,满足旅游产品描述的复杂需求。

// RouteManagementServlet 中处理路线更新的代码片段
public class RouteManagementServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        RouteService routeService = new RouteService();
        
        try {
            if ("update".equals(action)) {
                TourRoute route = new TourRoute();
                route.setRouteId(Integer.parseInt(request.getParameter("routeId")));
                route.setRouteName(request.getParameter("routeName"));
                route.setCategoryId(Integer.parseInt(request.getParameter("categoryId")));
                route.setPrice(new BigDecimal(request.getParameter("price")));
                route.setDestination(request.getParameter("destination"));
                route.setDurationDays(Integer.parseInt(request.getParameter("durationDays")));
                route.setFeatureDescription(request.getParameter("featureDescription"));
                route.setItineraryDetails(request.getParameter("itineraryDetails"));
                
                // 处理图片上传
                Part imagePart = request.getPart("mainImage");
                if (imagePart != null && imagePart.getSize() > 0) {
                    String imagePath = saveUploadedFile(imagePart);
                    route.setMainImageUrl(imagePath);
                }
                
                boolean success = routeService.updateRoute(route);
                if (success) {
                    request.setAttribute("message", "路线更新成功");
                } else {
                    request.setAttribute("error", "路线更新失败");
                }
            }
            request.getRequestDispatcher("/admin/route-list.jsp").forward(request, response);
            
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", "系统错误: " + e.getMessage());
            request.getRequestDispatcher("/admin/route-edit.jsp").forward(request, response);
        }
    }
    
    private String saveUploadedFile(Part part) throws IOException {
        String fileName = extractFileName(part);
        String savePath = getServletContext().getRealPath("/uploads") + File.separator + fileName;
        part.write(savePath);
        return "/uploads/" + fileName;
    }
}

路线管理界面

4. 订单状态流转与支付集成

系统实现了完整的订单状态机,从待确认、已确认、已支付到已完成或已取消,每个状态转换都有严格的业务规则校验。

// OrderStatusServlet 处理订单状态变更
public class OrderStatusServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        String orderId = request.getParameter("orderId");
        String newStatus = request.getParameter("status");
        User operator = (User) request.getSession().getAttribute("currentUser");
        
        if (!hasPermission(operator, newStatus)) {
            response.sendError(403, "权限不足");
            return;
        }
        
        try {
            Order order = orderService.getOrderById(orderId);
            if (order == null) {
                response.sendError(404, "订单不存在");
                return;
            }
            
            // 状态流转验证
            if (!isValidStatusTransition(order.getOrderStatus(), newStatus)) {
                response.sendError(400, "无效的状态变更");
                return;
            }
            
            boolean success = orderService.updateOrderStatus(orderId, newStatus, operator.getUserId());
            
            if (success && "paid".equals(newStatus)) {
                // 触发支付成功后续操作
                processPostPayment(order);
            }
            
            response.sendRedirect("order-details.jsp?orderId=" + orderId);
            
        } catch (BusinessException e) {
            request.setAttribute("error", e.getMessage());
            request.getRequestDispatcher("order-management.jsp").forward(request, response);
        }
    }
    
    private boolean isValidStatusTransition(String currentStatus, String newStatus) {
        // 定义订单状态流转规则
        Map<String, Set<String>> transitions = new HashMap<>();
        transitions.put("pending", Set.of("confirmed", "cancelled"));
        transitions.put("confirmed", Set.of("paid", "cancelled"));
        transitions.put("paid", Set.of("completed", "cancelled"));
        transitions.put("completed", Set.of());
        transitions.put("cancelled", Set.of());
        
        return transitions.get(currentStatus).contains(newStatus);
    }
}

订单提交界面

实体模型与业务逻辑封装

系统通过精心设计的实体类封装业务数据,每个实体类都包含完整的属性定义、构造方法、getter/setter方法以及业务逻辑方法。以TourRoute实体为例:

public class TourRoute {
    private int routeId;
    private String routeName;
    private int categoryId;
    private BigDecimal price;
    private BigDecimal discountRate;
    private String departureCity;
    private String destination;
    private int durationDays;
    private String featureDescription;
    private String itineraryDetails;
    private String includeServices;
    private String excludeServices;
    private String bookingNotes;
    private String mainImageUrl;
    private String imageGallery; // JSON格式存储
    private int stockQuantity;
    private boolean isActive;
    private Date createdTime;
    private Date updatedTime;
    
    // 计算折后价格的业务方法
    public BigDecimal getDiscountedPrice() {
        return price.multiply(discountRate).setScale(2, RoundingMode.HALF_UP);
    }
    
    // 检查库存状态的业务方法
    public boolean isAvailable() {
        return isActive && stockQuantity > 0;
    }
    
    // 解析图片画廊的辅助方法
    public List<String> getImageGalleryList() {
        if (imageGallery == null || imageGallery.trim().isEmpty()) {
            return new ArrayList<>();
        }
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(imageGallery, new TypeReference<List<String>>(){});
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }
    
    // 标准的getter和setter方法
    public int getRouteId() { return routeId; }
    public void setRouteId(int routeId) { this.routeId = routeId; }
    
    // ... 其他getter/setter方法
}

系统安全与性能优化措施

安全层面,系统实现了多层次的防护机制:用户密码采用BCrypt强哈希算法加密存储;敏感操作如订单状态修改需进行权限验证;所有数据库查询参数均使用预编译语句防止SQL注入;文件上传功能限制文件类型和大小,防止恶意文件上传。

性能优化方面,系统采用数据库连接池减少连接创建开销;频繁访问的静态数据如路线分类信息进行缓存;图片资源通过独立的静态服务器分发,减轻应用服务器压力;数据库表关键字段建立索引,优化查询性能。

未来功能扩展方向

  1. 移动端适配与小程序开发:开发响应式移动端界面或微信小程序,满足移动互联网时代用户的随时预订需求。可采用Vue.js或React重构前端,通过RESTful API与后端交互。

  2. 智能推荐引擎:基于用户浏览历史、预订记录和相似用户行为,实现个性化旅游路线推荐。可集成机器学习算法,分析用户偏好特征。

  3. 多供应商接入平台:扩展系统架构,支持多个旅游供应商入驻,平台提供统一的订单管理和结算系统,向旅游电商平台转型。

  4. 实时库存与价格同步:与酒店、机票等资源系统对接,实现实时库存查询和动态定价,提高资源利用率和竞争力。

  5. 客户关系管理集成:增加客户标签管理、旅行后评价收集、会员积分体系等功能,帮助旅行社建立完整的客户生命周期管理。

该系统通过扎实的技术实现和合理的架构设计,为旅游行业提供了一个功能完整、稳定可靠的在线销售解决方案。其清晰的代码结构和模块化设计为后续功能扩展和维护提供了良好的基础,体现了传统JSP/Servlet技术在现代Web应用开发中的持续价值。

本文关键词
JSPServlet在线旅游平台旅游路线销售源码解析

上下篇

上一篇
没有更多文章
下一篇
没有更多文章