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

JavaWeb商城项目(Day04)Web层(注册页面的Servlet)

程序员文章站 2022-05-03 23:40:57
注册的Servlet程序public class RegistServlet extends HttpServlet { // 因为web层只能调用服务层 不能直接调用持久层 private UserService userService = new UserServiceImpl(); @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws Servl...

注册的Servlet程序

public class RegistServlet extends HttpServlet {
    // 因为web层只能调用服务层 不能直接调用持久层
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
        1.获取请求的参数
        2.检查验证码是否可用
            正确
                3.检查用户名是否可用
                    不可用
                            跳回注册页面
                    可用
                        4.调用Service层保存到数据库
                            跳转到注册成功页面regist_success.html

            不正确
                 跳回注册页面
         */
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String repwd = req.getParameter("repwd");
        String email = req.getParameter("email");
        String code = req.getParameter("code"); // 由服务器生成验证码
        if ("bnbnp".equalsIgnoreCase(code)) {
            if (userService.existsUsername(username)) {
                System.out.println("用户名不可用"+username);
                req.getRequestDispatcher("/pages/user/regist.html").forward(req,resp);
            }else {
                System.out.println("用户名可用"+username);
                userService.registUser(new User(null,username,password,email));
                req.getRequestDispatcher("/pages/user/regist_success.html").forward(req,resp);
            }
        }else {
            System.out.println("验证码错误:"+code);
            req.getRequestDispatcher("/pages/user/regist.html").forward(req,resp);
        }
    }
}

本文地址:https://blog.csdn.net/m0_47119598/article/details/109629912