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

Unexpected output

 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have a code like:

public class HelloWorldServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException
{
PrintWriter out = res.getWriter();
res.setContentType("text/html");
out.println("Hey I am from servlet ");
res.setContentType("image/jpeg");
out.flush();
out.println(" " + res.isCommitted() + " ");
out.println(" " + res.getContentType());
out.println("This is being tested");
}
}

For this code I get output like:
Hey I am from servlet true image/jpeg;charset=ISO-8859-1

whereas I think I should get output as:
Hey I am from servlet true image/jpeg;charset=ISO-8859-1 This is being tested

I am not sure of why it is not printing the sentence "This is being tested". It is printing two sentences above it. Could anyone please correct my understanding here.

Thanks,
Rancy
 
Priya Viswam
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me the entire thing got printed.
 
Abhishek Sharma
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i tested same set of codes at my system and this is the output that i have got..



Now my question is even executing "out.flush();" (that means response has been sent to client) why next three set of lines are not giving error.
 
Priya Viswam
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call out.println even after flushing.
But you should not invoke sendRedirect.
 
Abhishek Sharma
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks got the point
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thank you for your replies.
I am not sure of what happened the last time I tried, but now when I tried again. Everything that I expected to print has been printed.

Thanks,
-Rancy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic