web.xml的配置
Filter的配置:
黑色头发 http://heisetoufa.iteye.com
<filter>
<filter-name>onlineFilter</filter-name>
<filter-class>struts.form.OnlineFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>onlineFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
Filter的配置:
package struts.form;
import java.io.IOException;
import javax.servlet.Filter;
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.http.*;
public class OnlineFilter extends HttpServlet implements Filter
{
public void init(FilterConfig filterConfig) throws ServletException
{
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
RequestDispatcher dispatcher = request.getRequestDispatcher("UserLogin.jsp");
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession(true);
// 从session里取的用户名信息
String username = (String) session.getAttribute("sessionUserName");
// 判断如果没有取到用户信息,就跳转到登陆页面
if (username == null || "".equals(username))
{
// 跳转到登陆页面
dispatcher.forward(request,response);
System.out.println("用户没有登陆,不允许操作");
}
else
{
// 已经登陆,继续此次请求
chain.doFilter(request,response);
System.out.println("用户已经登陆,允许操作");
}
}
public void destroy()
{
}
}
黑色头发 http://heisetoufa.iteye.com