路径跟踪过滤器

import java.io.IOException;

import java.util.Enumeration;

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.HttpSession;

public class PathFilter implements Filter {
    public void destroy() {}

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request     = (HttpServletRequest) request;
        String             path  = request.getRequestURI();
        Enumeration        e     = request.getParameterNames();
        StringBuffer       param = new StringBuffer();

        param.append("?");

        while (e.hasMoreElements()) {
            String name = (String) e.nextElement();

            param.append(name);

            String value = (String) request.getParameter(name);

            param.append("=" + value + "&");
        }

        param.deleteCharAt(param.length() - 1);

        HttpSession s       = request.getSession();
        String      curpath = (String) s.getAttribute("curpath");

        if (curpath == null) {
            s.setAttribute("curpath", path + param.toString());
        } else {
            if (!curpath.substring(0, (curpath.indexOf("?") == -1)
                                      ? curpath.length()
                                      : curpath.indexOf("?")).equals(path)) {
                s.setAttribute("lastpath", curpath);
                s.setAttribute("curpath", path + param.toString());
            }
        }

        chain.doFilter(request, response);
    }

    public void init(FilterConfig arg0) throws ServletException {}
}
posted @ 2008-05-30 10:08  maventalker  阅读(103)  评论(0编辑  收藏  举报