Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Servlets Refresh a Web Page Using In Servlet

Related Tutorials


 
 

Share on Google+Share on Google+

Refresh a Web Page Using In Servlet

Advertisement
In this section, you will learn how to refresh servlets page.

Refresh a Web Page Using In Servlet

     

In this simplified example we develop an application to Refresh a web Page using Servlet. We create two file timer.html and timer.java. When a web page ("timer.html") run on browser then it will call to Servlet ("timer.java") and refresh this web page and print the current Date and Time after 10 sec on the browser as a output.

Step 1: Create a web page(timer.html) to call a Servlets.

timer.html 

<HTML>
 <HEAD>
  <TITLE>Refresh Servlet Timer</TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <style type="text/css">
A:link {text-decoration: none;
    padding: 3px 7px;
    margin-right: 3px;
 
    border-bottom: none;
 
    color: #2d2b2b;  }
A:visited {text-decoration: underline;
    padding: 3px 7px;
    margin-right: 3px;
   
  
    color: #2d2b2b; }
A:active {text-decoration: none}
A:hover {text-decoration: none;
    padding: 3px 7px;
    margin-right: 3px;
    border: 0px;

    color: #2d2b2b; }
</style>

 
 </HEAD>

 <BODY>
 <br><br><br> <br><br><br>
 <table width="200px" height="100px" 
  align="center" bgcolor="#BBFFFF" border=0>
 <tr> 
      <td style="text-align:top;" valign="middle" 
      align="center" border=0>
   <a href="timer" ><b>Refresh Servlet Timer</b></a>
    </td>
 </tr>

 </BODY>
</HTML>

 

Step 2:Create a Servlet (timer.java) which refresh the page after every 10 seconds. 

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


public class timer extends HttpServlet{ 
 public void doGet(HttpServletRequest request, 
  HttpServletResponse response)
  throws 
ServletException,IOException{
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  Date now = new Date(); // The current date/time
  out.println("<html>");
  out.println("<head><title> Time Check </title></head>");
  out.println("<body>");
  out.println
  ("<table  width='100%' align='center' valign='top'>");
  out.println("<tr>");
  out.println("<td>&nbsp;");
  out.println("</td>");
  out.println("</tr>");
  out.println("</tr>");
  out.println("<tr>");
  out.println("<td valign='top' align='center' valign='top'>");
  out.println
   ("<p style='color:#00000;font-size:20pt'>"+
  "<b>The Time is Refresh After 10 Seconds.</b></p>");
  out.println("<td>");
  out.println("</tr>");
  out.println("<tr>");
  out.println("<td>&nbsp;");
  out.println("</td>");
  out.println("</tr>");
  out.println("</tr>");
  out.println("<tr>");
  out.println("<td>&nbsp;");
  out.println("</td>");
  out.println("</tr>");
  out.println("</tr>");
  out.println("<tr>");
  out.println("<td style='background-color:#C6EFF7;color:blue;'"+
  " width='50' align='center'>");
  out.println("<b>The current time is: " + now + "</b>");
  out.println("</td>");
  out.println("</tr>");
  out.println("<table>");
  out.println("</body></html>"); 
  response.setHeader("Refresh", "10");
  
  }
}

Save the above file into "timer\WEB-INF\classes" directory.

Step 3: Mapping the servlet (timer.java) in to web.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>timer</servlet-name>
<servlet-class>timer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>timer</servlet-name>
<url-pattern>/timer</url-pattern>
</servlet-mapping>

</web-app>

Step 4: Now compile the java code using javac command from command prompt.

Step 5: Start tomcat and type http://localhost:8080/timer/timer.html in the browser and Click on Text Link "Refresh Servlet Timer" . Your browser should display the Current Time and Refresh after 10 seconds.  

Successful Output of the program:

 

Download the full web application shows here.

Download the application

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 26, 2008

Related Tutorials

Discuss: Refresh a Web Page Using In Servlet   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Addy
January 31, 2012
Some mistakes observed

The table tag was not closed and the "td" tag does not have "border" attribute.
monika
July 2, 2012
how to call html from servlet

I have a html page containing meta tag with its url to a servlet displaying an image.Now I need to call that html page into another servlet which should display some text from database along with that first described image. please give some example code.
DMCA.com