• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IlligalStateException

 
mallika shah
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,
I tried:-
1.
out.println("something ");
RequestDispatcher view=request.getRequestDispatcher("/try3.jsp");
try {
view.forward(request,response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

2. out.println("something");
out.flush();
RequestDispatcher view=request.getRequestDispatcher("/try3.jsp");
try {
view.forward(request,response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

3. out.write("something");
out.flush();
RequestDispatcher view=request.getRequestDispatcher("/try3.jsp");
try {
view.forward(request,response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

IT should have thrown an illigalException, but its not throwing that exception anytime? why so?

thanks,
mallika
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think an IllegalStateException is happening. Did you check the server's console? Have you tried to explicitly catch IllegalStateException and print to the console in the catch block?
 
mallika shah
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Mark,

Actually its showing the output of my servlet only,not the jsp page where i was forwarding with requestDispatcher.

so I tried this;->

catch (IllegalStateException e) {
out.println(e.getMessage());
}
And then it displays:-
Cannot forward after response has been committed

thanks,
mallika
 
Joe Harry
Ranch Hand
Posts: 10047
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mallika,

I think the first one shouldn't have thrown an IllegalStateException. Did you try it?? What about your opinion ranchers about the first option originally posted by Mallika shah??
 
Joe Harry
Ranch Hand
Posts: 10047
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it and all 3 options gave me an IllegalStateException. However, I heard from somewhere that only when we force the o/p to be comitted, like using out.flush() and then we send the response using a requestdispatcher, we get an IllegalStateException!

Correct me guys if I"m wrong!
 
Akbar Upadyayula
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right jyoti
 
Akbar Upadyayula
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or
at a time we can not use both at a time only one can work for the second we will get illegalstate exception
 
Joe Harry
Ranch Hand
Posts: 10047
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Akbar!
 
Atul Sawant
Ranch Hand
Posts: 304
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so you mean to say that "out.println" is as good as "out.flush" in this scenario?
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried several combinations and this is what I got:

1) PrintWriter.println + forward
The result is the jsp being displayed after clearing what println had output. No exception and the forward method 'returns' to the doGet method in the forwarding servlet.

2) PrintWriter.println + PrintWriter.flush + forward
In this case the only thing that gets shown is println's output. I got an IllegalStateException.

3) PrintWriter.write + forward
Idem 1.

4) PrintWriter.write + PrintWriter.flush + forward
Idem 2.

5) ServletOutputStream.write + forward
Nothing gets displayed (blank page) and I got a ServletException (org.apache.jasper.JasperException), which I think comes from the fact that the jsp's servlet is trying to obtain a PrintWriter after the forwarding servlet having obtained a ServletOutputStream (and writing to it?).

6) ServletOutputStream.write + ServletOutputStream.flush + forward
Idem 2.


@mallika shah:
What is happening in your first post's cases is that you are not catching IllegalStateException, only ServletException and IOException. Besides IllegalStateException is a RuntimeExeption.
[ April 05, 2007: Message edited by: Sergio Tridente ]
 
mallika shah
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergio,
this is what is happening

Without catching IlligalStateException its showing the output of only my servlet where this code is inclued,not the jsp page where i was forwarding with requestDispatcher(thos one jsp output which i wanted to see after request forward)..also it didnt indicate any IlligalException being thowon though is is being thrown.


so I tried this;->

catch (IllegalStateException e) {
out.println(e.getMessage());
}
And then it displays:-
Cannot forward after response has been committed
that clears the point :-IlligalStateException is BEING thrwon nad it stops the processing after it is thown.

thanks,
mallika
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic