Thursday, May 26, 2011

How to Autorefersh the Servlet

This code contains the how 2 auto refresh the servlet and add display how many time u refresh this page

package org.best.example;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class AutoRefresh extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
HttpSession session = req.getSession();
Long time = (Long) session.getAttribute("time");
if (time==null)
session.setAttribute("time",new Long(0));
long temp = 1;
if (time!=null)
temp = (time.longValue())+1;
if(temp<5)
res.addHeader("Refresh","15");

out.println("<html><head><title>Client Refresh</title></head><body>");
out.println("Viewed this page " +temp+"time.");
session.setAttribute("time",new Long(temp));
out.println("</body></html>");
}
}

No comments: