• Post Reply Bookmark Topic Watch Topic
  • New Topic

Forwarding from a servlet to a jsp.  RSS feed

 
Kel Walker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Experts,
I have a jsp myjsp.jsp.
The jsp has a HTML form which submits/posts to a servlet.
The servlet processes the request and then forwards to the same jsp myjsp.jsp like so :
RequestDispatcher rd =
getServletContext().getRequestDispatcher("/myjsp.jsp");
rd.forward(request, response);
My problem is when returning from the servlet I want the jsp to act as though it is receiving a completely new request.
Because there are parameters still in the request object when it gets forwarded myjsp.jsp is not behaving correctly.
Is there any way I can destroy the old request object once processed and force a new one. I am looking for behaviour similar to <jsp:forward ...>.
Please share solutions/workarounds.
Thank you.
 
Balaji Loganathan
author and deputy
Bartender
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can code something like this on top of the jsp page to chk new request objects forwarded by ur servlet
<% if (request != null ) // To avoid execution on initialization of jsp
if (request.getParameter("YourParameter") != null) // u can use session that are initialized in servlet
{
//Perform some action
}

}
%>
 
Kel Walker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean that there is no way to generate a new request to the jsp from the servlet?
There are quite a lot of unwanted parameters in the request object that I would have to test for and ignore. This could make my jsp a bit messy.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not use sendRedirect() instead of forward() ?
sendredirect() acts exactly as you want (new request).
 
Kel Walker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Roy Ben Ami,
That was the answer I was looking for. I knew there was a way.
Could you give me example syntax.
Thanks again.
 
Kel Walker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No worries. I checked the javadocs.
Problem solved. Thank you.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You welcome
glad i could help
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!