In this section we will discuss how can we update the time on the server
Updating Time
In this section we will discuss how can we update the time on the server.
The example is being given below will show you that how can you update the time. In example I used the addHeader() and setHeader() methods.
The setHeader() method sets the key/value in any request header.
The addHeader() method adds the user specified header.
Example : UpdatingTime.java
import java.text.DateFormat; import java.util.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class UpdatingTime extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/plain"); PrintWriter pw= res.getWriter(); res.addHeader("Language", "hi"); Locale locale = new Locale("hi", ""); DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); fmt.setTimeZone(TimeZone.getDefault()); res.setHeader("Refresh", "1"); pw.println("This page is refreshing in every 1 sec."); pw.println(fmt.format(new Date()).toString()); } }
web.xml
<web-app> <servlet> <servlet-name>UpdatingTime</servlet-name> <servlet-class>UpdatingTime</servlet-class> </servlet> <servlet-mapping> <servlet-name>UpdatingTime</servlet-name> <url-pattern>/UpdatingTime</url-pattern> </servlet-mapping> </web-app>
Output : When you will this example the following output will look you. The time will refreshed in every 1 sec.
[ 0 ] Comments