Day05JavaWeb [Cookie and Session] Comprehensive case registration

Comprehensive case-registration analysis

  • (1) Analysis
  • (2) Business logic
    Submit the registration information to the Sevlet
    Servlet to obtain the user name to determine whether it already exists.
    If it does not exist, add it, and the result is successful.
    If it exists, return the registered information directly and let the user continue to register
    Insert picture description here

Comprehensive case-registration logic implementation

test\java\com\wzx\TestUserService.java
looks up the user name. Existing means that the user has been registered and cannot be registered again, otherwise it is not registered. Save the user data and prompt the registration is successful.

  @Test
    public void test02(){
    
    
        //1:获取参数
        String username= "tony";
        String password= "bbb";
        User user = new User(username,password);
        //2:调用登录
        UserService userService = new UserService();
        int code = userService.register(user);
        //3:查看结果
        System.out.println(code);
    }

The username of src\com\wzx\service\UserService.java
cannot be the same, so you can check if the user exists first, and register if it does not exist

 public int register(User user) {
    
    
        //查找  用户名是否已存在,
        UserDao dao = new UserDao();
        int count = dao.findByName(user.getUsername());
        if(count == 0){
    
    //未注册
            //不存在,进行保存,
            dao.add(user);
            return 1;
        }else{
    
    
            //否则不保存提示失败
            return -1;
        }
    }


The Dao method for almost all projects in src\com\wzx\dao\UserDao.java is addXxx deleteXxx findXxx updateXxx

  public int findByName(String username) {
    
    
        for(User u:userList){
    
    
            if(username.equals(u.getUsername())){
    
    
                return 1;//用户名已注册
            }
        }
        return 0;//用户未注册
    }

    public void add(User user) {
    
    
        userList.add(user);
    }

Comprehensive case-registration UI interface


Add a link to login.jsp so that newcomers who do not have a user password can get their account password by registering

<a href="/taobao/register.jsp">注册网易邮箱</a>



register.jsp

Fill in user information on the registration page

<!-- 编写表单页面-->
<font color="red">${error_msg}</font>
    <form method="post" action="/taobao/register">
        用户名:<input name="username" type="text"/><br/>
        密码:<input name="password" type="text"/><br/>
        确认密码:<input name="password2" type="text"/><br/>
        email<input name="email" type="text"/><br/>
        姓名<input name="name" type="text"/><br/>
        性别<input name="gender" type="radio" value="1"/><input name="gender" type="radio" value="2"/><br/>
        出生日期<input name="birthday" type="date"/><br/>
        <input  type="submit"/><br/>
    </form>

Comprehensive Case-Register Servlet

src\com\wzx\web\RegisterServlet.java


@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        //1:获取参数
        request.setCharacterEncoding("utf-8");
        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        try {
    
    
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
    
    
            e.printStackTrace();
        } catch (InvocationTargetException e) {
    
    
            e.printStackTrace();
        }
        //2:调用注册
        UserService userService = new UserService();
        int code = userService.register(user);
        //3:查看结果
        System.out.println(code);
        System.out.println(user);
        //如果用户注册成功,回到登录页面去登录
        if(code == 1){
    
    
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }else{
    
    
            //如果用户注册失败,回到注册页面重新注册
            request.setAttribute("error_msg","该用户名已被注册");
            request.getRequestDispatcher("/register.jsp").forward(request,response);
        }

    }
}
  • The logic of a web project is complicated
  • Other web parts can refer to the login module

Guess you like

Origin blog.csdn.net/u013621398/article/details/108528885