This page last changed on Jan 24, 2006 by jnolen.

Cannot resolve external resource into attachment. How do I get hold of the HttpServletRequest?

HttpServletRequest request = ServletActionContext.getRequest();
if (request != null)
{
     // do something here
}

You should always assume that ServletActionContext.getRequest() will return null. ServletActionContext is only populated if the request comes in through WebWork. There are a number of circumstances in which it will not be populated, either because a web request has come in through some other path, or because there was no web request in the first place:

  • AJAX requests that come in via the DWR servlet
  • SOAP/XML-RPC requests
  • Scheduled tasks, including the sending of email notifications

Treat ServletActionContext as a bonus. If it's populated you can do neat things with it, but don't rely on it.

If you are accessing through DWR, you can use WebContextFactory.get().getHttpServletRequest() as described in the DWR Documentation.

Note: You should break that up to make sure that WebContextFactory.get() returns an object, much as you break up the ServletActionContext.getRequest() call to check for null above - just in case it's not accessed by DWR either.

Posted by dhardiker@adaptavist.com at Feb 04, 2007 06:08
Document generated by Confluence on Mar 22, 2007 20:59