views:

1231

answers:

5

Hi,

Does anyone know how I can retrieve the previous JSP URL that a page has come from within a JSP?

Can I retrieve this from the session/ request/ response object?

Hope this makes sense, Thank you

A: 

(assuming Struts 1)

Well in the struts-config.xml there is a input parameter in the action element (in the action-mappings section) So in your action (java class) you can access this value like this :

public ActionForward action(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { //do some stuff return mapping.getInputForward(); //return to the caller }

so if you want an action to return to its originating page, you can define multiple action entry pointing to the same action but with a different name and input value.

Hope that helps.

Julien Grenier
Unfortunately, the 'inputForward' may not always be the place that you came from.It's perfectly feasible to have the HTML generated by one action to contain a form that posts to a second action. When this occurs, the 'inputForward' will refer to the JSP page of the second action, NOT the first.
belugabob
A similar question was asked on Stackoverflow, about a week ago and, after a couple of suggestions involving HttpReferrer headers, an alternative was suggested. I'll lokk it up and post a link here.
belugabob
A: 

Assuming that you are talking about navigating within your own application and assuming struts 2 you can pass a parameter to the action, lets call it next, with the name of the next action. Lets say you have an action defined in struts.xml:

<action name="myaction" class="com.me.MyActionClass">
   <result name="success">${next}</result>
</action>

In MyActionClass you will have to declare the property next with its getters and setters. On invoking this action you must provide the value for next which could be the same name as the page from which you are invoking the action.

Vincent Ramdhanie
+2  A: 

Many thanks - apologies for not being able to mark my correct answer as the site seems to have changed and i cant see how to do this but i used

<%String referer = request.getHeader("Referer"); out.print("" + referer);%>

test
Be careful with the use of "Referer" and other HTTP headers, as intermediate proxies might filter them.
Guido
A: 

Even simpler:

<%= request.getHeader("Referer") %>
Steve McLeod
A: 

for example in referer i got the uri as http://www.sun.com/questions?pnr=18&amp;value=gg while i pront referer at that time i got this string ok. now i need to get the parameter value how to get it. from that string