过滤器——LoginFilter

import java.io.IOException;
import java.io.PrintWriter;

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;

/**
*
* @author HuangHui
*
*/
public class LoginFilter implements Filter {

public void destroy() {

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;

// 获得通道
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
// 获得session
HttpSession session = req.getSession();
// 获得资源路径
String url = req.getServletPath();
// System.out.println("url:" + url);

// 去掉某些不能过滤的资源
if (url.equals("/login.jsp")) {
chain.doFilter(req, resp);
return;
}
if (session.getAttribute("login") == null) {
out.println("您还没有登录,3秒后自动转入登录页面!!!");
// 重定向
resp.setHeader("refresh", "3;url=/bsemgr/login.jsp");
} else {
chain.doFilter(req, resp);
}
}

public void init(FilterConfig arg0) throws ServletException {

}

}



-------------- web.xml 的配置如下 ---------------
<!-- 配置LoginFilter -->
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.weidu.imp.util.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

你可能感兴趣的:(struts,企业应用,Webwork)