1

I want to direct the user back to the previous page with the same get in the url

if (somecheck) { 
 req.getRequestDispatcher("register.jsp?email="+req.getParameter("email")).forward(req, resp);
}

But this directs the user to "/register.jsp" instead of "/register.jsp?email=testemail@mail.com"

How can i add the email to the url?

Jelle
  • 758
  • 2
  • 14
  • 36
  • `email` parameter is already in your request. Just read it in register jsp from request object. – Anton Dovzhenko Jan 16 '17 at 08:54
  • @Faraz Durrani But i want it in the url – Jelle Jan 16 '17 at 08:55
  • 1
    Then you should use `response.sendRedirect("register.jsp?email="+req.getParameter("email"));` only redirect can change actual url (or you may do it in js). Forward doesn't change the url, it gives the response of requested resource. – Anton Dovzhenko Jan 16 '17 at 08:56
  • 1
    Please, see detailed explanation here [RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()](http://stackoverflow.com/questions/2047122/requestdispatcher-forward-vs-httpservletresponse-sendredirect) – Anton Dovzhenko Jan 16 '17 at 08:59
  • 1
    Unfortunately not, `sendRedirect()` doesn't keeps track of any parameters sent in initial request. Roughly, it goes back to browser and forces it to send another request using url passed to `sendRedirect` – Anton Dovzhenko Jan 16 '17 at 09:23
  • @Anton Dovzhenko Thanks for the help, i got it working now – Jelle Jan 16 '17 at 09:36
  • You are welcome – Anton Dovzhenko Jan 16 '17 at 09:38

2 Answers2

0
ServletContext context = request.getServletContext();
String path = "/register.jsp?email="+request.getParameter("email");
RequestDispatcher rd = context.getRequestDispatcher(path);
rd.include(request, response);
0

Solution is using response.sendRedirect("register.jsp?email="+req.getParameter("email")); thanks to Anton Dovzhenko

Jelle
  • 758
  • 2
  • 14
  • 36