Christian P.
Christian P.

Reputation: 15

how to redirect in jsp using <jsp:forward> with a relative url

I'm trying to redirect from a page to another always use <jsp:forward page='/some_page.jsp' />or response.sendRedirect(/some_page.jsp)

but in this case I need to redirect to another page someting like home/try/page and I try to use <jsp:forward page='home/try/page' /> but it doesn't work some help?

Upvotes: 1

Views: 10164

Answers (2)

Moni
Moni

Reputation: 433

<jsp:forward /> tag is intended for forwarding the request on to a relative url.

now you want to redirect your page here - 'home/try/page'. If your want to redirect from one directory to another directory page than try something like below -

 <jsp:forward page="/home/try/page.jsp"/>
 <jsp:forward page="../home/try/page.jsp"/>
 or
 <jsp:forward page="../../home/try/page.jsp"/>

"../" will get you out from the current folder.

For example - let assume we requested the following URL, -

http://localhost/myJSPApp/Security/login.jsp

When the file "login.jsp" executes it performs the following action

<jsp:forward page="../Welcome/Welcome.jsp" />

This is a forward to another JSP in another folder "Welcome" which is at the same level as the "Security" folder.

I hope it will help you.

Upvotes: 3

Chris Franklin
Chris Franklin

Reputation: 167

It looks like you may need to move up the "tree" to get to a different folder. For example, if you are current in page x.jsp in folder "foo" and need to get to page y.jsp in folder "bar", you would need to do this - jsp: forward page='../bar/y.jsp'.

The "../" tells jsp to back out of the current folder(foo) it is in and look for the next folder(bar). You would need to add one "../" for each level you go up.

Hope this helps.

Upvotes: 0

Related Questions