对某个页面的过滤

package work;

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 {
    public void destroy() {}
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain filterchain) throws IOException, ServletException {
        HttpServletRequest requ=(HttpServletRequest)request;
        HttpServletResponse resp=(HttpServletResponse)response;
        HttpSession session=requ.getSession(true);

        if(session.getAttribute("name")==null){
            resp.sendRedirect("Login.jsp");
        }else{
            filterchain.doFilter(request, response);
        }
    }
    public void init(FilterConfig arg0) throws ServletException {}

}

web.xml

<filter>
      <filter-name>LoginFilter</filter-name>
      <filter-class>work.LoginFilter</filter-class>
  </filter>
<filter-mapping>
      <filter-name>LoginFilter</filter-name>
      <url-pattern>/Cloud.jsp</url-pattern>
  </filter-mapping>

 

posted @ 2017-12-05 22:47  随心佩  阅读(273)  评论(0编辑  收藏  举报