欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java web 实训项目报告: Simonshop 西蒙购物网(下)

程序员文章站 2024-01-25 08:33:11
...

LoginServlet

package net.typ.shop.servlet;


import java.io.IOException;

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 net.typ.shop.bean.User;
import net.typ.shop.service.UserService;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置请求对象的字符编码
        request.setCharacterEncoding("utf-8");
        // 获取会话对象
        HttpSession session = request.getSession();
        // 获取用户名
        String username = request.getParameter("username");
        // 获取密码
        String password = request.getParameter("password");
        // 定义用户服务对象
        UserService userService = new UserService();
        // 执行登录方法,返回用户实体
        User user = userService.login(username, password);
        // 判断用户登录是否成功
        if (user != null) {
            // 设置session属性
            session.setMaxInactiveInterval(5 * 60);
            session.setAttribute("username", username);
            session.removeAttribute("loginMsg");
            // 根据用户权限跳转到不同页面
            if (user.getPopedom() == 0) {
                response.sendRedirect(request.getContextPath() + "/backend/management.jsp");
            } else if (user.getPopedom() == 1) {
                response.sendRedirect(request.getContextPath() + "/showCategory");
            }
        } else {
            // 设置session属性
            session.setAttribute("loginMsg", "用户名或密码错误!");
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
登录成功进入后台:

Java web 实训项目报告: Simonshop 西蒙购物网(下)

Java web 实训项目报告: Simonshop 西蒙购物网(下)

登录成功进入前台显示类别

Java web 实训项目报告: Simonshop 西蒙购物网(下)

Java web 实训项目报告: Simonshop 西蒙购物网(下)

Java web 实训项目报告: Simonshop 西蒙购物网(下)

 

Java web 实训项目报告: Simonshop 西蒙购物网(下)

登录失败:

Java web 实训项目报告: Simonshop 西蒙购物网(下)

Java web 实训项目报告: Simonshop 西蒙购物网(下)

Java web 实训项目报告: Simonshop 西蒙购物网(下)

 

LogoutServlet:

package net.typ.shop.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 让session失效
        request.getSession().invalidate();
        // 重定向到登录页面
        response.sendRedirect(request.getContextPath() + "/login.jsp");
        System.out.println("用户注销成功!");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

普通用户登录:

Java web 实训项目报告: Simonshop 西蒙购物网(下)

Java web 实训项目报告: Simonshop 西蒙购物网(下)

Java web 实训项目报告: Simonshop 西蒙购物网(下)

 

RegisterServlet

package net.typ.shop.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.Timestamp;

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 net.typ.shop.bean.User;
import net.typ.shop.service.UserService;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置请求对象的字符编码
        request.setCharacterEncoding("utf-8");
        // 获取session对象
        HttpSession session = request.getSession();

        // 获取用户名
        String username = request.getParameter("username");
        // 获取密码
        String password = request.getParameter("password");
        // 获取电话号码
        String telephone = request.getParameter("telephone");
        // 设置注册时间(时间戳对象)
        Timestamp registerTime = new Timestamp(System.currentTimeMillis());
        // 设置用户为普通用户
        int popedom = 1;

        // 创建用户对象
        User user = new User();
        // 设置用户对象信息
        user.setUsername(username);
        user.setPassword(password);
        user.setTelephone(telephone);
        user.setRegisterTime(registerTime);
        user.setPopedom(popedom);

        // 创建UserService对象
        UserService userService = new UserService();
        // 调用UserService对象的添加用户方法
        int count = userService.addUser(user);

        // 判断是否注册成功
        if (count > 0) {
            // 设置session属性
            session.setAttribute("registerMsg", "恭喜,注册成功!");
            // 重定向到登录页面
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            // 在控制台输出测试信息
            System.out.println("恭喜,注册成功,跳转到登录页面!");
        } else {
            // 设置session属性
            session.setAttribute("registerMsg", "遗憾,注册失败!");
            // 重定向到注册页面
            response.sendRedirect(request.getContextPath() + "/frontend/register.jsp");
            // 在控制台输出测试信息
            System.out.println("遗憾,注册失败,跳转到注册页面!");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

Java web 实训项目报告: Simonshop 西蒙购物网(下)

 注册成功跳转到登录页面:

 Java web 实训项目报告: Simonshop 西蒙购物网(下)

Java web 实训项目报告: Simonshop 西蒙购物网(下)

数据库中查看已注册成功

Java web 实训项目报告: Simonshop 西蒙购物网(下)

注册失败:

Java web 实训项目报告: Simonshop 西蒙购物网(下)

Java web 实训项目报告: Simonshop 西蒙购物网(下)

ShowCategoryServlet:

package net.typ.shop.servlet;

import java.io.IOException;
import java.util.List;

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 net.typ.shop.bean.Category;
import net.typ.shop.service.CategoryService;

@WebServlet("/showCategory")
public class ShowCategoryServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 创建类别服务对象
        CategoryService categoryService = new CategoryService();
        // 获取全部商品类别
        List<Category> categories = categoryService.findAllCategories();
        // 获取session对象
        HttpSession session = request.getSession();
        // 把商品类别列表以属性的方式保存到session里
        session.setAttribute("categories", categories);
        // 重定向到显示商品类别页面(showCategory.jsp)
        response.sendRedirect(request.getContextPath() + "/frontend/showCategory.jsp");
        // 在服务器控制台输出测试信息
        for (Category category: categories) {
            System.out.println(category);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

输入showCategory:

Java web 实训项目报告: Simonshop 西蒙购物网(下)

Java web 实训项目报告: Simonshop 西蒙购物网(下)

ShowProductServlet:

package net.typ.shop.servlet;

import java.io.IOException;
import java.util.List;

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 net.typ.shop.bean.Product;
import net.typ.shop.service.CategoryService;
import net.typ.shop.service.ProductService;

@WebServlet("/showProduct")
public class ShowProductServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取类别标识符
        int categoryId = Integer.parseInt(request.getParameter("categoryId"));
        // 创建商品类别服务对象
        CategoryService categoryService = new CategoryService();
        // 由类别标识符获取类别名
        String categoryName = categoryService.findCategoryById(categoryId).getName();
        // 创建商品服务对象
        ProductService productService = new ProductService();
        // 获取指定商品类别的商品列表
        List<Product> products = productService.findProductsByCategoryId(categoryId);
        // 获取session对象
        HttpSession session = request.getSession();
        // 把商品列表对象以属性的方式保存到session里
        session.setAttribute("products", products);
        // 重定向到显示商品信息页面
        response.sendRedirect(request.getContextPath() + "/frontend/showProduct.jsp?categoryName=" + categoryName);
        // 在服务器端控制台输出测试信息
        for (int i = 0; i < products.size(); i++) {
            System.out.println(products.get(i));
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

Java web 实训项目报告: Simonshop 西蒙购物网(下)

结果:

Java web 实训项目报告: Simonshop 西蒙购物网(下)

控制台:

Java web 实训项目报告: Simonshop 西蒙购物网(下)

失败:

Java web 实训项目报告: Simonshop 西蒙购物网(下)

修改代码:

Java web 实训项目报告: Simonshop 西蒙购物网(下)

Java web 实训项目报告: Simonshop 西蒙购物网(下)

Java web 实训项目报告: Simonshop 西蒙购物网(下)