- 未登陸,禁止訪問任何頁面,自動跳轉到登陸頁
package com.catalogeasy.filter;<br />import java.io.IOException;<br />import javax.servlet.Filter;<br />import javax.servlet.FilterChain;<br />import javax.servlet.FilterConfig;<br />import javax.servlet.ServletException;<br />import javax.servlet.ServletRequest;<br />import javax.servlet.ServletResponse;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;<br />public class LoginFilter implements Filter {<br />public void destroy() {<br />// TODO Auto-generated method stub<br />}<br />public void doFilter(ServletRequest arg0, ServletResponse arg1,<br />FilterChain arg2) throws IOException, ServletException {<br />// TODO Auto-generated method stub<br />HttpServletRequest httprequest = (HttpServletRequest) arg0;<br />HttpServletResponse httpresponse = (HttpServletResponse) arg1;<br />String temp = httprequest.getServletPath();<br />if(httprequest.getSession().getAttribute("user")==null){<br />if("/jdom.do".equals(temp)||"/login.do".equals(temp)){<br />arg2.doFilter(arg0, arg1);<br />}else{<br />httpresponse.sendRedirect("/CatalogUser");<br />}<br />}else{<br />arg2.doFilter(arg0, arg1);<br />}<br />/*if(httprequest.getSession().getAttribute("user")==null&&((!"/jdom.do".equals(temp))||(!"/login.do".equals(temp)))){<br />httpresponse.sendRedirect("/CatalogUser/user/login.jsp");<br />}else{<br />arg2.doFilter(arg0, arg1);<br />}*/<br />}<br />public void init(FilterConfig arg0) throws ServletException {<br />// TODO Auto-generated method stub<br />}
2.登陸後跳轉到請求頁面
import java.io.IOException;<br />import java.net.URLEncoder;<br />import javax.servlet.Filter;<br />import javax.servlet.FilterChain;<br />import javax.servlet.FilterConfig;<br />import javax.servlet.ServletException;<br />import javax.servlet.ServletRequest;<br />import javax.servlet.ServletResponse;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;<br />import com.ww.golf.bo.Player;<br />import com.ww.golf.util.ActionUtils;<br />public class LogFilter implements Filter {<br /> protected FilterConfig filterConfig;<br /> public void init(FilterConfig config) throws ServletException {<br /> this.filterConfig = config;<br /> }<br /> public void doFilter(ServletRequest srequest, ServletResponse sresponse,<br /> FilterChain chain) throws IOException, ServletException {<br /> HttpServletRequest request = (HttpServletRequest) srequest;<br /> HttpServletResponse response = (HttpServletResponse) sresponse;<br /> Player player = (Player) request.getSession().getAttribute(<br /> ActionUtils.PLAYER_OBJ_KEY);<br /> if (player == null) {<br /> StringBuffer sUrl = new StringBuffer("");<br /> if (request.getRequestURI() != null) {<br /> sUrl<br /> .append(URLEncoder.encode(request.getRequestURI(),<br /> "UTF-8"));<br /> }<br /> if (request.getQueryString() != null) {<br /> sUrl.append("?");<br /> sUrl.append(URLEncoder<br /> .encode(request.getQueryString(), "UTF-8"));<br /> }<br /> System.out.println("URL:" + sUrl);<br /> response.sendRedirect("../index.jsp?" + ActionUtils.GO_URI_KEY<br /> + "=" + sUrl.toString());<br /> } else {<br /> chain.doFilter(request, response);<br /> }<br /> }<br /> public void setFilterConfig(final FilterConfig filterConfig) {<br /> this.filterConfig = filterConfig;<br /> }<br /> public void destroy() {<br /> this.filterConfig = null;<br /> }<br />}<br />