D06 Sping Boot 入门 Sping框架--Java Web之书城项目(二)实现用户注册功能

7、实现用户注册功能

/*
* 1、获取请求参数
* 2、检查验证码是否正确
* Ⅰ、正确
* 3、检查用户名是否可用
* ①、可用
* 调用Service保存到数据库
* 跳到注册成功页面
* ②、不可用
* 跳回注册页面
* Ⅱ、错误
* 跳回注册页面
* */

  Ⅰ、在com.gychen.web中新建RegistServlet类继承httpServlet

 1 package com.gychen.web;
 2 
 3 import com.gychen.pojo.User;
 4 import com.gychen.service.UserService;
 5 import com.gychen.service.impl.UserServiceImpl;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import java.io.IOException;
12 
13 public class RegistServlet extends HttpServlet {
14 
15     private UserService userService = new UserServiceImpl();
16     @Override
17     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
18 
19 //        1、获取请求参数
20         String username = req.getParameter("username");
21         String password = req.getParameter("password");
22         String email = req.getParameter("email");
23         String verification_code = req.getParameter("verification_code");
24 //        2、检查验证码是否正确
25         if("abcde".equalsIgnoreCase(verification_code)){
26 
27 //          Ⅰ、正确
28 //              3、检查用户名是否可用
29             if(userService.existUsername(username)){
30 //                  ①、不可用
31 //                      跳回注册页面
32                 System.out.println("用户名["+username+"]已存在");
33                 req.getRequestDispatcher("/pages/user/regist.html").forward(req,resp);
34             }else{
35 //                  ②、可用
36                 System.out.println("用户名["+username+"]可用");
37 //                      调用Service保存到数据库
38                 userService.registUser(new User(null,username,password,email));
39 //                      跳到注册成功页面
40                 req.getRequestDispatcher("/pages/user/regist_success.html").forward(req,resp);
41             }
42 
43         }else {
44 //          Ⅱ、错误
45 //              跳回注册页面
46             System.out.println("验证码错误["+verification_code+"]错误");
47             req.getRequestDispatcher("/pages/user/regist.html").forward(req,resp);
48         }
49 
50 
51     }
52 }

  

猜你喜欢

转载自www.cnblogs.com/nuister/p/12606178.html