当前位置:   article > 正文

JavaWeb——Requst&Response案例(用户登录、注册)_irestresponse 怎么使用

irestresponse 怎么使用

1.环境准备

1.1准备静态页面到项目的webapp目录下


1.2创建db1数据库,创建tb_ user表,创建User实体类

  1. -- 创建数据库
  2. create database db1;
  3. use db1
  4. -- 创建用户表
  5. CREATE TABLE tb_user(
  6. id int primary key auto_increment,
  7. username varchar(20) unique,
  8. password varchar(32)
  9. );
  10. -- 添加数据
  11. INSERT INTO tb_user(username,password) values('zhangsan','123'),('lisi','234');
  12. SELECT * FROM tb_user;

  1. public class User {
  2. private Integer id;
  3. private String username;
  4. private String password;
  5. public Integer getId() {
  6. return id;
  7. }
  8. public void setId(Integer id) {
  9. this.id = id;
  10. }
  11. public String getUsername() {
  12. return username;
  13. }
  14. public void setUsername(String username) {
  15. this.username = username;
  16. }
  17. public String getPassword() {
  18. return password;
  19. }
  20. public void setPassword(String password) {
  21. this.password = password;
  22. }
  23. @Override
  24. public String toString() {
  25. return "User{" +
  26. "id=" + id +
  27. ", username='" + username + '\'' +
  28. ", password='" + password + '\'' +
  29. '}';
  30. }
  31. }


1.3导坐标

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.myproject</groupId>
  7. <artifactId>request_response</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>war</packaging>
  10. <dependencies>
  11. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  12. <dependency>
  13. <groupId>mysql</groupId>
  14. <artifactId>mysql-connector-java</artifactId>
  15. <version>8.0.21</version>
  16. </dependency>
  17. <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
  18. <dependency>
  19. <groupId>org.mybatis</groupId>
  20. <artifactId>mybatis</artifactId>
  21. <version>3.4.6</version>
  22. </dependency>
  23. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  24. <dependency>
  25. <groupId>javax.servlet</groupId>
  26. <artifactId>javax.servlet-api</artifactId>
  27. <version>3.1.0</version>
  28. <scope>provided</scope>
  29. </dependency>
  30. <!-- https://mvnrepository.com/artifact/org.apache.maven/maven-plugin-api -->
  31. <dependency>
  32. <groupId>org.apache.maven</groupId>
  33. <artifactId>maven-plugin-api</artifactId>
  34. <version>2.0</version>
  35. </dependency>
  36. <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
  37. <dependency>
  38. <groupId>commons-io</groupId>
  39. <artifactId>commons-io</artifactId>
  40. <version>2.6</version>
  41. </dependency>
  42. </dependencies>
  43. <build>
  44. <plugins>
  45. <plugin>
  46. <groupId>org.apache.tomcat.maven</groupId>
  47. <artifactId>tomcat7-maven-plugin</artifactId>
  48. <version>2.2</version>
  49. </plugin>
  50. </plugins>
  51. </build>
  52. </project>


1.4创建mybatis-config.xml核心配置文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!--起别名-->
  7. <typeAliases>
  8. <package name="com.pojo"/>
  9. </typeAliases>
  10. <environments default="development">
  11. <environment id="development">
  12. <transactionManager type="JDBC"/>
  13. <dataSource type="POOLED">
  14. <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
  15. <property name="url" value="jdbc:mysql:///db1?serverTimezone=UTC"/>
  16. <property name="username" value="root"/>
  17. <property name="password" value="gyx.l123"/>
  18. </dataSource>
  19. </environment>
  20. </environments>
  21. <mappers>
  22. <!--扫描mapper-->
  23. <package name="com.mapper"/>
  24. </mappers>
  25. </configuration>

1.5 UserMapper.xml映射文件, UserMapper接口

  1. package com.mapper;
  2. import com.pojo.User;
  3. import org.apache.ibatis.annotations.Param;
  4. public interface UserMapper {
  5. User select(@Param("username")String username,@Param("password") String password);
  6. }
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.mapper.UserMapper">
  6. <select id="select" resultType="com.pojo.User">
  7. select * from tb_user WHERE username=#{username} and password=#{password}
  8. </select>
  9. </mapper>

 

2.代码实现

  1. public interface UserMapper {
  2. User select(@Param("username")String username,@Param("password") String password);
  3. User selectByUsername(String username);
  4. void add(User user);
  5. }
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.mapper.UserMapper">
  6. <select id="select" resultType="com.pojo.User">
  7. select * from tb_user WHERE username=#{username} and password=#{password}
  8. </select>
  9. <select id="selectByUsername" resultType="com.pojo.User">
  10. SELECT * from tb_user WHERE username=#{username}
  11. </select>
  12. <insert id="add">
  13. INSERT INTO tb_user VALUES (null,#{username},#{password})
  14. </insert>
  15. </mapper>

2.1用户登录

  1. @WebServlet("/loginServlet")
  2. public class LoginServlet extends HttpServlet {
  3. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  4. doGet(request, response);
  5. }
  6. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  7. //1.接受用户名和密码
  8. String username1 = request.getParameter("username");
  9. String password1 = request.getParameter("password");
  10. //2.调用Mybatis完成查询
  11. //2.1获取SqlSessionFactory对象:从官网复制
  12. String resource = "mybatis-config.xml";
  13. InputStream inputStream = Resources.getResourceAsStream(resource);
  14. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  15. //2.2获取SqlSession对象
  16. SqlSession sqlSession = sqlSessionFactory.openSession();
  17. //2.3获取mapper对象
  18. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  19. //2.4调用方法
  20. User select = mapper.select(username1, password1);
  21. //2.5释放资源
  22. sqlSession.close();
  23. //解析html,并解决中文乱码
  24. response.setContentType("text/html;charset=utf-8");
  25. //获取字符流
  26. PrintWriter writer = response.getWriter();
  27. //3.判断
  28. if (select != null) {
  29. writer.write("登录成功");
  30. } else {
  31. writer.write("登录失败");
  32. }
  33. }
  34. }

2.2用户注册

 

  1. @WebServlet("/registerServlet")
  2. public class RegisterServlet extends HttpServlet {
  3. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  4. doGet(request, response);
  5. }
  6. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  7. //1.接受用户名和密码
  8. String username = request.getParameter("username");
  9. String password = request.getParameter("password");
  10. //2.封装用户对象
  11. User user = new User();
  12. user.setUsername(username);
  13. user.setPassword(password);
  14. //3..调用Mybatis完成查询
  15. //3.1获取SqlSessionFactory对象:从官网复制
  16. String resource = "mybatis-config.xml";
  17. InputStream inputStream = Resources.getResourceAsStream(resource);
  18. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  19. //3.2获取SqlSession对象
  20. SqlSession sqlSession = sqlSessionFactory.openSession();
  21. //3.3获取mapper对象
  22. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  23. //3.4调用方法
  24. User select = mapper.selectByUsername(username);
  25. //解析html,并解决中文乱码
  26. response.setContentType("text/html;charset=utf-8");
  27. //获取字符流
  28. PrintWriter writer = response.getWriter();
  29. //4.判断
  30. if (select == null) {
  31. //添加数据
  32. mapper.add(user);
  33. //提交事务
  34. sqlSession.commit();
  35. //释放资源
  36. sqlSession.close();
  37. //提示信息
  38. writer.write("注册成功");
  39. } else {
  40. //提示信息
  41. writer.write("用户名已存在");
  42. }
  43. }
  44. }

3.创建SqlSessionFactory代码优化

  1. public class SqlSessionFactoryUtils {
  2. private static SqlSessionFactory sqlSessionFactory;
  3. static {
  4. try {
  5. String resource = "mybatis-config.xml";
  6. InputStream inputStream = Resources.getResourceAsStream(resource);
  7. sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. public static SqlSessionFactory getSqlSessionFactory() {
  13. return sqlSessionFactory;
  14. }
  15. }

 

  1. @WebServlet("/loginServlet")
  2. public class LoginServlet extends HttpServlet {
  3. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  4. doGet(request, response);
  5. }
  6. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  7. //1.接受用户名和密码
  8. String username1 = request.getParameter("username");
  9. String password1 = request.getParameter("password");
  10. //2.调用Mybatis完成查询
  11. //2.1获取SqlSessionFactory对象:从官网复制
  12. // String resource = "mybatis-config.xml";
  13. // InputStream inputStream = Resources.getResourceAsStream(resource);
  14. // SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  15. SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
  16. //2.2获取SqlSession对象
  17. SqlSession sqlSession = sqlSessionFactory.openSession();
  18. //2.3获取mapper对象
  19. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  20. //2.4调用方法
  21. User select = mapper.select(username1, password1);
  22. //2.5释放资源
  23. sqlSession.close();
  24. //解析html,并解决中文乱码
  25. response.setContentType("text/html;charset=utf-8");
  26. //获取字符流
  27. PrintWriter writer = response.getWriter();
  28. //3.判断
  29. if (select != null) {
  30. writer.write("登录成功");
  31. } else {
  32. writer.write("登录失败");
  33. }
  34. }
  35. }

  1. @WebServlet("/registerServlet")
  2. public class RegisterServlet extends HttpServlet {
  3. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  4. doGet(request, response);
  5. }
  6. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  7. //1.接受用户名和密码
  8. String username = request.getParameter("username");
  9. String password = request.getParameter("password");
  10. //2.封装用户对象
  11. User user = new User();
  12. user.setUsername(username);
  13. user.setPassword(password);
  14. //3..调用Mybatis完成查询
  15. //3.1获取SqlSessionFactory对象:从官网复制
  16. // String resource = "mybatis-config.xml";
  17. // InputStream inputStream = Resources.getResourceAsStream(resource);
  18. // SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  19. SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
  20. //3.2获取SqlSession对象
  21. SqlSession sqlSession = sqlSessionFactory.openSession();
  22. //3.3获取mapper对象
  23. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  24. //3.4调用方法
  25. User select = mapper.selectByUsername(username);
  26. //解析html,并解决中文乱码
  27. response.setContentType("text/html;charset=utf-8");
  28. //获取字符流
  29. PrintWriter writer = response.getWriter();
  30. //4.判断
  31. if (select == null) {
  32. //添加数据
  33. mapper.add(user);
  34. //提交事务
  35. sqlSession.commit();
  36. //释放资源
  37. sqlSession.close();
  38. //提示信息
  39. writer.write("注册成功");
  40. } else {
  41. //提示信息
  42. writer.write("用户名已存在");
  43. }
  44. }
  45. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号