• 【JavaWeb】登录页面记住密码,账号或密码错误


     ✨✨个人主页:沫洺的主页

    📚📚系列专栏: 📖 JavaWeb专栏📖 JavaSE专栏 📖 Java基础专栏📖vue3专栏 

                               📖MyBatis专栏📖Spring专栏📖SpringMVC专栏📖SpringBoot专栏

                               📖Docker专栏📖Reids专栏📖MQ专栏📖SpringCloud专栏     

    💖💖如果文章对你有所帮助请留下三连✨✨

    🌈登录页面优化

    🎨代码优化

    使用Java Web 三层架构模式(Web+Service +Dao/Mapper)优化代码框架

    具体优化:

    • 📌遵循三层架构的分层思想模式,目的是为了“高内聚低耦合”
    • 📌Web表现层主要是指与用户交互的界面。用于接收用户输入的数据和显示处理后用户需要的数据
    • 📌Service业务逻辑层是通过数据访问层拿到存在数据库里的原始数据,然后再对数据进行逻辑上的处理,比如说验证。
    • 📌Dao数据访问层的代码都是对数据库数据的“增删改查”,将存储在数据库中的数据提交给业务层,同时将业务层处理的数据保存到数据库。
    • 📌util包: 对MyBatis获取工厂进行工具封装到util包下,方便开发,提高代码复用性
    • 📌对前端代码进行调整, 通过jsp对el表达式进行展示,与Web层结合实现具体功能

    🎨Web层

    🔎LoginServlet

    1. package com.moming.web;
    2. import com.moming.pojo.User;
    3. import com.moming.service.UserService;
    4. import javax.servlet.*;
    5. import javax.servlet.http.*;
    6. import javax.servlet.annotation.*;
    7. import java.io.IOException;
    8. import java.net.URLEncoder;
    9. @WebServlet("/loginServlet")
    10. public class LoginServlet extends HttpServlet {
    11. UserService userService= new UserService();
    12. @Override
    13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    14. request.setCharacterEncoding("utf-8");
    15. //1.接收用户信息
    16. String username = request.getParameter("username");
    17. String password = request.getParameter("password");
    18. //获取复选框数据
    19. String remeber = request.getParameter("remeber");
    20. //调用service层获取结果
    21. User user = userService.login(username, password);
    22. response.setContentType("text/html;charset=utf-8");
    23. if(user!=null){
    24. //判断用户是否勾选记住密码
    25. //创建cookie对象
    26. //Cookie c_username = new Cookie("username", username);
    27. //URLEncoder.encode(username,"utf-8")解决账号是中文乱码
    28. Cookie c_username = new Cookie("username", URLEncoder.encode(username,"utf-8"));
    29. Cookie c_password = new Cookie("password", password);
    30. //访问此路径下的都带cookie
    31. c_username.setPath("/moming");
    32. c_password.setPath("/moming");
    33. if("1".equals(remeber)){
    34. //设置cookie存活时间一天
    35. c_username.setMaxAge(60*60*24);
    36. c_password.setMaxAge(60*60*24);
    37. }else {
    38. //销毁cookie
    39. c_username.setMaxAge(0);
    40. c_password.setMaxAge(0);
    41. }
    42. //发送cookie
    43. response.addCookie(c_username);
    44. response.addCookie(c_password);
    45. //将登录成功的user对象存储到session中
    46. HttpSession session = request.getSession();
    47. session.setAttribute("user",user);
    48. //存在,登录成功,跳转Servlet
    49. String contextPath = request.getContextPath();
    50. //重定向
    51. response.sendRedirect(contextPath+"/successServlet");
    52. }else {
    53. //不存在,登录失败,跳转的登录页面
    54. request.setAttribute("msg", "用户名或密码错误");
    55. //转发
    56. request.getRequestDispatcher("/login.jsp").forward(request,response);
    57. }
    58. }
    59. @Override
    60. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    61. doGet(request, response);
    62. }
    63. }

    🔎SuccessServlet

    1. package com.moming.web;
    2. import com.moming.pojo.User;
    3. import javax.servlet.*;
    4. import javax.servlet.http.*;
    5. import javax.servlet.annotation.*;
    6. import java.io.IOException;
    7. @WebServlet("/successServlet")
    8. public class SuccessServlet extends HttpServlet {
    9. @Override
    10. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    11. HttpSession session = request.getSession();
    12. //获取session数据
    13. User user =(User) session.getAttribute("user");
    14. request.setAttribute("msg", "登录成功,欢迎"+user.getUsername());
    15. request.getRequestDispatcher("/success.jsp").forward(request,response);
    16. }
    17. @Override
    18. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    19. doGet(request, response);
    20. }
    21. }

    🎨Service层

    🔎UserService

    1. package com.moming.service;
    2. import com.moming.mapper.UserMapper;
    3. import com.moming.pojo.User;
    4. import com.moming.util.SqlSessionFactoryUtils;
    5. import org.apache.ibatis.session.SqlSession;
    6. import org.apache.ibatis.session.SqlSessionFactory;
    7. public class UserService {
    8. //通过工具类获取工厂对象
    9. SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    10. public User login(String username,String password){
    11. //获取sqlSession
    12. SqlSession sqlSession = sqlSessionFactory.openSession();
    13. //获取mapper
    14. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    15. //调用dao层
    16. User user = mapper.selectUser(username, password);
    17. sqlSession.close();
    18. return user;
    19. }
    20. }

    🎨util工具包

    🔎SqlSessionFactoryUtils

    1. package com.moming.util;
    2. import org.apache.ibatis.io.Resources;
    3. import org.apache.ibatis.session.SqlSessionFactory;
    4. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    5. import java.io.IOException;
    6. import java.io.InputStream;
    7. public class SqlSessionFactoryUtils {
    8. //构造方法私有
    9. private SqlSessionFactoryUtils() {
    10. }
    11. private static SqlSessionFactory sqlSessionFactory;
    12. static {
    13. try {
    14. //1.加载mybatis的核心配置文件,获取SqlSessionFactory
    15. //定义配置文件的路径
    16. String resource = "mybatis-config.xml";
    17. //资源加载返回字节输入流
    18. InputStream inputStream = Resources.getResourceAsStream(resource);
    19. //获取工厂
    20. sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    21. } catch (IOException e) {
    22. e.printStackTrace();
    23. }
    24. }
    25. public static SqlSessionFactory getSqlSessionFactory() {
    26. return sqlSessionFactory;
    27. }
    28. }

    🎨前端代码

    🔎login.css

    1. * {
    2. margin: 0;
    3. padding: 0;
    4. }
    5. html {
    6. height: 100%;
    7. width: 100%;
    8. overflow: hidden;
    9. margin: 0;
    10. padding: 0;
    11. background: url(../imgs/img.png) no-repeat 0px 0px;
    12. background-repeat: no-repeat;
    13. background-size: 100% 100%;
    14. -moz-background-size: 100% 100%;
    15. }
    16. body {
    17. display: flex;
    18. align-items: center;
    19. justify-content: center;
    20. height: 100%;
    21. }
    22. #loginDiv {
    23. width: 37%;
    24. display: flex;
    25. justify-content: center;
    26. align-items: center;
    27. height: 300px;
    28. background-color: rgba(255, 255, 255, 0.27);
    29. box-shadow: 7px 7px 17px rgba(52, 56, 66, 0.5);
    30. border-radius: 5px;
    31. }
    32. p {
    33. margin-top: 20px;
    34. margin-left: 20px;
    35. }
    36. #username{
    37. margin-left: 15px;
    38. border-radius: 5px;
    39. border-style: hidden;
    40. height: 30px;
    41. width: 140px;
    42. outline: none;
    43. padding-left: 10px;
    44. }
    45. #password{
    46. margin-left: 15px;
    47. border-radius: 5px;
    48. border-style: hidden;
    49. height: 30px;
    50. width: 140px;
    51. outline: none;
    52. padding-left: 10px;
    53. }
    54. #remeber{
    55. margin-left: 15px;
    56. border-radius: 5px;
    57. border-style: hidden;
    58. height: 15px;
    59. width: 40px;
    60. outline: none;
    61. padding-left: 10px;
    62. }
    63. #username{
    64. width: 200px;
    65. }
    66. #password{
    67. width: 202px;
    68. }
    69. .button {
    70. border-color: cornsilk;
    71. background-color: #5a88c8;
    72. color: aliceblue;
    73. border-style: hidden;
    74. border-radius: 5px;
    75. width: 100px;
    76. height: 31px;
    77. font-size: 16px;
    78. }
    79. #subDiv {
    80. text-align: center;
    81. margin-top: 30px;
    82. }
    83. #loginMsg{
    84. text-align: center;
    85. color: black;
    86. }
    87. #errorMsg{
    88. text-align: center;
    89. color:red;
    90. }

    🔎login.jsp

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. html>
    3. <html lang="en">
    4. <head>
    5. <meta charset="UTF-8">
    6. <title>登录title>
    7. <link href="css/login.css" rel="stylesheet">
    8. head>
    9. <body>
    10. <div id="loginDiv">
    11. <form action="/moming/loginServlet" method="post" id="form">
    12. <h1 id="loginMsg">登录页面h1>
    13. <div id="errorMsg">${msg}div>
    14. <p>账号:<input id="username" name="username" type="text">p>
    15. <p>密码:<input id="password" name="password" value="${cookie.password.value}" type="password">p>
    16. <p>记住密码:<input id="remeber" name="remeber" value="1" ${cookie.username.value!=null?'checked':''} type="checkbox">p>
    17. <div id="subDiv">
    18. <input type="submit" class="button" value="登录">
    19. <a href="register.html">没有账号?点击注册a>
    20. div>
    21. form>
    22. div>
    23. <script>
    24. let v = decodeURI('${cookie.username.value}');
    25. document.getElementById("username").value=v;
    26. script>
    27. body>
    28. html>

    🔎success.jsp

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <html>
    3. <head>
    4. <title>Titletitle>
    5. head>
    6. <body>
    7. <h1>${msg}h1>
    8. body>
    9. html>

    💦总结说明

    对于登录页面的优化有一个注意点就是将原来的login.html改为了login.jsp页面,目的是为了支持el表达式,通过优化代码了解JavaWeb三层架构模式,同时了解CookieSession的区别与使用方式,实现记住密码和页面数据共享,还有就是重定向转发的使用,下一步就是对注册页面的优化

  • 相关阅读:
    【计算机网络】B类IP地址
    2023年全球及中国运动休闲服饰发展趋势分析:互联网经营将成为行业趋势[图]
    L1-009 N个数求和分数 20
    Python问题1:ModuleNotFoundError: No module named ‘numpy‘
    初识网络原理
    Vue 的动态菜单表格数据展示以及分页查询实现
    C#根据任意不重复的数组产生一个连续的新数组且最短,其中新数组最大数和者最大数+1不在该任意数组中
    C语言 宏定义使用方式
    掉瓶子小游戏
    linux设备树节点添加新的复位属性之后设备驱动加载异常问题分析
  • 原文地址:https://blog.csdn.net/HeyVIrBbox/article/details/126805519