import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import Counter.CounterController;

public class CounterServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws
      ServletException, IOException {
        serveRequest(req, res);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws
      ServletException, IOException {
        serveRequest(req, res);
    }

    private void serveRequest(HttpServletRequest req, HttpServletResponse res) throws
      ServletException, IOException {

        CounterController cc = CounterController.getInstance();

        int visits = cc.incrementCounter();

        res.setContentType("text/html");

        PrintWriter out = res.getWriter();
        out.println("<h1>Contador de visitas</h1>");
        out.println("<p>" + visits + "</p>");
        out.println("<p><a href=\"counter\">Recargar</a> | <a href=\"counter.html\">Volver atr&aacute;s</a></p>");
    }
}
