java4all@1986 java. Powered by Blogger.

Refreshing a webpage using Servlets?

>> Monday, June 13, 2011

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>quot;);
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");
  
  }
}

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


Welcome to Tomcat

Welcome to Tomcat


timer
timer


timer
/timer



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.

0 comments:

Post a Comment

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP