jQueryrocket

方法一:

登录页面嵌入这一段js

if (top != window){
top.location.href = window.location.href; 
// window.parent.location.href="${pageContext.request.contextPath}/login.jsp";
// window.parent.location.href="${pageContext.request.contextPath}/login.jsp";
}

方法二:

使用Filter,同时过滤对静态页面和controller的访问(非ajax)

web.xml配置

<filter>
    <filter-name>loginfilter</filter-name>
    <filter-class>
        com.lty.ebus.custom.filters.CheckLoginFilter</filter-class>
    <init-param>
        <param-name>rootPath</param-name>
        <param-value>/login.jsp</param-value>
    </init-param>
</filter>
<!-- 所有需要session才能访问的JSP或HTML页面均放在webviews下-->
<filter-mapping>
    <filter-name>loginfilter</filter-name>
    <url-pattern>/webviews/*</url-pattern> 
</filter-mapping>
<!-- 过滤controller -->
<filter-mapping>
    <filter-name>loginfilter</filter-name>
    <url-pattern>/webapp/*</url-pattern>
</filter-mapping>

 过滤器

public class CheckLoginFilter implements Filter {

    private String rootPath;

    public void destroy() {
        if (!StringUtils.isEmpty(rootPath)) {
            this.rootPath = null;
        }
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        HttpServletRequest request = (HttpServletRequest) req;
        Object session = RedisHelper.get(SysGlobalConstants.SESSIONID.concat(request.getSession().getId()));
        String uri = request.getRequestURI().toLowerCase();
        if (StringUtils.isEmpty(session) && uri.indexOf("login") < 0 && uri.indexOf("logout") < 0) {
            // 设置header,便于ajax请求做处理
            response.setHeader("sessionstatus", "timeout");
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter()
                    .println("<script language='javascript'>if(window.opener==null){window.top.location.href='"
                            + rootPath + "';}else{window.opener.top.location.href='" + rootPath
                            + "';window.close();}</script>");
        } else {
            chain.doFilter(req, res);
        }
    }

    public void init(FilterConfig con) throws ServletException {
        this.rootPath = con.getServletContext().getContextPath().concat(con.getInitParameter("rootPath"));
    }

}

方法三:

如果是ajax请求 那种,除了上面的还要往后看。该js文件需要被引入到有ajax请求(对session有要求)的页面中(其实思路上和第一种是差不多的)。

JS

/** 
 * 设置未来(全局)的AJAX请求默认选项 
 * 主要设置了AJAX请求遇到Session过期的情况 
 */  
var appName = $("#appName").val();

$.ajaxSetup({  
    complete: function(xhr,status) { 
        var sessionStatus = xhr.getResponseHeader('sessionstatus');  
        if(sessionStatus == 'timeout') {  
            var top = getTopWinow();  
            top.location.href = appName + '/login.jsp';              
        }  
    }  
});  
  
/** 
 * 在页面中任何嵌套层次的窗口中获取顶层窗口 
 * @return 当前页面的顶层窗口对象 
 */  
function getTopWinow(){  
    var p = window;  
    while(p != p.parent){  
        p = p.parent;  
    }  
    return p;  
}

  方法四:

在ajax判断解析数据判断,弹窗跳转

原文地址:https://www.cnblogs.com/kaspar/p/12557355.html