• 【SpringMVC】使用三层架构实现登录,注册。(上篇)


    构思

    在这里插入图片描述

    界面层

    1.jsp
    在这里插入图片描述
    【见名知义】

    failed.jsp-->失败页面,登录、注册失败就跳转至失败页面
    index.jsp-->默认生成的界面,没什么用
    login.jsp-->登录界面
    register.jsp-->注册界面
    welcome.jsp-->欢迎界面,登录成功就跳转至欢迎界面
    

    index.jsp
    在这里插入图片描述
    login.jsp
    在这里插入图片描述

    <%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2019/8/13
      Time: 17:29
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登录页面</title>
    </head>
    <body>
    <form action="${pageContext.request.contextPath}/login" method="post">
        <p>用户名:<input type="text" placeholder="请输入用户名" name="userName"></p>
        <p>密码:<input type="password" placeholder="请输入密码名" name="userPwd"></p>
        <p><input type="reset" value="重置"><input type="submit" value="登录"></p>
        <a href="${pageContext.request.contextPath}/register.jsp">
            没有账号?前往注册
        </a>
    </form>
    </body>
    </html>
    
    

    register.jsp
    在这里插入图片描述
    failed.jsp
    在这里插入图片描述
    welcome.jsp
    在这里插入图片描述
    2.Servlet
    在这里插入图片描述
    1.BeanUtils将表单信息塞进User对象里面储存,抽取一个工具类CreateUser

    public final class CreateUser {
        public static User createUser(HttpServletRequest request, HttpServletResponse response){
            User nowUser=new User();
            try {//解决乱码
            request.setCharacterEncoding("utf-8");
            //收集用户信息
            Map<String, String[]> parameterMap = request.getParameterMap();
            //创建当前User对象
            //User nowUser=new User();
    
            //使用BeanUtils将信息塞入nowUser对象
                BeanUtils.populate(nowUser,parameterMap);
            } catch (IllegalAccessException | UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return nowUser;
        }
    }
    
    

    2.登录LoginServlet

    @WebServlet("/login")
    public class LoginServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            User nowUser=CreateUser.createUser(request,response);
            System.out.println(nowUser.getUserName());
            //使用UserService服务,注册
            UserServiceImpl userService=new UserServiceImpl();
            boolean login = userService.login(nowUser);
            if(login){
                HttpSession session = request.getSession();
                session.setAttribute("user",nowUser);
                response.sendRedirect("welcome.jsp");
            }else {
                response.sendRedirect("failed.jsp");
            }
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
        }
    }
    

    3.注册RegisterServlet

    @WebServlet("/register")
    public class RegisterServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            User nowUser=CreateUser.createUser(request,response);
            System.out.println(nowUser.getUserName());
    
            //使用UserService服务,注册
            UserServiceImpl userService=new UserServiceImpl();
            boolean register = userService.register(nowUser);
            if(register){
                HttpSession session = request.getSession();
                session.setAttribute("user",nowUser);
                response.sendRedirect("login.jsp");
            }else {
                response.sendRedirect("failed.jsp");
            }
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
        }
    }
    
    

    逻辑业务层、数据访问层请看下篇
    https://blog.csdn.net/qq_42158942/article/details/99474259

  • 相关阅读:
    这个Pandas函数可以自动爬取Web图表
    /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by /usr/local/python3.6/lib/python3.6/site-packages/paddle/fluid/core_avx.so)
    kubernetes yaml详解
    centos 安装kubernetes
    初识kafka-connect
    Docker1:本地连接Docker中的mysql,实现数据同步
    git分支管理规范
    cat 配置告警规则
    Java的即时编译(Just In Time, JIT)及其优化
    java自定义实现一个缓存器
  • 原文地址:https://www.cnblogs.com/BIG-BOSS-ZC/p/11807336.html
Copyright © 2020-2023  润新知