Idea+mybatis+Servlet+mysql实现页面登录和注册

登录html

<form action="/tomcat-demo2/loginDemo" method="get">
                    <div class="inputBox">
                        <input type="text" name="name" placeholder="姓名">

                    </div>
                    <div class="inputBox">
                        <input type="password" name="password" placeholder="密码">

                    </div>
                    <div class="inputBox">
                        <input type="submit" value="登录">
                    </div>
                </form>

登录servlet

@WebServlet("/loginDemo")
public class LoginDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String name=new String(request.getParameter("name").getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        String password=new String(request.getParameter("password").getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        response.setContentType("text/html;charset=utf-8");
        //1、加载mybatis核心组件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2、获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3、获取UserMapper对象
        UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
        //4、传参
        User user=usermapper.select(name,password);
        //5、关闭资源
        sqlSession.close();
        //6、对比
        PrintWriter writer = response.getWriter();
        if(user==null){
            writer.write("登录失败~");
            System.out.println("falure");

        }else {
            writer.write("登录成功~");
            System.out.println("ok");
        }



    }

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

    }
}

注册html

<form action="/tomcat-demo2/registerDemo" method="get">
                    <div class="inputBox">
                        <input type="text" name="name" placeholder="姓名">

                    </div>
                    <div class="inputBox">
                        <input type="password" name="password" placeholder="密码">

                    </div>
                    <div class="inputBox">
                        <input type="submit" value="注册">
                    </div>



                </form>

注册servlet

@WebServlet("/registerDemo")
public class registerDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = new String(request.getParameter("name").getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        String password = new String(request.getParameter("password").getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        response.setContentType("text/html;charset=utf-8");
        User user=new User();
        user.setName(name);
        user.setPassword(password);
        //1、加载mybatis核心组件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2、获取sqlsession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3、获取Mapper
        UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
        //4、判断输入姓名是否重复
        User user1=usermapper.selectByName(name);
        if(user1==null){
            usermapper.add(user);
            sqlSession.commit();
            sqlSession.close();
        }else {
            response.getWriter().write("用户名已存在");
        }
    }

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

    }
}

pojo类

public class User {
    private int id;
    private String name;
    private String password;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

mapper和mapperxml

public interface UserMapper {
    @Select("select * from stu_info where name=#{name} and password=#{password}")
    User select(@Param("name") String name, @Param("password") String password);
    @Select("select * from stu_info where name=#{name}")
    User selectByName(@Param("name") String name);
    @Insert("insert into stu_info values(null, #{name},#{password})")
    void add(User user);
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liyin.mapper.UserMapper">
    <resultMap id="userMapper" type="User">
        <result column="password" property="password"/>
    </resultMap>

</mapper>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>tomcat-demo2</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>tomcat-demo2 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.6</version>

    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.5</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.37</version>
    </dependency>
    <!--junit单元测试坐标-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.3.0-alpha10</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>

      </plugin>


    </plugins>
  </build>


</project>

mybatis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.liyin.pojo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!--数据库连接信息-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///myemployees?SSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <!--加载sql映射文件-->
    <mappers>
<!--        <mapper resource="com/tiger/mapper/UserMapper.xml"/>-->
        <!--Mapper代理方式 包扫描-->
        <package name="com.liyin.mapper"/>
    </mappers>
</configuration>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

油光发亮的小猛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值