当前位置:   article > 正文

java filter 跳转页面_Filter过滤,未登录的用户页面跳转

filter 强制 跳转页面

package com.dog.filter;

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

public class OnlineFilter extends HttpServlet implements Filter {

private static final long serialVersionUID = 1L;

@Override

public void destroy() {

System.out.println("OnlineFilter destroy>>>>>>>>>>>>>>>>>>");

}

@Override

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

/*RequestDispatcher dispatcher = request

.getRequestDispatcher("../index.jsp");*/

HttpServletRequest req = (HttpServletRequest) request;

HttpServletResponse resp = (HttpServletResponse) response;

String path = req.getContextPath();

String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path;

HttpSession session = req.getSession(true);

String username = (String) session.getAttribute("username");

if (username == null || "".equals(username)) {

resp.setHeader("Cache-Control", "no-store");

resp.setDateHeader("Expires", 0);

resp.setHeader("Prama", "no-cache");

resp.sendRedirect(basePath+"/index.jsp");

} else {

chain.doFilter(req, resp);

}

}

@Override

public void init(FilterConfig arg0) throws ServletException {

System.out.println("OnlineFilter init>>>>>>>>>>>>>>>>>");

}

}

package com.dog.filter;

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

public class OnlineFilter extends HttpServlet implements Filter {

private static final long serialVersionUID = 1L;

@Override

public void destroy() {

System.out.println("OnlineFilter destroy>>>>>>>>>>>>>>>>>>");

}

@Override

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

/*RequestDispatcher dispatcher = request

.getRequestDispatcher("../index.jsp");*/

HttpServletRequest req = (HttpServletRequest) request;

HttpServletResponse resp = (HttpServletResponse) response;

String path = req.getContextPath();

String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path;

HttpSession session = req.getSession(true);

String username = (String) session.getAttribute("username");

if (username == null || "".equals(username)) {

resp.setHeader("Cache-Control", "no-store");

resp.setDateHeader("Expires", 0);

resp.setHeader("Prama", "no-cache");

resp.sendRedirect(basePath+"/index.jsp");

} else {

chain.doFilter(req, resp);

}

}

@Override

public void init(FilterConfig arg0) throws ServletException {

System.out.println("OnlineFilter init>>>>>>>>>>>>>>>>>");

}

}

OnlineFilter

com.dog.filter.OnlineFilter

OnlineFilter

/main/*

上午在完成这部分功能的时候遇到一个问题。Filter过滤的太多了。点击提交按钮,不是先提交到action.而是先提交到filter。 这样filter的session就取不到值,无论填写什么信息都会因为session是null而跳转到index.jsp产生循环跳转的问题。

解决方法就是 将登录过滤的filter的url加上路径,然后将需要过滤的页面放到相应的文件夹下,这样就能过滤指定的url地址啦。 访问index.jsp的时候就不会过滤。 不错。 又解决了一个问问。慢慢来。切莫浮躁。

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-12-03 10:37

浏览 21837

评论

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/编程挑战者/article/detail/61330
推荐阅读
相关标签
  

闽ICP备14008679号