package com.web.study; import java.io.IOException; 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.Filter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class PermissionFilter implements Filter { public PermissionFilter() { } private FilterConfig filterConfig; public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException,ServletException { HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)res; String servletPath = request.getServletPath(); HttpSession session = request.getSession(); String flag = (String)session.getAttribute("flag"); if (servletPath.equals("/Jsp/logon.jsp")||servletPath.equals("/Jsp/index.jsp")||servletPath.equals("/LoginServlet")) { filterChain.doFilter(req, res); } else { if(flag!=null&&flag.equals("login_success")){ filterChain.doFilter(req, res); } else if (flag!=null && flag.equals("login_error")) { request.setAttribute("return_uri", servletPath); RequestDispatcher rd = request.getRequestDispatcher("/Jsp/login.jsp"); rd.forward(request, response); } else { request.setAttribute("return_uri", servletPath); RequestDispatcher rd = request.getRequestDispatcher("/Jsp/login.jsp"); rd.forward(request, response); } } } public void destroy() { } }
时间: 2024-10-05 16:39:16