Servlet:

@WebServlet("/") public class MainServlet extends HttpServlet { double counter = 0; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setAttribute("counter", counter); req.setAttribute("name", "VII"); req.getRequestDispatcher("mypage.jsp").forward(req, resp); counter++; } } 

Jsp page:

 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>Helo, ${name}!</h1> <h3>This page was visited: <strong>${counter}</strong> time</h3> </body> </html> 

Why counter is increased by 2 instead of 1: Launched via debugger ... doGet is called 2 times when the page is reloaded, but why? Deployl on tomcat (full code: github )

  • five
    Because most browsers make an additional request to get favicon. - Sergey Gornostaev
  • Check in debug mode req.getRequestURL() - default locale

1 answer 1

In general, the problem is that the deployment to the main localhost (localhost: 8080 /) and the servlet was available at the same address (localhost: 8080 /):

  @WebServlet("/") ... 

Well, I found the solution ... Even two (you can apply both at once):
1. Application launch configuration -> Deployment -> Application context: instead of "/" indicated "/ mysite"
2. Change the @WebServlet ("/") annotation to @WebServlet ("/ servlet")

  • Both options do not work. - Drakonoved
  • Yes ... I remember that I began to climb into all the menus in a row and poke everything I don’t like. But it seems to me that the default favicon has somehow been downloaded, and the request has ceased to be duplicated. - Sublimer