0

I notice that when my Java servlet starts up, the doGet method gets called 3 times. After that, if I refresh the web page that uses that servlet, it only gets called once, as it should. My servlet is running on the Google App Engine, so perhaps this has something to do with that. Or is this normal for any servlet? Here is the servlet code:

package com.example.demos;

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;

/**
 */
@SuppressWarnings("serial")
public class HelloWorldServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter out = resp.getWriter();
        out.println("Hello, world");
    }

    public void init() throws ServletException {
        System.out.println("Servlet: init()");
    }

    public void destroy() {
        System.out.println("Servlet: destroy()");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("Servlet: processRequest()");
    }
}
Johann
  • 27,536
  • 39
  • 165
  • 279
  • can you check in the browser's developer tools / network if you have only one call when you start – Edwin Jan 31 '18 at 08:34
  • That's not normal. Something weird is going. As Edwin suggested, can you check the browser console ? – Suresh Atta Jan 31 '18 at 08:36
  • Actually, I start the app using Debug in IntelliJ. The breakpoint in the doGet is hit twice before the browser tab is launched. Once the browser tab is launched, the breakpoint is hit again. If I refresh the page, the breakpoint only gets hit once. So the browser is really only sending one request. – Johann Jan 31 '18 at 08:40
  • The question has been marked as duplicate. However, the link to the answer indicates that Tomcat is being used. App Engine uses Jetty, but in all likelihood, the problem is the same as it was for Tomcat, so I'll accept the answer. – Johann Jan 31 '18 at 09:37

0 Answers0