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



Home Servlets Time Updater in Servlet

Related Tutorials


 
 

Share on Google+Share on Google+

Time Updater in Servlet

Advertisement
In this program we are going to make one program on servlet which will keep on updating the time in every second and the result will be displayed to you.

Time Updater in Servlet

     

In this program we are going to make one program on servlet which will keep on updating the time in every second and the result will be displayed to you. 

To make this servlet firstly we need to make a class named TimeUpdater.  The name of the class should be such that it becomes easy to understand what the program is going to do. Call the method getWriter() method of the response object which will return a PrintWriter object. Use the method getHeader() of the response object to add a new header. We can also use setHeader() in place of getHeader(). The setHeader() method overrides the previous set header. Now by using the PrintWriter object display the result on the browser.

The code of the program is given below:

 

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

public class TimeUpdater extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw = response.getWriter();

response.addHeader("Refresh", "1");
pw.println(new Date().toString());
}
}

The output of the program is given below:

Advertisement

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: March 12, 2008

Related Tutorials

Discuss: Time Updater in Servlet   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Aarish Narang
January 16, 2012
java

please help how to run servlet program in eclips
DMCA.com