Skip to main content
Oct 16, 2015 2:28 AM UTC
Solved

URL does not change with RequestDispatcher forward

There are several posts on the same topic, but they do not have a clear solution and code-snippets.

When i do a Forward using RequestDispatcher.. the result page loads but the URL does not change.

The URL where we start and Submit data to the PostServlet: http://localhost:4502/content/ttii/en/postformtest.html

Final result URL should be: http://localhost:4502/content/ttii/en/postformtestresult.html

But is: http://localhost:4502/services/processFormData

Would appreciate help!

HTML Form

<form name="userRegistrationForm" method="post" action="/services/processFormData"> <input type="submit" title="Submit" class="btn submit btn-success" value="Submit" tabindex="25" name="bttnAction"> </form>

My Servlet class..

@SlingServlet( label = "TTI - TTICommon POST Servlet", metatype = true, methods = { "POST" }, name="com.tti.tticommons.service.servlets.TTIPostServlet", paths = { "/services/processFormData" } ) public class TTIPostServlet extends SlingAllMethodsServlet{ @Override protected void doPost(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,IOException { final SlingHttpServletRequest syntheticRequest = new SyntheticSlingHttpServletGetRequest(request); final RequestDispatcherOptions options = new RequestDispatcherOptions(); options.setReplaceSelectors(""); options.setForceResourceType("cq/Page"); request.getRequestDispatcher("/content/ttii/en/postformtestresult.html", options).forward(syntheticRequest, response); } }

 

the Wrapper Servlet:

public class SyntheticSlingHttpServletGetRequest extends SlingHttpServletRequestWrapper { private static final String METHOD_GET = "GET"; public SyntheticSlingHttpServletGetRequest(final SlingHttpServletRequest request) { super(request); } @Override public String getMethod() { return METHOD_GET; } }
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
JustinEd3Accepted solution
Adobe Employee
Oct 16, 2015 2:28 AM UTC

This is the expected behavior of RequestDispatcher.forward(). You can't change the URL and keep the request context (attributes).

6 replies

Oct 16, 2015 2:28 AM UTC

Thanks.

1. I'm curious to know how does the OutOfTheBox AEM Form Components handle such situation?

2. There are some blogs/forums that referred me to use a Filter. Does this have any relevance here?

com.day.cq.wcm.foundation.forms.impl.FormsHandlingServlet [ http://docs.adobe.com/docs/v5_1/cq-wcm-javadoc/com/day/cq/wcm/foundation/forms/impl/FormsHandlingServlet.html ]

Translate
JustinEd3Adobe EmployeeAccepted solution
Adobe Employee
Oct 16, 2015 2:28 AM UTC

This is the expected behavior of RequestDispatcher.forward(). You can't change the URL and keep the request context (attributes).

Translate
Oct 16, 2015 2:28 AM UTC

I'm using a custom code.

But i'm stuck here.. If i use..

A) include or forward, then i can retrieve the values set as "request.setAttribute" in the result page. The page is forwarded and displays the result page, but the URL does not change. Shows as http://localhost:4502/services/processFormData instead it should be http://localhost:4502/content/ttii/en/postformtestresult.html

RequestDispatcher dispatcher = request.getRequestDispatcher(responseHtml); dispatcher.include(syntheticRequest, response); or dispatcher.forward(syntheticRequest, response); [Where 'syntheticRequest' is SlingHttpServletRequestWrapper]

 

B) If i use, 'sendRedirect', the URL changes to the result page, but i'm loosing the attributes set to the request.

final SlingHttpServletRequest syntheticRequest = new SyntheticSlingHttpServletGetRequest(request); response.sendRedirect(responseHtml); [Where 'syntheticRequest' is SlingHttpServletRequestWrapper]
Translate
smacdonald2008
Level 10
Oct 16, 2015 2:28 AM UTC

Are you following an AEM doc or example from community? Did you see this thread that Sham answered:

Translate
Oct 16, 2015 2:28 AM UTC

Thanks for your note.

response.sendRedirect() will solve the redirection issue and the url will change. But i will loose the data that came along with the POST. This data is in "syntheticRequest" above.

Translate
Kunal_Gaba_
New Member
Oct 16, 2015 2:28 AM UTC

Why don't you use response.sendRedirect() method instead of using the forward method of request dispatcher ?

Translate