• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion of JSP into Servlet

 
Praveen Sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per Head First book the following JSP :
<html><body>
<% int count=0; %>
Page Count :
<%= ++count %>
</body></html>

gets converted to the following Servlet:
public class basicCounter_jsp extends some SpecialServlet
{
public void _jspService(request, response) throws
IOException,ServletException
{
PrintWriter out = response.getWriter ();
response.setContentType("text/html");
out.write("<html><body>");
int count = 0;
out.write("Page Count : ");
out.print(++count);
out.write("</body></html>");
}
}

What I dont understand is that why the expression (<%= ++count %> is getting written to the response as out.print and all other output statements are being written to the response as out.write ?
 
Praveen Sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me change my question :

What I dont understand is that why the expression (<%= ++count %> is getting written to the response with out.print while all the other output statements are being written to the response with out.write ?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The <%= foo %> notation implicitly calls out.print(foo). The <% foo %> doesn't.
[ August 25, 2008: Message edited by: Bauke Scholtz ]
 
Roland Benedek
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess would be because you use <%= %>

Which can print variables and all the other items are seen simple text and surrounded by quotes?
 
Praveen Sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats correct, the <%= foo %> notation implicitly calls out.print(foo).
But the "out.write" statements look incorrect, right ? As out.write is meant to write bytes to the response.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is indeed not intuitive that the PrintWriter instance is named 'out'. But that doesn't mean that it automagically becomes an OutputStream instance of which its write() method indeed takes bytes only.
[ August 25, 2008: Message edited by: Bauke Scholtz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic