servlet Filter 无法捕获ajax提交的请求吗
1个回答
2017-11-02 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
可以捕获,
/**
* 拦截器示例
* @author chenpengye
* 2015年12月10日 上午10:23:50
*/public class SessonFilter implements Filter {
private static Logger log = LoggerFactory.getLogger(SessonFilter.class); @Override
public void init(FilterConfig filterConfig) throws ServletException { //empty implement
} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
HttpSession session = httpRequest.getSession();
String uri = httpRequest.getRequestURI();
String ctxpath = httpRequest.getContextPath();
String userid = (String)session.getAttribute("userid"); if(uri.contains("/user/login") || uri.contains("/user/regPage") ){
chain.doFilter(request, response);
}else{
if(userid == null){
String contextPath = httpRequest.getContextPath();
String redirect = contextPath ; //ajax session 过期处理
//1:判断是否是ajax请求
if (httpRequest.getHeader("x-requested-with") != null
&& "XMLHttpRequest".equalsIgnoreCase(httpRequest.getHeader("x-requested-with"))) {
//向http头添加 状态 sessionstatus
httpResponse.setHeader("sessionstatus","timeout");
httpResponse.setStatus(403); //向http头添加登录的url
httpResponse.addHeader("loginPath", ctxpath);
chain.doFilter(request, response);
log.debug("ajax request"); return ;
}
httpResponse.sendRedirect(redirect); return;
}else{
chain.doFilter(request, response);
}
}
} @Override
public void destroy() { //empty implement
}
}
/**
* 拦截器示例
* @author chenpengye
* 2015年12月10日 上午10:23:50
*/public class SessonFilter implements Filter {
private static Logger log = LoggerFactory.getLogger(SessonFilter.class); @Override
public void init(FilterConfig filterConfig) throws ServletException { //empty implement
} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
HttpSession session = httpRequest.getSession();
String uri = httpRequest.getRequestURI();
String ctxpath = httpRequest.getContextPath();
String userid = (String)session.getAttribute("userid"); if(uri.contains("/user/login") || uri.contains("/user/regPage") ){
chain.doFilter(request, response);
}else{
if(userid == null){
String contextPath = httpRequest.getContextPath();
String redirect = contextPath ; //ajax session 过期处理
//1:判断是否是ajax请求
if (httpRequest.getHeader("x-requested-with") != null
&& "XMLHttpRequest".equalsIgnoreCase(httpRequest.getHeader("x-requested-with"))) {
//向http头添加 状态 sessionstatus
httpResponse.setHeader("sessionstatus","timeout");
httpResponse.setStatus(403); //向http头添加登录的url
httpResponse.addHeader("loginPath", ctxpath);
chain.doFilter(request, response);
log.debug("ajax request"); return ;
}
httpResponse.sendRedirect(redirect); return;
}else{
chain.doFilter(request, response);
}
}
} @Override
public void destroy() { //empty implement
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询