Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JSP  flush

Posted on 2005-04-22
13
Medium Priority
?
2,655 Views
Last Modified: 2012-08-13
is there any necessity of flush in JSP page ?


how do i flush in  JSP page .  whats the syntax ?  
because when u use JSTL and bean then you can write  "out.flush()"  ,   so in that case how can i  flush ?



my question is : how can i flush in my JSP page  while using bean and JSTL ?
0
Comment
Question by:cofactor
  • 5
  • 5
  • 3
13 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 80 total points
ID: 13841059
<% out.flush() ; %>

should do it...

>> my question is : how can i flush in my JSP page  while using bean and JSTL ?

I don't understand :-/
0
 
LVL 29

Assisted Solution

by:bloodredsun
bloodredsun earned 80 total points
ID: 13841066
javax.servlet.jsp.JspWriter.flush() flushes the stream.

>> is there any necessity of flush in JSP page ?
No really but see below.

>>how do i flush in  JSP page .  whats the syntax ?  
<% out.flush() ;%>

>>my question is : how can i flush in my JSP page  while using bean and JSTL
don't know if you can via JSTL, but then again, why do you need to do it? It may be a hangover when you called jsp:includes with a flush attrbute, where now you can use c:import which does it for you.

flush() is really only needed on 2 types of occassions, when you need to send some information to the user to prevent timeouts on pages that take some time to complete an action (a bit of a hack really) and when you are using an outputstream for somehting.

Take this example.

<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>
<%@page contentType="image/gif" %><%
    OutputStream o = response.getOutputStream();
    InputStream is =
      new URL("http://myserver/myimage.gif").getInputStream();
    byte[] buf = new byte[32 * 1024]; // 32k buffer
    int nRead = 0;
    while( (nRead=is.read(buf)) != -1 ) {
        o.write(buf, 0, nRead);
    }
    o.flush();
    o.close();// *important* to ensure no more jsp output
    return;
%>

You can see that you need to call flush here to ensure that call the bytes written to the stream are outputted.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13841076
>>     o.flush();
>>     o.close();// *important* to ensure no more jsp output

This is the best way of doing it -- as although most Implementations of OutputStream call flush() in the close() method, it is not a requirement of the specification, and calling it twice hurts no one...

I'll stop typing now ;-)

hehehe
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 29

Expert Comment

by:bloodredsun
ID: 13841099
Tim, do you ever call out.flush() in your JSPs anymore? I can't remember the last time I did in a JSP. In download servlets for  pdfs, docs, and ppts yes, but not JSPs.....
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13841315
No, never..

I only ever flush a stream in a servlet (like if I'm generating images or something)...

Never called it in a JSP (that I can recall) :-/
0
 

Author Comment

by:cofactor
ID: 13841456
>don't know if you can via JSTL, but then again, why do you need >to do it? It may be a hangover when you called jsp:includes with a >flush attrbute, where now you can use c:import which does it for >you.

yes, i was mentioning exactly  this thing.    do u mean to use c:import  tag and relax without bothering   flush ?



>flush() is really only needed on 2 types of occassions, when >you >need to send some information to the user to prevent >timeouts on >pages that take some time to complete an action >(a bit of a hack >really) and


not able to understand . can u explain more with an example



>when you are using an outputstream for somehting.

this part is  fine.
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13841538
>>re: c:import
Yes, as with jsp:include you used to have to say: <jsp:include page="test.jsp" flush="true" />

>>an example

Sure,

<%@ page language="java" contentType="text/html" %>
<% out.flush ;%>
<%--this sends something to the browser--%>

<%--
Some JSP code here: An example may be some db stuff that takes a while,
maybe a minute or more. With the above code the browser will not "time-out" or show "server not found" as you have sent a response, just a very small one as you clear the JSP buffer using flush()
--%>

<html>
<body>

blah
blah

some ResultSet stuff

</body>
</html>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13841576
Some browsers (like Mozilla) will show partial data as it arrives from the site, so out.flush() may make your data appear "line by line" if you are doing a big process..

IE doesn't render till it has all the data though...  so this doesn't work on IE
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13841599
>>IE doesn't render till it has all the data though...  so this doesn't work on IE

But it does prevent IE from timing out as it has received some response from the website, does it not?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13841627
true...
0
 

Author Comment

by:cofactor
ID: 13975526
i am very sorry for late response. i am very much satisfied with your answers. increasing points
0
 

Author Comment

by:cofactor
ID: 13975542
Sorry,  the Accepted and assisted answer  has been reversed . anyway, it was a big help.

thank you
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13975587
no probs
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Machine Learning is one of the profound applications of AI and therefore, just like AI, it is surrounded by myths and fears. Check out these facts about ML that demystify the related myths.
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question