java如何从通过apache ErrorDocument重定向实现的Struts 2操作中获取原始请求_URI?
如何从Struts 2操作中访问请求URI?在perl/php/ruby中,它可以通过ENV[“REQUEST_URI”]等方式轻松获得。在java中,页面似乎是上下文。getErrorData()。getRequestURI()是我要寻找的,但遗憾的是,PageContext似乎没有在操作中定义,这可能是因为ErrorDocument重定向使请求看起来不像是错误,也可能是因为它是后来定义的
一个特定的例子
假设tomcat上有一个apache fronted(mod_jk/ajp)struts 2应用程序,它是通过apache中的ErrorDocument 404配置实现的。详情如下:
--原始请求url(触发404)--
http://server/totally/bogus/path
--http。形态--
ErrorDocument 404 /struts2app/lookup.action
--struts action--
public String bogusUrlLookup() {
HttpServletRequest request = ServletActionContext.getRequest();
// contains /lookup.action as does request.getRequestURI();
String url = RequestUtils.getServletPath(request);
// PageContext is null, so I cannot reach ErrorData from it.
log.info("pageContext="+ServletActionContext.getPageContext());
// Not in the ENV
// Map env = System.getenv();
// Not in the ATTRIBUTES
// request.getAttributeNames()
// Not in HEADER
// request.getHeaderNames()
return ERROR;
}
同样,我只需要字符串“/totally/bogus/path”,但在上面的操作中,我能找到的唯一url字符串是“/struts2app/lookup.action”。我之所以与ErrorDocument联系在一起,是因为/bogus/path不太可能在我的应用程序的名称空间中,因为apache提供其他非tomcat资源
# 1 楼答案
使用:
在
httpd.conf
文件中。然后使用request.getAttribute("REDIRECT_URL");
在jsp/servlet中获取变量# 2 楼答案
如果您不想错过查询字符串部分,最好:
# 3 楼答案
请求。getAttribute(“javax.servlet.forward.request_uri”)
# 4 楼答案