*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes JSP Page doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "JSP Page doubt?" Watch "JSP Page doubt?" New topic
Author

JSP Page doubt?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Ranchers,

Why is that the jsp page is always buffered?? Why not the same happens with servlets? Any technical advantage because of this?


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Maybe because in a decent architecture, servlets are supposed to be controllers, not views.


[My Blog]
All roads lead to JavaRanch
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Satou,

I'm not understanding?? How does this affect buffering?? Can you elaborate it a bit please!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Sep 26, 2006
Posts: 9345
    
    2

I'm talking about buffering the response!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Servlets also buffer the response. See ServletResponse#setBufferSize
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Sep 26, 2006
Posts: 9345
    
    2

Satou! - Agreed. Thanks!
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

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 ]

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Vishal Chugh
Ranch Hand

Joined: Oct 27, 2007
Posts: 177
This was good one .

You all think so deep , great..


Thanks
Vishal Chugh
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: JSP Page doubt?
 
Similar Threads
jxl.jar to be used in a .jsp file
JSP Utility Function
Error while retrieving column from database
jsp page
AJAX Problem in responseText