1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package controller; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CountDown extends HttpServlet { private static final long serialVersionUID = 1L; public CountDown() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><head><title>Count Down</title></head>"); out.println("<center><h1>Count Down Example</h1></center>"); if(request.getParameter("count") != null) { int strCount = Integer.parseInt(request.getParameter("count")) - 1; if(strCount == 0) { out.println("<a href=\"" + getURLWithContextPath(request) + "\">Start Again...!!!</a>"); } else { response.setHeader("REFRESH", "1; " + getURLWithContextPath(request) + "?count=" + strCount); out.println("<h1>" + strCount + "</h1>"); } } else { out.println("<form method=\"GET\" action=\"" + getURLWithContextPath(request) + "\">" + "<span style=\"margin-right:5px;\">Set Count</span>" + "<input type=\"text\" name=\"count\" /> <input type=\"submit\" value=\"Start Counter\" />" + "</form>"); } out.println("</html>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public static String getURLWithContextPath(HttpServletRequest request) { return request.getScheme() + "://" + request.getServerName() + ":" +request.getServerPort() + request.getRequestURI(); } } |