//在请求处理完成后调用的方法 @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub } //在请求处理之后但是视图渲染之前调用的方法 @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } //在请求处理之前调用的方法 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception { // TODO Auto-generated method stub String url = request.getRequestURI(); //获取请求的url //System.out.println("进入拦截器,url = " + url); Object user = request.getSession().getAttribute("user");//获取session中的user对象 if(user == null){ //如果session中的user对象为空,表示未登录或者登录状态失效 System.out.println("未登录或登录失效,url = " + url); if("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){ //如果是ajax请求 Map<String, String> ret = new HashMap<String, String>(); ret.put("type", "error"); ret.put("msg", "登录状态已失效,请重新去登录!"); response.getWriter().write(JSONObject.fromObject(ret).toString());//返回JSON格式的错误信息 return false; } response.sendRedirect(request.getContextPath() + "/system/login"); //跳转到登录页面 return false; } return true; } }翻译代码
时间: 2024-03-07 19:52:46 浏览: 61
这段代码是一个拦截器的实现类。它实现了HandlerInterceptor接口,并重写了其中的三个方法:preHandle、postHandle和afterCompletion。这三个方法分别在请求处理之前、请求处理之后但是视图渲染之前、请求处理完成后被调用。
在preHandle方法中,拦截器会获取请求的url和session中的user对象。如果user对象为空,则表示未登录或登录状态失效,拦截器会判断当前请求是否为ajax请求,如果是ajax请求,则返回JSON格式的错误信息,否则跳转到登录页面。如果user对象不为空,则放行请求。
在postHandle和afterCompletion方法中,拦截器不做任何处理,直接返回。
相关问题
package com.ischoolbar.programmer.interceptor; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import com.ischoolbar.programmer.entity.User; /** * 登录过滤拦截器 * @author llq * */ public class LoginInterceptor implements HandlerInterceptor{ @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception { // TODO Auto-generated method stub String url = request.getRequestURI(); //System.out.println("进入拦截器,url = " + url); Object user = request.getSession().getAttribute("user"); if(user == null){ //表示未登录或者登录状态失效 System.out.println("未登录或登录失效,url = " + url); if("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){ //ajax请求 Map<String, String> ret = new HashMap<String, String>(); ret.put("type", "error"); ret.put("msg", "登录状态已失效,请重新去登录!"); response.getWriter().write(JSONObject.fromObject(ret).toString()); return false; } response.sendRedirect(request.getContextPath() + "/system/login"); return false; } return true; } }给代码加上注释
//导入相关的包
package com.ischoolbar.programmer.interceptor;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.ischoolbar.programmer.entity.User;
/**
* 登录过滤拦截器
*
* @author llq
*
*/
public class LoginInterceptor implements HandlerInterceptor {
//在请求处理完成后调用的方法
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
//在请求处理之后但是视图渲染之前调用的方法
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
}
//在请求处理之前调用的方法
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
// TODO Auto-generated method stub
String url = request.getRequestURI(); //获取请求的url
//System.out.println("进入拦截器,url = " + url);
Object user = request.getSession().getAttribute("user"); //获取session中的user对象
if (user == null) { //如果session中的user对象为空,表示未登录或者登录状态失效
System.out.println("未登录或登录失效,url = " + url);
if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) { //如果是ajax请求
Map<String, String> ret = new HashMap<String, String>();
ret.put("type", "error");
ret.put("msg", "登录状态已失效,请重新去登录!");
response.getWriter().write(JSONObject.fromObject(ret).toString()); //返回JSON格式的错误信息
return false;
}
response.sendRedirect(request.getContextPath() + "/system/login"); //跳转到登录页面
return false;
}
return true;
}
}
package com.ischoolbar.programmer.interceptor.home; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; /** * 前台登录拦截器 * @author llq * */ public class LoginInterceptor implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception { // TODO Auto-generated method stub String requestURI = request.getRequestURI(); Object admin = request.getSession().getAttribute("account"); if(admin == null){ //表示未登录或者登录失效 System.out.println("链接"+requestURI+"进入拦截器!"); String header = request.getHeader("X-Requested-With"); //判断是否是ajax请求 if("XMLHttpRequest".equals(header)){ //表示是ajax请求 Map<String, String> ret = new HashMap<String, String>(); ret.put("type", "error"); ret.put("msg", "登录会话超时或还未登录,请重新登录!"); response.getWriter().write(JSONObject.fromObject(ret).toString()); return false; } //表示是普通链接跳转,直接重定向到登录页面 response.sendRedirect(request.getServletContext().getContextPath() + "/home/login"); return false; } return true; } }中response.sendRedirect(request.getServletContext().getContextPath() + "/home/login");中getServletContext爆红
根据您提供的代码,`getServletContext()` 方法在 `response.sendRedirect()` 中使用是没有问题的。所以,可能是您的开发环境没有正确导入 `javax.servlet` 包。
请确保您的项目中已经正确导入了 `javax.servlet` 的相关库文件。如果您使用的是 Maven 或 Gradle 等构建工具,请检查您的项目依赖配置中是否包含了正确的 Servlet API。
如果问题仍然存在,请提供更多的上下文信息,例如您的开发环境和构建工具,以便我能够更好地帮助您解决问题。
阅读全文