views:

829

answers:

2

How can you access the REQUEST_URI from within a Struts 2 Action? In perl/php/ruby it is readily available via ENV["REQUEST_URI"] and the like. In java it seems that the PageContext.getErrorData().getRequestURI() is what I'm looking for, but sadly, the PageContext does not appear to be defined within the action, either because the ErrorDocument redirection makes the request not look like an error, or because it is defined later.

A particular example

Given an apache fronted (mod_jk/ajp) struts 2 app on tomcat reached via the ErrorDocument 404 configuration in apache. With the following details:

-- Original request url (which triggers the 404) --

http://server/totally/bogus/path

-- http.conf --

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;
}

Again all I need is the string "/totally/bogus/path", but in the above action the only url string that I can find is "/struts2app/lookup.action". I'm tied to the ErrorDocument because the totally/bogus/path is not likely to be within the namespace of my application because apache serves other non-tomcat resources.

+1  A: 

request.getAttribute("javax.servlet.forward.request_uri")

Nope. That is null in this context.
andynu
A: 

Use

JkEnvVar REDIRECT_URL ""

in your httpd.conf file

and then use request.getAttribute("REDIRECT_URL"); to get the variable in your jsp/servlets

Sanjay Sheoran