package com.cv.servlet.refresh;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
/**
* Servlet implementation class RefreshServlet
*
* @author Chandra Vardhan
*/
public class RefreshServlet extends HttpServlet {
private static final Logger LOGGER = Logger.getLogger(RefreshServlet.class);
private static int count = 0;
protected void doPost(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException {
LOGGER.info("LOGGER : RefreshServlet doGet(HttpServletRequest,HttpServletResponse)");
++count;
// Set refresh, autoload time as 5 seconds
response.setIntHeader("Refresh", 3);
// Set response content type
response.setContentType("text/html");
// Get current time
Calendar calendar = new GregorianCalendar();
String am_pm;
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
if (calendar.get(Calendar.AM_PM) == 0)
am_pm = "AM";
else
am_pm = "PM";
String CT = hour + ":" + minute + ":" + second + " " + am_pm;
PrintWriter out = response.getWriter();
String title = "Auto Page Refresh using Servlet";
String docType = "<!doctype html public \"-//w3c//dtd html 4.0 "
+ "transitional//en\">\n";
if ((count%2)==0) {
out.println(docType + "<html>\n" + "<head><title>" + title
+ "</title></head>\n" + "<body bgcolor=\"#0000FF\">\n"
+ "<h1 align=\"center\">" + title + "</h1>\n"
+ "<p>Current Time is: " + CT + "</p>\n");
} else {
out.println(docType + "<html>\n" + "<head><title>" + title
+ "</title></head>\n" + "<body bgcolor=\"#ffff00\">\n"
+ "<h1 align=\"center\">" + title + "</h1>\n"
+ "<p>Current Time is: " + CT + "</p>\n");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
LOGGER.info("LOGGER : RefreshServlet doGet(HttpServletRequest ,HttpServletResponse )");
doPost(request, response);
}
} |
No comments:
Post a Comment