![](/img/trans.png)
[英]Tomcat infinte loop on requestDispatcher#forward(). StackOverflowError
[英]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.