Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in flush method

 
V Gala
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following code will work fine ? Assume all the variables are initialized

properly. Select two
A public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
PrintWriter out = response.getWriter();
ServletOutputStream stream = response.getOutputStream();
out.println ("Worked....");
}
B public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println ("Worked....");
}
C public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
PrintWriter out = response.getWriter();
out.flush();
response.setContentType("myname");
out.println ("Worked....");
}

D None of the above



B and C

Answer A is incorrect because we cannot call getOutputStream after getWriter. it will throw an IllegalStateException. Answer B will work fine. Answer C also will work fine even though myname is not a valid content type. The container will think that it

is a new mime type.


Is option c a valid answer?
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Quote Your Sources.
 
Ash More'
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a para straight from the api documentation:
=============================================================================
This method may be called repeatedly to change content type and character encoding. This method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after getWriter has been called or after the response has been committed.
=============================================================================

well after using the flush method on out object, content can still be written to out. the problem comes while adding headers and cookies after the response is committed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic