This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes IlligalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "IlligalStateException" Watch "IlligalStateException" New topic
Author

IlligalStateException

mallika shah
Ranch Hand

Joined: May 11, 2006
Posts: 164
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

Joined: Feb 05, 2003
Posts: 4727

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?


A good workman is known by his tools.
mallika shah
Ranch Hand

Joined: May 11, 2006
Posts: 164
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

Joined: Sep 26, 2006
Posts: 9618
    
    2

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??


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9618
    
    2

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

Joined: Feb 09, 2007
Posts: 12
you are right jyoti
Akbar Upadyayula
Greenhorn

Joined: Feb 09, 2007
Posts: 12
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

Joined: Sep 26, 2006
Posts: 9618
    
    2

Thanks Akbar!
Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
so you mean to say that "out.println" is as good as "out.flush" in this scenario?


Mission SCWCD. Mission SCJP Complete: SCJP 1.4 - 91%
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

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 ]

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
mallika shah
Ranch Hand

Joined: May 11, 2006
Posts: 164
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
 
subject: IlligalStateException