Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

New trick: Reload a page N times without JavaScript and refresh tag

JavaFAQ Home » JSP, Servlets Go to all tips in JSP, Servlets

Bookmark and Share

Here is real trick: Reload your page without JavaScript and meta refresh tag exactly N times!

I will show you real trick how you can reload your page exactly N times without meta tags and JavaScript in your JSP or servlet application - pure HTML!

We get used to use the "refresh" meta tag in our pages.

For example like this:


<meta http-equiv="refresh" content="10;">

where the page will refresh every 10 seconds. You can even redirect to another page if you write your code like this:


<meta http-equiv="refresh" content="10;url=http://javafaq.nu">

The drawback of this code is that you have no control on how many times it can be done - refresh without URL redirection will run forever and with URL just once.

Another solution is to use JavaScript function with some kind of counter to reload page N times .

It can be called by "onload()" function from body tag Needless to say that it is most preferred solution. But from my server logs I see that ~10% people surf the web with disabled JavaScript Sad

Here is third way - pure HTML code.

If you need for some reason (and I had such reasons) to load and run some JSP page exactly N times then do the next - add the image which points to your file, like this:


<img id="theId" src="test.jsp" />

This tag is for image loading, but in our case will recall current page. It will not loop forever, as one can expect. Tomcat is wise enough to prevent such recursive looping Smile

You can run the code below on Tomcat and see that will printout ": ----" exactly N times, in our case 3 times.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Test paget</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<body  onload="timer=setTimeout('window.close()', 10000)">

<h4>Test page only, closes self in 10 secs</h4>
   for (int counter = 0; counter < 3; counter ++){
<img id="theId" src="test.jsp" />
    System.out.println(": ----");


If you do not use the JSP just place the image tag N times in your html page, at the end of page before </body> tag, like this:


<img id="theId" src="test.jsp" />
<img id="theId" src="test.jsp" />


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy