登录功能的实现

登录页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<form action="/web2/login.do" method="post">
    <table>
        <tr>
            <td>账号:<input type="text" name="username" placeholder="请输入账号" required></td>
        </tr>
        <tr>
            <td>密码:<input type="password" name="password" placeholder="请输入密码" required></td>
        </tr>
        <tr>
            <td><input type="submit" value="登录"></td>
        </tr>
    </table>
</form>
</body>
</html>

主页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页面</title>
</head>
<body>
<h1>恭喜</h1>
</body>
</html>

web.xml

<!--访问应用时默认打开登录页面-->
<welcome-file-list>
  <welcome-file>login.html</welcome-file>
</welcome-file-list>

获取数据库连接

package com.javakc.web2.Utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

//获得数据库的链接
public class Utils {
    public static Connection getConn() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "  ";
        String user = "  ";
        String password = " ";
        Connection conn = DriverManager.getConnection(url, user, password);
        return conn;
    }
}

登录方法

package com.javakc.web2.dao;

import com.javakc.web2.Utils.Utils;

import java.sql.*;

//登录方法
public class LoginDao {
    public static boolean login(String username, String password) {
        //通过工具类取得数据连接
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int count = 0;
        try {
            conn = Utils.getConn();
            String sql = "select count(1)from login where username=? and password=?";
            //创建能操作数据库的PreparedStatement对象
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, username);
            stmt.setString(2, password);
            rs = stmt.executeQuery();
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                stmt.close();
                rs.close();
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return (count == 0) ? false : true;
    }
}

LoginServlet

package com.javakc.web2.servlet;

import com.javakc.web2.dao.LoginDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(
        name = "LoginServlet",
        urlPatterns = "/login.do",
        loadOnStartup = 0
)
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.收集客户端登录参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //2.验证登录参数是否符合要求
        if (username != null && username.trim().length() > 0 && password != null && password.trim().length() > 0) {
            //2.1验证成功继续执行
            //3.验证账号密码是否正确
            boolean success = LoginDao.login(username, password);
            if (success) {
                request.getSession();
                //3.1登录成功,展示欢迎界面
                response.sendRedirect("/web2/index.html");
                return;
            }
        } else {
            //2.2验证失败直接跳转到登录页面
            response.sendRedirect("/web2/login.html");
        }
    }
}

LoginFilter

package com.javakc.web2.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(
        filterName = "LoginFilter",
        urlPatterns = "/*"
)
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //1.获取到http相关接口
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //2.获取客户端访问的路径
        String url = request.getRequestURI();
        //3.1如果匹配到放行的路径
        if (url.equals("/web2/") || url.contains("/web2/login.html") || url.contains("/web2/login.do")) {
            filterChain.doFilter(request, response);
            return;
        }
        //3.2匹配是否已经登陆了
        else {
            HttpSession session = request.getSession(false);
            if (session != null) {
                filterChain.doFilter(request, response);
                return;
            }
        }
        //跳转到登录页面
        response.sendRedirect("/web2/login.html");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值