关于在java过滤器中进行页面重定向的问题
系统采用Struts架构,想在java过滤器中判断session是否失效,失效即跳转到登录页面,不好使啊!代码如下:publicvoiddoFilter(ServletR...
系统采用Struts架构,想在java过滤器中判断session是否失效,失效即跳转到登录页面,不好使啊!
代码如下:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String indexPage = config.getInitParameter("indexPage");
// 设置request编码用的字符集
request.setCharacterEncoding("utf-8"); // ①
HttpServletRequest requ = (HttpServletRequest) request;
HttpSession session = requ.getSession(true);
System.out.println("进入Session过滤器");
// 获取客户请求的页面
String requestPath = requ.getServletPath();
System.out.println("S请求动作:" + requestPath);
if (!requestPath.endsWith("/login_noauth1.do")) {
String userid = String.valueOf(session.getAttribute("username"));
if (userid.equals("null")) {
((HttpServletResponse) response).sendRedirect(indexPage);
return;
} else {
chain.doFilter(request, response);
}
} else {
chain.doFilter(request, response);
}
} 展开
代码如下:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String indexPage = config.getInitParameter("indexPage");
// 设置request编码用的字符集
request.setCharacterEncoding("utf-8"); // ①
HttpServletRequest requ = (HttpServletRequest) request;
HttpSession session = requ.getSession(true);
System.out.println("进入Session过滤器");
// 获取客户请求的页面
String requestPath = requ.getServletPath();
System.out.println("S请求动作:" + requestPath);
if (!requestPath.endsWith("/login_noauth1.do")) {
String userid = String.valueOf(session.getAttribute("username"));
if (userid.equals("null")) {
((HttpServletResponse) response).sendRedirect(indexPage);
return;
} else {
chain.doFilter(request, response);
}
} else {
chain.doFilter(request, response);
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询