Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IllegalStateException doubt ?

 
dharmendra Rathor
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Answere is D: if ( ! res.isCommitted() )

Thanks,
Vidhya
 
vidhya jayapalan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D: if ( ! res.isCommitted() )
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vidhya,
How do you say that the flush option will not throw any exceptio?
With regards,
Padma priya N.G.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I meant the first option.
With regards,
Padma priya N.G.
 
vidhya jayapalan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vidhya,
Thanks for the explanation.
Padma priya N.G.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic