簡體   English   中英

Servlet過濾器-傳遞無法與RequestDispatcher#forward一起使用的屬性

[英]Servlet filters - passing an attribute not working with RequestDispatcher#forward

我正在嘗試建立一個站點,允許用戶創建自己的頁面-作為站點的子域,因此,我目前正在嘗試的是使用一個過濾器來查看子域,如果不是在使用中(如果已保留),則該用戶將被轉發到他們選擇其子域名的頁面。

我遇到的問題是,當我在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
    }

}

我認為問題在於我沒有在web.xml中聲明以下內容:

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

我知道這篇文章已有3年歷史了,但是我仍然想確認Louis的這篇文章, 只有在不需要轉發屬性(即request.getAttribute,request.setAttribute)的情況下,轉發才能在沒有以下web.xml的情況下正常運行。

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

我不確定在不需要時通過在web.xml中指定調度程序標簽是否有任何開銷,但是您確實需要在web.xml中使屬性起作用

它應該工作。 URLUtils.getLowestSubdomainFromURL()剛返回null ,或者請求-響應鏈中的其他內容觸發了HttpServletResponse#sendRedirect()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM