写了个filter,检查session,如果session为空,那么跳转到error.jsp页面.
package com.htxx.interceptor;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.registry.infomodel.User;
import com.htxx.entity.Adminer;
import com.htxx.utils.Const;
public class CheckLoginFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request=(HttpServletRequest)servletRequest;
HttpServletResponse response=(HttpServletResponse)servletResponse;
HttpSession session=request.getSession();
//session.setMaxInactiveInterval(10);
String path = request.getRequestURI();
String contextPath = request.getContextPath();
String url = path.substring(contextPath.length());
// System.out.println(url);
if(!url.matches(Const.NO_INTERCEPTOR_PATH)){
Adminer user=(Adminer)session.getAttribute(Const.WBUSER_SESSION);
String userid=(String)session.getAttribute(Const.M_USER);
if (userid == null && user ==null) {
response.sendRedirect(request.getContextPath()
+ Const.WB_USER_ERROR);
return;
}
else {
}
}
filterChain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
可是在测试的时候发现,error.jsp能加载,但是页面不跳转。
在网上查了查,发现是ajax提交的问题,原文地址:http://www.th7.cn/web/ajax/201606/172122.shtml
。所以在ajax提交时,加了个error来接收。
$("#Form4").ajaxSubmit({
type: "post",
dataType:"json",
async:true,
success: function (result) {
// alert(data.state);
//新增成功
if(result.state==1){
window.location.href='<%=basePath %>mobile/week/fotoFifthPage?zbid='+result.data;
//window.event.returnValue=false;
$("#tj4").removeAttr("disabled");
}else{//新增失败
alert(result.message);
$("#tj4").removeAttr("disabled");
return false;
}
},error:function(result){
window.location.href='<%=basePath %>pages/error.jsp';
}
});
如果ajax返回error,则跳转到error.jsp页面。这样,在Form4提交时,会通过filter检查session,如果session失效,页面跳转。