Redirect a Form to the same page from Servlet after submission | Community
Skip to main content
VeenaVikraman
Community Advisor
Community Advisor
October 16, 2015
Solved

Redirect a Form to the same page from Servlet after submission

  • October 16, 2015
  • 2 replies
  • 8970 views

Hi

 

   I was just analyzing normal form handling from CQ(without using CQ form) and I got stuck in between.  I have a form which I am submitting to a servlet through POST. After processing i want the form to redirect to the same jsp. I am not able to figure out how to achieve this. Any help would be appreciated

 

Thanks

 

Veena

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Ojjis

Hi, there are probably a few ways of doing this. Here's two:

1) response.sendRedirect(req.getHeader("referer"));
This makes use of the referer header which may or may not be there, so it's not reliable all the time. 


2) Have the form pass the page it's on as an variable  and then use that for redirecting.
This of course could be a security risk so you need to check that the url submitted from the form to your servlet actually is residing on your page :)

Good Luck
/johan

2 replies

smacdonald2008
Level 10
October 16, 2015

You want to submit data to a CQ Sling Servet, process the data using the Sling Servlet (ie - encode it), and then return the data back to the JSP. We have several community articles on how to do this.

See:

http://scottsdigitalcommunity.blogspot.ca/2013/06/posting-form-data-to-adobe-cq-using.html

This walks your though how to do this. As an example of processing the data -- the data is encoded as JSON using the servlet. 

Ojjis
OjjisAccepted solution
Level 7
October 16, 2015

Hi, there are probably a few ways of doing this. Here's two:

1) response.sendRedirect(req.getHeader("referer"));
This makes use of the referer header which may or may not be there, so it's not reliable all the time. 


2) Have the form pass the page it's on as an variable  and then use that for redirecting.
This of course could be a security risk so you need to check that the url submitted from the form to your servlet actually is residing on your page :)

Good Luck
/johan