How to get the current JSP PageContext from an Action with Struts

Posted on 2007-11-27
Last Modified: 2013-11-24
Hi Experts!

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?
Question by:abel
  • 5
  • 2
LVL 14

Expert Comment

ID: 20364318
If not mistaken, the method RequestUtils.computerUrl() is designed for struts tag, and not for action class,
coz this method has moved to TagUtils in later version.

What I suggest is that you base on the RequestUtils.computerUrl() source code to write a similar method like
CutomRequestUtils.computeURL(ServletContext context, HttpServletRequest request, ActionForward forward, Map params)

coz the PageContext is mainly use for retrieving the ServletContext and HttpRequest.
LVL 39

Author Comment

ID: 20364442
So, basically what you are suggesting is doing it by hand (or, based on the open source of the current computeUrl() method).

In a way, it should be enough to have an ActionForward and the ServletContext or HttpServletRequest  (to find the root of the servlet) to find the absolute url for a forward. I am surprised there is no programmatic way to do so.

Do you know of a helper method that does this for me, including merging the params Map?
LVL 39

Author Comment

ID: 20365013
What I have is the situation that in a legacy application a couple of JSP files exist that must be forwarded (redirected) when a certain requirement is met. The requirement (a state) is stored in the session.

Some JSP have a Struts Action. Hence I wanted to use the existing MVC pattern and not break it.

However, I the non-form JSP files need to be redirected sometimes too. I'd like to have it configurable and I hoped to use the struts-config for it. Is it possible to add an action that does nothing, except redirect or show the JSP page? Or am I making things to hard and should I just break the MVC pattern?

any help or thoughts is appreciated,

-- Abel --
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 14

Accepted Solution

boonleng earned 500 total points
ID: 20372009
> Do you know of a helper method that does this for me, including merging the params Map?
Not that i know of, the generate path, merging of params.. etc are all done inside the method computeUrl().

> Is it possible to add an action that does nothing, except redirect or show the JSP page?
Yes. you can create a dummy action and configure the <foward  redirect="true"...> on the action.
Or just extend ForwardAction( and set redirect to true on the returned ActionForward.
But why not just use ForwardAction which forward to the jsp?
LVL 39

Author Comment

ID: 20380984
To answer your last question: sometimes the moment of redirection is from a JSP and sometimes it is from an Action. Sometimes the location is at the root of the service (context) and sometimes it is at some arbitrary depth (several folders deep). The hostname can be different, too.

The largest problem (in lieu of the above) was that sometimes the redirect was internal (ActionForward) and sometimes it was external (response.sendRedirect). To add to the confusion, the whole application is build on a staggering 13 (!) frames and framesets that have inter-relations that will make your head spin.

That's the legacy. I hoped to find one-size-fits-all solution, hence the trying of either getting the servlet context and/or the JSP it is all coming from to build the redirection url. This was leading to only more spaghetti in the end: trying to keep within the bounds of Struts in an application that already violates it on many places proved to be largely impossible.

Making structure from a mess is hard (try to build a house from a ruin is harder than building a house from scratch) but I am not allowed to rebuild (budget and other reasons). Meaning that in the end I end up placing a new window every here and there.... :S

My best-fit solution so far (I have tried three extensible approaches) is to use a dedicated page that mimics some of the frame-behavior of existing pages. As it starts to look now, it may be the correct approach.

Thanks for the help so far,

-- Abel --
LVL 39

Author Comment

ID: 20418604
Ok, by now I have found a couple of other workarounds to this. Basically, I use the HttpServletRequest object to store and retrieve the values and I changed the code such that I do not need the JspContext anymore (bad practice, it seemed now that I understand more of this codebase).

Because you've been the only one around that has helped me out, I'll grand you the points.
LVL 39

Author Closing Comment

ID: 31411245
though the solution was not used in the end, thanks for the help!

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 110
Java asynchronous logging 4 70
HSSFWorkbook cannot be resolved error 10 136
table example 4 33
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question