<jsp:forward> - Strange behaviour

Discussions

Web tier: servlets, JSP, Web frameworks: <jsp:forward> - Strange behaviour

  1. <jsp:forward> - Strange behaviour (3 messages)

    Hello Guys,

    I'm facing a strange problem with <jsp:forward> tag. Please go thru the following points....

    There is a parent jsp (main.jsp) where i'm including a inc.jsp using <jsp:include> action.

    now in inc.jsp, using <jsp:forward> i'm forwarding the control to frwd.jsp

    control is forwarded to frwd.jsp and doesnt get back to inc.jsp, but it gets back to main.jsp.

    according to <jsp:forward> action, control sud remain to forwarded jsp i.e. frwd.jsp.

    if i'm using <jsp:forward> in main.jsp instead of inc.jsp, everything works fine, but the moment i'm using in any included jsp, the prob occurs.

    One more thing, instead of using <jsp:forward>, I tried to redirect the page to frwrd.jsp using sendRedirect into inc.jsp

    In this case, sendRedirect is not working...it simply ignores that statement and executes rest of the script.

    Can anybody tell me whats actullay going on? Why <jsp:forward> doesnt work as expected when it is been used in a jsp which is been included by another jsp?.

    Below is the code for the main.jsp, inc.jsp and frwrd.jsp - just for referecne....

    main.jsp
    ----------
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    </HEAD>
    <BODY>
    <%
    out.println(response.encodeRedirectURL("this is main jsp. here i'm including inc.jsp with <jsp:include> action...."));
    %>
    <jsp:include page="inc.jsp"/>
    <hr>
    Rest of the content of main.jsp
    </BODY>
    </HTML>


    inc.jsp
    ---------
    <br/> content of inc.jsp. now i'll forward this to frwd.jsp...
    <jsp:forward page="frwd.jsp"/>
    <br>&nbsp;<br> content of inc.jsp after <jsp:forward> tag....


    frwrd.jsp
    -------------
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> frwd.jsp </TITLE>
    </HEAD>

    <BODY>
    <h3> content of frwd.jsp </h3>
    </BODY>
    </HTML>

    Please let me know if I'm unclear on any of the above points.

    Jay
  2. The JSP standard is vague about what happens when you have includes to that themselves include or forward to other pages. In my experience, the exact behavior is server-specific, and can be pretty flaky. One trick that I have found to help things along is to put a flush="true" instruction on the include and forward tags.

    On a related not, the sendRedirect() function is not working because you have partially committed a response to the browser. The sendRedirect() function only works if you have not generated any output. Technically, the same is true of forwarding, but I have found that forwarding operations sometimes work anyway.
  3. Paul:

    Agreed that <jsp:forward> has really very vague behaviour. Also thanks for your help. After your suggestion I tried the following code and it worked with Tomcat 5.0, but it didnt with weblogic 6.1. Below is the code for all three jsps....

    main.jsp
    -----------
    <jsp:include page="inc.jsp" flush="true"/>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    </HEAD>
    <BODY>
    <hr>
    Rest of the content of main.jsp
    </BODY>
    </HTML>


    inc.jsp
    ---------
    <jsp:forward page="frwd.jsp"/>
    <br/> content of inc.jsp. now i'll forward this to frwd.jsp...
    <br>&nbsp;<br> content of inc.jsp after <jsp:forward> tag....

    frwd.jsp
    ----------
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> frwd.jsp </TITLE>
    </HEAD>
    <BODY>
    <h1> content of frwd.jsp </h1>
    </BODY>
    </HTML>

    with tomcat 5.0 it worked fine as expected. it just come up with "content of frwd.jsp". but for weblogic 6.1, it also saw me the content of main.jsp. Let me know if there is any issue specific to weblogic 6.1

    One more thing I'd like to know you is there is not flush="true" for <jsp:forward>. If u include it, tomcat 5.0 will fire an error, where as weblogic will ignore it.

    Jay
  4. Unfortunately, the behavior of weblogic 6.1 is entirely consistent with the standard. The forward operation is supposed to replace the current output with the output of the forward page. Tomcat is just replacing "inc.jsp", but Weblogic is replacing both "inc.jsp" and "main.jsp".

    I suggest you rework your logic to use only includes. You can use conditional logic to determine which pages are actually included. I am assuming you are using nested includes in order to build up a page out of a set of templates.


    main.jsp
    -----------
    Main JSP
    <jsp:include page="inc.jsp" flush="true"/>


    inc.jsp
    -----------
    Inc JSP
    <% if("inc-1.jsp".equals(request.getParameter("inc")) %>
    <jsp:include page="inc-1.jsp" flush="true"/>
    <% } else { %>
    <jsp:include page="inc-2.jsp" flush="true"/>
    <% } %>


    inc-1.jsp
    -----------
    Inc 1 JSP


    inc-2.jsp
    -----------
    Inc 2 JSP