0
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
                         FilterChain chain) throws IOException,
                                                   ServletException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)res;
        HttpSession session = request.getSession(false);
        if (session == null ||
            session.getAttribute("currentSessionUser") == null) {
            response.sendRedirect("Loginpage.jsp"); // No logged-in user found, so redirect to login page.
            response.setHeader("Cache-Control",
                               "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            response.setHeader("Pragma",
                               "no-cache"); // HTTP 1.0.                 response.setDateHeader("Expires", 0);
        } else {
            chain.doFilter(req,
                           res); // Logged-in user found, so just continue request.
        }
    }
}

我想在jsp中使用上面的代码来强制用户在注销后重定向登录页面我们如何将它转换为jsp以便我可以将它包含在我的jsp页面中。我像下面这样写它,通过这样做我们可以调用那个chain.filter()方法

<%@ page import=" java.io.IOException"%>
<%@ page import="javax.servlet.Filter"%>
<%@ page import="javax.servlet.FilterChain"%>
<%@ page import="javax.servlet.FilterConfig"%>
<%@ page import="javax.servlet.ServletException"%>
<%@ page import="javax.servlet.ServletRequest"%>
<%@ page import="javax.servlet.ServletResponse"%>
<%@ page import="javax.servlet.http.HttpServletRequest"%>
<%@ page import="javax.servlet.http.HttpServletResponse"%>
<%@ page import=" javax.servlet.http.HttpSession"%>
<%@ page import="javax.servlet.annotation.WebFilter"%>
<%
    //@Override         
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;
    HttpSession session = req.getSession(false);
        if (session == null || session.getAttribute("userHash") == null)
        {  
        res.sendRedirect("/timescape/applicationservices/fileshare/vm/login/login.jsp"); // No logged-in user found, so redirect to login page.
        res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1. 
        res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        res.setDateHeader("Expires", 0);
        } 
        else
          {
          chain.doFilter(request, response); // Logged-in user found, so just continue request. 
          }

    %>
4

1 回答 1

0

假设您只需要 JSP 中的重定向,您可以轻松地做到这一点:

<%
if (session == null || session.getAttribute("currentSessionUser") == null) {
    response.sendRedirect("Loginpage.jsp"); // No logged-in user found, so redirect to login page.                  
    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.                 
    response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
    response.setDateHeader("Expires", 0);
    return;
}
%>

这是因为在 JSP 中,总是有一个包含对象的隐式变量被调用responseHttpServletResponse并且总是session有一个包含对象的隐式变量被调用HttpSession

于 2012-09-05T08:51:35.460 回答