Filter Code:
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
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 com.hacongz.framework.util.GlobalConstant;
import com.hacongz.system.web.form.SysUserForm;
public class LogInFilter implements Filter{
protected FilterConfig filterConfig;
public void init(FilterConfig config) throws ServletException {
this.filterConfig = config;
}
public void doFilter(ServletRequest srequest, ServletResponse sresponse,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) srequest;
HttpServletResponse response = (HttpServletResponse) sresponse;
SysUserForm user=(SysUserForm)request.getSession().getAttribute(GlobalConstant.SESSION_USER_OBJECT);//取出用户对象
if(user==null) {
StringBuffer sUrl = new StringBuffer("");
if (request.getRequestURI() != null) {
sUrl
.append(URLEncoder.encode(request.getRequestURI(),
"UTF-8"));
}
if (request.getQueryString() != null) {
sUrl.append("?");
sUrl.append(URLEncoder
.encode(request.getQueryString(), "UTF-8"));
}
String retUrl = (URLDecoder.decode(sUrl.toString(), "UTF-8")).substring(6);
System.out.println("URL=============================" + retUrl);
response.sendRedirect("../index/loginAlone.jsp?" +"test=" + retUrl);
} else {
chain.doFilter(request, response);
}
}
public void setFilterConfig(final FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
public void destroy() {
this.filterConfig = null;
}
}
web.xml config:
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.hacongz.framework.web.filter.LogInFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/page/shopping/*</url-pattern>
</filter-mapping>