【问题标题】:Servlet filters - passing an attribute not working with RequestDispatcher#forwardServlet 过滤器 - 传递不适用于 RequestDispatcher#forward 的属性
【发布时间】:2010-07-02 13:44:51
【问题描述】:

我正在尝试建立一个允许用户创建自己的页面的站点 - 作为站点的子域,因此我目前正在尝试做的是有一个查看子域的过滤器,并且如果未使用或已保留,则用户将被转发到他们选择子域名的页面。

我遇到的问题是,当我在 ServletRequest 对象上设置一个属性,然后使用 RequestDispatcher 转发时,过滤器被再次调用 - 但它看不到我设置的属性。

我已经调试并观察它工作(或不工作!),并且正在设置属性,但是在转发之后,该属性不存在。

有人可以帮助解释发生了什么,以及我可以如何解决这个问题吗??

我可能还应该提到我正在为 Java Google App Engine 进行开发。

这是我的过滤器代码。

import java.io.IOException;
import java.util.Enumeration;
import java.util.logging.Logger;

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



public class SubdomainFilter implements Filter{
    private static Logger log = Logger.getLogger(SubdomainFilter.class.getName());
    private static final String[] RESERVED_SUBDOMAINS = {"admin", "home", "www"};
    private static final String registerPage = "/register_a_page";

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        //if we've been forwarded, it must have been because of an invalid subdomain
        if(arg0.getAttribute("subdomain") != null) {
            arg2.doFilter(arg0, arg1);
        } else { //otherwise, look at the subdomain and determine what to do
            boolean invalidSubdomain = false;
            try {
                String requestURLInfo = ((HttpServletRequest)arg0).getRequestURL().toString();
                String subdomain = URLUtils.getLowestSubdomainFromURL(requestURLInfo);
                arg0.setAttribute("subdomain", subdomain);
                if(subdomainReserved(subdomain) || subdomainInUse(subdomain)) {
                    invalidSubdomain = true;
                }//Otherwise the subdomain must be valid
            } catch(Exception ex) {
                log.severe("Filter could not get subdomain:\n" + ex.toString());
            } finally {
                if(invalidSubdomain) {
                    RequestDispatcher dispatcher = arg0.getRequestDispatcher(registerPage);
                    dispatcher.forward(arg0, arg1);
                } else {
                    arg2.doFilter(arg0, arg1);
                }
            }
        }

    }

    private boolean subdomainReserved(String subdomain) {
        boolean subdomainReserved = false;
        for(String reservedSubdomain : RESERVED_SUBDOMAINS) {
            if(reservedSubdomain.equals(subdomain)) {
                subdomainReserved = true;
                break;
            }
        }
        return subdomainReserved;
    }

    private boolean subdomainInUse(String subdomain) {
        boolean subdomainInUse = false;
        //TODO: implement
        return subdomainInUse;
    }
    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
    }

}

【问题讨论】:

    标签: java servlets servlet-filters


    【解决方案1】:

    我认为问题在于我没有在 web.xml 中声明以下内容:

    <filter-mapping>
       <filter-name>SubdomainFilter</filter-name>
       <servlet-name>*</servlet-name>
       <dispatcher>REQUEST</dispatcher>
       <dispatcher>FORWARD</dispatcher>
    </filter-mapping>  
    

    【讨论】:

    • 与原始配置的确切区别是什么?
    【解决方案2】:

    它应该工作。 URLUtils.getLowestSubdomainFromURL() 刚刚返回 null,或者请求-响应链中的其他内容已触发 HttpServletResponse#sendRedirect()

    【讨论】:

    • 经过调试,getLowestSubdomainFromURL 肯定返回字符串“www”。我不知道我为 Google App Engine 开发是否有什么不同?
    【解决方案3】:

    我知道这篇文章已经 3 年了,但我仍然想确认 Louis 的帖子,如果没有以下 web.xml,转发工作正常仅当您不需要转发属性(即请求.getAttribute, request.setAttribute)。

    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    

    我不确定当您不需要时在 web.xml 中指定调度程序标签是否有任何开销,但 您肯定需要在 web.xml 中使用它才能使属性工作。

    【讨论】:

      猜你喜欢
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2017-01-13
      • 2015-10-18
      相关资源
      最近更新 更多