18.10.10 下午 第54天上課
====================================================================================================
過濾器:
public class PermissionFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
HttpServletResponse httpServletResponse=(HttpServletResponse)response;
//獲取請求uri路徑
String uri=httpServletRequest.getRequestURI();
//登入頁面
int n1=uri.indexOf("login.jsp");
//處理登入的servlet
int n2=uri.indexOf("LoginServlet");
if(n1>0||n2>0){
//將請求繼續傳遞(放行)
chain.doFilter(httpServletRequest,httpServletResponse);
}else{
HttpSession session = httpServletRequest.getSession();
//session中存在的使用者資訊,放行
if(session.getAttribute("user")!=null){
//講請求繼續傳遞
chain.doFilter(httpServletRequest,httpServletResponse);
}else{
//如果session不存在使用者資訊,則跳轉至登入頁面
httpServletResponse.sendRedirect("login.jsp");
}
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
====================================================================================================
監聽器