| Author |
IllegalStateException doubt ?
|
dharmendra Rathor
Greenhorn
Joined: Mar 20, 2007
Posts: 17
|
|
3. Consider the following code for the doGet() method: public void doGet(HttpServletRequest req, HttpServletResponse res) { PrintWriter out = res.getWriter); out.println("<html><body>Hello</body></html>"); //1--here-- res.sendError(HttpServletResponse.SC_UNAUTHORIZED); } Which of the following lines can be inserted at //1 so that the above code does not throw any exception? A: res.flushBuffer(); B: res.resetBuffer(); C: res.clearStatus(); D: if ( ! res.isCommitted() ) What is the answer of obove question ?
|
 |
vidhya jayapalan
Greenhorn
Joined: Jul 14, 2005
Posts: 12
|
|
Hi, Answere is D: if ( ! res.isCommitted() ) Thanks, Vidhya
|
 |
vidhya jayapalan
Greenhorn
Joined: Jul 14, 2005
Posts: 12
|
|
Explanation: D: correct isCommitted() - a boolean indicating if the response has been committed if written at //1 wont throw any exception A: incorrect flushBuffer()- Forces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written. B: and C: are incorrect - No such methods exists Thanks, Vidhya
|
 |
Prafulla N. Patil
Ranch Hand
Joined: Mar 20, 2006
Posts: 106
|
|
|
D: if ( ! res.isCommitted() )
|
Thanks, Prafulla Patil
SCJP, SCWCD
|
 |
Padma priya Gururajan
Ranch Hand
Joined: Oct 05, 2006
Posts: 411
|
|
Hi Vidhya, How do you say that the flush option will not throw any exceptio? With regards, Padma priya N.G.
|
Padma priya N.G.
Be the change you want to be - Mahatma Gandhi
|
 |
Padma priya Gururajan
Ranch Hand
Joined: Oct 05, 2006
Posts: 411
|
|
Hi, I meant the first option. With regards, Padma priya N.G.
|
 |
vidhya jayapalan
Greenhorn
Joined: Jul 14, 2005
Posts: 12
|
|
Padma, I think you got confused by the order i had answered A: Incorrect B: Incorrect C: Incorrect D: correct A: incorrect flushBuffer()- Forces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written. Refer API. Hence, if used at place //1 in the question above, the next line res.sendError(HttpServletResponse.SC_UNAUTHORIZED); will throw IllegalStateException because the response is already sent to the client. Thanks, Vidhya
|
 |
Padma priya Gururajan
Ranch Hand
Joined: Oct 05, 2006
Posts: 411
|
|
Hi Vidhya, Thanks for the explanation. Padma priya N.G.
|
 |
 |
|
|
subject: IllegalStateException doubt ?
|
|
|