Expand my Community achievements bar.

SOLVED

URL does not change with RequestDispatcher forward

Avatar

Former Community Member

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; } }
1 Accepted Solution

Avatar

Correct answer by
Employee

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

View solution in original post

6 Replies

Avatar

Employee Advisor

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

Avatar

Former Community Member

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.

Avatar

Level 10

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

Avatar

Former Community Member

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]

Avatar

Correct answer by
Employee

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

Avatar

Former Community Member

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/FormsHandlingSer... ]