Java遇见HTML---JSP篇(阶段项目-用Model1改造用户登录)

  • Users
package com.po;

import javax.xml.bind.helpers.ParseConversionEventImpl;

/**
 * Created by NLTE on 2016/11/29 0029.
 */
public class Users {
    private String username;
    private String password;

    public Users() {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
  • UserDao
package com.dao;

import com.po.Users;

/**用户业务逻辑类
 * Created by NLTE on 2016/11/29 0029.
 */
public class UserDao {
    public boolean userLogin(Users users){
        if ("admin".equals(users.getUsername())&&"admin".equals(users.getPassword())){
            return true;
        }else {
            return false;
        }
    }
}
  • login
<%--
  Created by IntelliJ IDEA.
  User: NLTE
  Date: 2016/11/29 0029
  Time: 21:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登陆</title>
</head>
<body>
<div>
    <form method="post" action="dologin.jsp">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="登陆"></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>
  • dologin
<%--
  Created by IntelliJ IDEA.
  User: NLTE
  Date: 2016/11/29 0029
  Time: 21:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="loginUser" class="com.po.Users" scope="page"/>
<jsp:useBean id="userDao" class="com.dao.UserDao" scope="page"/>
<jsp:setProperty name="loginUser" property="*"/>
<%
    request.setCharacterEncoding("UTF-8");//防止中文乱码

    if (userDao.userLogin(loginUser)){
        session.setAttribute("username", loginUser.getUsername());
        session.setAttribute("password", loginUser.getPassword());
        request.getRequestDispatcher("login-success.jsp").forward(request, response);
    }else {
        response.sendRedirect("login-failure.jsp");
    }
%>
  • login-success
<%--
  Created by IntelliJ IDEA.
  User: NLTE
  Date: 2016/11/29 0029
  Time: 21:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登陆成功</h1>
<hr>
<%
    String username = "";
    String password = "";
    if (session.getAttribute("username")!=null){
        username = session.getAttribute("username").toString();
    }
    if (session.getAttribute("password")!=null){
        password = session.getAttribute("password").toString();
    }
%>
<br>
用户名:<%=username%><br>
密码:<%=password%>
</body>
</html>
  • login-failure
<%--
  Created by IntelliJ IDEA.
  User: NLTE
  Date: 2016/11/29 0029
  Time: 21:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登陆失败</h1>
<hr>
<a href="login.jsp">返回重新登陆</a>
</body>
</html>
点赞