Search code examples
javajspjsp-tags

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?


Solution

  • <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.