Solved

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

Posted on 2007-11-27
7
10,914 Views
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?
0
Comment
Question by:abel
  • 5
  • 2
7 Comments
 
LVL 14

Expert Comment

by:boonleng
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.
0
 
LVL 39

Author Comment

by:abel
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?
0
 
LVL 39

Author Comment

by:abel
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,

Cheers,
-- Abel --
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 14

Accepted Solution

by:
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(http://struts.apache.org/1.0.2/api/org/apache/struts/actions/ForwardAction.html) and set redirect to true on the returned ActionForward.
But why not just use ForwardAction which forward to the jsp?
0
 
LVL 39

Author Comment

by:abel
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,

Cheers,
-- Abel --
0
 
LVL 39

Author Comment

by:abel
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.
0
 
LVL 39

Author Closing Comment

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
JKS to store upstart data 2 65
dividesSelf challange 15 79
Books that can get me started on JAVA 2 54
countHi2 challenge 7 44
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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now