过滤器 如何实现获取不到用户名跳转回登录界面

servlet

package filter;

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;

public class loginfilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain arg2)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
          HttpServletRequest requ=(HttpServletRequest)request;
          HttpServletResponse resp=(HttpServletResponse)response;
          HttpSession session=requ.getSession();
          Object object = session.getAttribute("xm");
          if(object==null){
        	  resp.sendRedirect("login.jsp");
          }else{
        	  arg2.doFilter(requ,resp);
          }
	}
	   

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}

  其实仅仅是使用了一个借口,只要判断当前页面中是否有用户名,有的话就自动跳转到login.jsp

posted @ 2017-11-24 22:51  橘柑之味  阅读(320)  评论(0编辑  收藏  举报