如何将以下代码段转换为JSP

时间:2012-09-05 08:46:11

标签: java jsp logout

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. 
          }

    %>

1 个答案:

答案 0 :(得分: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中,总是有一个名为response的隐式变量包含HttpServletResponse对象,总是有一个名为session的隐式变量包含HttpSession对象。