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

JSP Page doubt?

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranchers,

Why is that the jsp page is always buffered?? Why not the same happens with servlets? Any technical advantage because of this?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe because in a decent architecture, servlets are supposed to be controllers, not views.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satou,

I'm not understanding?? How does this affect buffering?? Can you elaborate it a bit please!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are talking about the output stream, right ? Or do you mean "cache" ?
Can you please rephrase your question ? I may be out of context here.

My first post was bad anyway, as output buffering is also used for servlets. So please ignore it
[ April 23, 2007: Message edited by: Satou kurinosuke ]
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm talking about buffering the response!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets also buffer the response. See ServletResponse#setBufferSize
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But with servlets the buffering should be explicitly set, but with jsp's it is by default buffered and then sent to the response?? WHY???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe because (and this is a supposition), as I said in the first post, in an MVC architecture, servlets are not supposed to output anything to the client, but rather redirect to a view (JSP file) which will deal with the output. So there's no real need for buffering in servlets.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satou! - Agreed. Thanks!
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what the javadocs say about JspWriter:


The initial JspWriter object is associated with the PrintWriter object of the ServletResponse in a way that depends on whether the page is or is not buffered. If the page is not buffered, output written to this JspWriter object will be written through to the PrintWriter directly, which will be created if necessary by invoking the getWriter() method on the response object. But if the page is buffered, the PrintWriter object will not be created until the buffer is flushed and operations like setContentType() are legal. Since this flexibility simplifies programming substantially, buffering is the default for JSP pages.


The key point, I think, is that it gives you the flexibility to begin writing to the response (well, in fact to the buffered JspWriter) and later, if you want to, set headers (like the mentioned content type) and do other stuff that would be illegal if the response had been committed. You may even begin writing to the JspWriter and then be able to call c:redirect or jsp:forward and there will be no IllegalStateException thrown because the reponse was not committed.

Also remember that during translation the Container may translate carriage returns and spaces into out.write("\n") (this is what tomcat does). Not to mention about template text that we may include at the very beginning of our Jsp (like the omnipresent <html><body> tags). If the JspWriter were not to be buffered, then even if you do not try to output anything explicitly in your JSP you may have an IllegalStateExcpetion when trying to set headers (even if that is the first thing that you code in your page).
[ April 24, 2007: Message edited by: Sergio Tridente ]
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was good one .

You all think so deep , great..


Thanks
Vishal Chugh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic