Struts does not return the proper URL to the Web Browser ?

Discussions

Web tier: servlets, JSP, Web frameworks: Struts does not return the proper URL to the Web Browser ?

  1. Hi ,
    <br>
    I have a scenario in using Struts that while a form request is submitted to an Action , the action will use ActionMapping.findForward() to forward the request to another Action , and finally to a JSP page. However , after the JSP page is returned to the browser , the browser's address bar does not show the actual JSP page location. Instead it still shows the URL that is mapped to the first Action.
    <br>
    For example (this sequence of the example is a bit different from my scenario , but the result is similiar) , examine Bluestone's Struts example at :
    <br>
    http://developer.bluestone.com/Scripts/SaIsapi.dll/StrutsTrailMap.class/struts-logon/logon.jsp
    <br>
    Looking at the struts-config.xml which is mentioned at http://developer.bluestone.com/Scripts/SaIsapi.dll/StrutsTrailMap.class/struts-trailmap/Trail6.htm , you can see the below mapping :
    <pre>
    <action path="/logon"
            type="logon.LogonAction"
            name="logonForm">
        <forward name="success" path="/success.jsp" />
        <forward name="failure" path="/failure.jsp" />
    </action>
    </pre>

    After successful login , I would expect the URL shown on the Web Browser's address bar to be something like http://developer.bluestone.com/Scripts/SaIsapi.dll/StrutsTrailMap.class/struts-logon/success.jsp
    Instead I got this : <br>
    http://developer.bluestone.com/Scripts/SaIsapi.dll/StrutsTrailMap.class/struts-logon/logon.do

    <br>
    Is this intentionally done by Struts ? What is the reason behind this ? Is there a way to change it so that it displays the actual URL ?

    <br>Thanks for reading this.<br>
    Kean
  2. When forwarding a request using the RequestDispatcher, it is not using the Location HTTP-header for forwarding the request, but it is using servlet-chaining to forward the request. So, the client doesn't really know about your http-request being forwarded to another servlet/jsp.

    Regards.
  3. In the forward element for the action, you can set the redirect attribute to equal true:

    <forward
      name="success"
      path="/success.jsp"
      redirect="true" />

    This will cause a redirect to occur, instead of a forward and the client browser will change to the proper URL.

    The problem with this approach is that any data that is stored in the request will disappear because a new request is issued for the redirect. It will fix your original problem, but it causes much, much worse.

    The truth is that you have to train the users just to ignore the browser URL. In general, they should never mess with or worry about the URL. I've seen some apps actually hide the URL field.

    Chuck
  4. Thanks for the useful explanation and tips. It's very much appreciated. It sure helped me save countless hours of trying to debug something that is intentional.