JSP 过滤器的使用Filter

JSP 过滤器的使用Filter,第1张

利用过滤器实现以下功能:
        ①用户登陆之后 向Session中放入用户的数据
        ②进入主页的时候判断用户是否已经登录 在过滤器中实现

1.过滤器 SysFilter.java

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

public class SysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
        //HttpServletRequest req 和 ServletRequest servletRequest 是父子关系
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        if(request.getSession().getAttribute("USER_SESSION")==null){
            response.sendRedirect("/error.jsp");
        }
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}

2.登录页面 login.java


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

public class login extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);//达到复用的效果
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if(username.equals("root")&&password.equals("root")){//登录成功
            req.getSession().setAttribute("USER_SESSION",req.getSession().getId());
            resp.sendRedirect("/sys/success.jsp");
        }else{
            resp.sendRedirect("/error.jsp");
        }

    }
}

3.退出 logout.java

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

public class logout extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);//达到复用的效果
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object user_session = req.getSession().getAttribute("USER_SESSION");
        if(user_session!=null){
            req.getSession().removeAttribute("USER_SESSION");
            resp.sendRedirect("/login.jsp");
        }
    }
}

web.xml文件配置



  
    login
    com.demo.servlet.login
  
  
    login
    /login
  

  
    logout
    com.demo.servlet.logout
  
  
    logout
    /logout
  

  
    Sysfilter
    com.demo.Filter.SysFilter
  
  
    Sysfilter
    /sys/*
  

登录页面 login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    登录


    
username
password

登录成功 success.jsp

<%--
  Created by IntelliJ IDEA.
  User: 13604
  Date: 2022/5/13
  Time: 16:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    主页




注销

登录失败 error.jsp

<%--
  Created by IntelliJ IDEA.
  User: 13604
  Date: 2022/5/13
  Time: 16:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    404


    返回登录页面


pom.xml配置



  4.0.0

  org.example
  javaweb-login
  1.0-SNAPSHOT
  war


  
  
  
    javax.servlet
    servlet-api
    2.5
  
  
  
    javax.servlet.jsp
    javax.servlet.jsp-api
    2.3.3
  
  
  
    javax.servlet.jsp.jstl
    jstl-api
    1.2
  
  
  
    taglibs
    standard
    1.1.2
  
  
  
    mysql
    mysql-connector-java
    5.1.47
  
  


 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/918898.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存