How to get the current JSP PageContext from an Action with Struts
Posted on 2007-11-27
I've been struggling with this seemingly easy question for some time now. Maybe my question is wrong, because what I am actually after is how to do a RequestUtils.computerUrl(). In the (very) old Struts 1.0.2, which I am unfortunately bound to during the current change in a legacy application, it seems about the only trustworthy way to calculate the absolute URL for doing a redirect action, based on an ActionForward.
I hope I am not too vague. I need to construct a URL, either absolute or relative to the current page context, based on an ActionForward, with a Map of parameters. I was construction the URL by hand, but found out that it is erroneous, because the current path is not known and the context path (to which the ActionForward is relative) is not known either.
The computeUrl() method requires as first parameter (whoever knows why) a PageContext. I can store the current pageContext from the JSP file, but then I violate the MVC pattern (apart from being annoying).
I have found the JspFactory.getPageContext(), but that is used for initializing the page context, which is obviously not what I need here. Something like a static method of PageContext.getCurrentContext() I haven't yet found. I saw some references on the internet and some unanswered questions, none provided an answer. Though it shouldn't be too hard in a context-central environment to retrieve the context, shouldn't it?
For what it's worth, I use IBM WebSphere 5.1.1, which runs on JDK 1.4, with Struts 1.0.2 (yes, I know, it is old).
I am not looking for an answer for how to concatenate the different pieces of the URL, I can do that much myself, I'm after a stable distributable method that will construct the URL correctly using an ActionForward as a reference.
Thanks in advance,
-- Abel --
PS: I'm rather acquainted with newer JSP, JSTL, J2EE, Struts 2 etc, I've never worked with the ancient Struts 1.0.2 before, maybe it was just not provided for yet?