Filter过滤器实现登录验证

用户登录了才能访问某个页面,不登录无法访问该页面

登录Servlet:

package servlet;
​
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import util.Constant;
​
import java.io.IOException;
​
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        if (username.equals("admin")){
            req.getSession().setAttribute(Constant.USER_SESSION,"admin");
            resp.sendRedirect("/sys/success.jsp");
        }else {
            resp.sendRedirect("/error/error.jsp");
        }
    }
​
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

 

注销Servlet:

package servlet;
​
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import util.Constant;
​
import java.io.IOException;
​
public class Logout extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       if (req.getSession().getAttribute(Constant.USER_SESSION) != null){
           req.getSession().removeAttribute(Constant.USER_SESSION);
           resp.sendRedirect("/login.jsp");
       }
​
    }
​
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

登录页:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<form action="/servlet/login" method="post">
    <input type="text" name="username">
    <input type="submit" value="登录">
</form>
</body>
</html>

错误页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录失败</title>
</head>
<body>
<h1>登录失败</h1>
<h2>没有权限,或用户名错误</h2>
<a href="/login.jsp">回到登录页</a>
</body>
</html>

登录成功页:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
</head>
<body>
<h1>登录成功</h1>
<a href="/servlet/logout">注销</a>
</body>
</html>

 

过滤器Filter:

package Filter;
​
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import util.Constant;
​
import java.io.IOException;
​
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }
​
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
​
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
​
        if (request.getSession().getAttribute(Constant.USER_SESSION) == null){
            response.sendRedirect("/error/error.jsp");
        }
​
        filterChain.doFilter(servletRequest,servletResponse);
    }
​
    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}

原文地址:http://www.cnblogs.com/wztblogs/p/16816731.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性