赞
踩
package com.dog.filter;
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class OnlineFilter extends HttpServlet implements Filter {
private static final long serialVersionUID = 1L;
@Override
public void destroy() {
System.out.println("OnlineFilter destroy>>>>>>>>>>>>>>>>>>");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
/*RequestDispatcher dispatcher = request
.getRequestDispatcher("../index.jsp");*/
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
String path = req.getContextPath();
String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path;
HttpSession session = req.getSession(true);
String username = (String) session.getAttribute("username");
if (username == null || "".equals(username)) {
resp.setHeader("Cache-Control", "no-store");
resp.setDateHeader("Expires", 0);
resp.setHeader("Prama", "no-cache");
resp.sendRedirect(basePath+"/index.jsp");
} else {
chain.doFilter(req, resp);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("OnlineFilter init>>>>>>>>>>>>>>>>>");
}
}
package com.dog.filter;
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class OnlineFilter extends HttpServlet implements Filter {
private static final long serialVersionUID = 1L;
@Override
public void destroy() {
System.out.println("OnlineFilter destroy>>>>>>>>>>>>>>>>>>");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
/*RequestDispatcher dispatcher = request
.getRequestDispatcher("../index.jsp");*/
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
String path = req.getContextPath();
String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path;
HttpSession session = req.getSession(true);
String username = (String) session.getAttribute("username");
if (username == null || "".equals(username)) {
resp.setHeader("Cache-Control", "no-store");
resp.setDateHeader("Expires", 0);
resp.setHeader("Prama", "no-cache");
resp.sendRedirect(basePath+"/index.jsp");
} else {
chain.doFilter(req, resp);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("OnlineFilter init>>>>>>>>>>>>>>>>>");
}
}
OnlineFilter
com.dog.filter.OnlineFilter
OnlineFilter
/main/*
上午在完成这部分功能的时候遇到一个问题。Filter过滤的太多了。点击提交按钮,不是先提交到action.而是先提交到filter。 这样filter的session就取不到值,无论填写什么信息都会因为session是null而跳转到index.jsp产生循环跳转的问题。
解决方法就是 将登录过滤的filter的url加上路径,然后将需要过滤的页面放到相应的文件夹下,这样就能过滤指定的url地址啦。 访问index.jsp的时候就不会过滤。 不错。 又解决了一个问问。慢慢来。切莫浮躁。
分享到:
2012-12-03 10:37
浏览 21837
评论
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。