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

构思

【SpringMVC】使用三层架构实现登录,注册。(上篇)_第1张图片

界面层

1.jsp
【SpringMVC】使用三层架构实现登录,注册。(上篇)_第2张图片
【见名知义】

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

index.jsp
【SpringMVC】使用三层架构实现登录,注册。(上篇)_第3张图片
login.jsp
【SpringMVC】使用三层架构实现登录,注册。(上篇)_第4张图片

<%--
  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
【SpringMVC】使用三层架构实现登录,注册。(上篇)_第5张图片
failed.jsp
【SpringMVC】使用三层架构实现登录,注册。(上篇)_第6张图片
welcome.jsp
【SpringMVC】使用三层架构实现登录,注册。(上篇)_第7张图片
2.Servlet
【SpringMVC】使用三层架构实现登录,注册。(上篇)_第8张图片
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

你可能感兴趣的:(Java,Spring)