Javaweb实现用户登录注册案例

Javaweb实现用户登录注册案例_第1张图片

项目结构

Javaweb实现用户登录注册案例_第2张图片

pom.xml

    

        
            mysql
            mysql-connector-java
            8.0.13
        
        
            com.alibaba
            druid
            1.2.8
        

        
            org.projectlombok
            lombok
            1.18.6
        

        
            org.mybatis
            mybatis
            3.4.1
        


        
            junit
            junit
            4.12
        
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        

        
        
            commons-io
            commons-io
            2.4
        
    

 User实体类

package com.cong.pojo;

public class User {

    private Integer id;
    private String username;
    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

登录页面 from表单提交到

/loginServlet 路径




    
    login
    



LOGIN IN

Username:

Password:

mapper接口

public interface UserMapper {

    User selectUsernameAndPassword(@Param("username") String username, @Param("password") String password);

    //根据用户名查询用户是否存在
    User selectUserByUsername(String username);

    //注册用户
    int insertUser(User user);
}

mybatis映射文件




    
        insert into tb_user(username, password) values(#{username}, #{password})
    

    
    

servlet代码


@WebServlet(value = "/loginServlet")
public class loginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取用户名密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //调用mapper
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = build.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //在数据库中查询是否存在
        User user = userMapper.selectUsernameAndPassword(username,password);
        if (user != null) {
            //用户存在 返回登录成功
            //设置中文乱码
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("登录成功");
        } else {
            //用户不存在 返回登录失败
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("登录失败");
        }
        sqlSession.close();

    }

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

Javaweb实现用户登录注册案例_第3张图片

页面代码 from表单提交到地址

/RegisterServlet" 



    
    欢迎注册
    



欢迎注册

已有帐号? 登录
用户名
密码

 servlet代码


@WebServlet(value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取用户名
        String username = request.getParameter("username");
        //获取密码
        String password = request.getParameter("password");

        //封装为User对象
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        //查询用户名是否存在
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = build.openSession(true);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User u = userMapper.selectUserByUsername(username);
        if (u != null) {
            //用户名已存在
            //乱码问题
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("用户名已存在");
        } else {
            //用户名不存在 可以注册 提交事务
            userMapper.insertUser(user);
            sqlSession.close();
            //注册成功
            //跳转到登录页面
            response.sendRedirect("/login.html");
        }
    }

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

你可能感兴趣的:(java-ee)