Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Request Dispatcher-help needed

 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m using RequestDispatcher like this,

A.jsp
-----
RequestDispatcher dis = getServletContext().getRequestDispatche("/myservlet?value=abc");

dis.forward(request,response);

i want to display a text message in A.jsp before forwarding.
how it is to be done?

Thanks
 
Mat Williams
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Basically you can't. from the api
forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.


What does this mean - if you send anything to the client it either causes the forward to throw an IllegalStateException if what you have sent has already made it to the client OR what you sent to the client is flushed from the buffer and so never makes it to the client.

So what can you do. The easiest thing is to return a page that displays the message and then, through a meta tag or another way (there are several), redirects the browser to the page that you wanted the client to go to.
Or as an alternative return a page that has the message you want to display and as it loads - via javascript it calls the servlet again which can then do a



HTH Mat
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mat....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic