Declare the variable OUTSIDE a the get/post method scope, you will then be able to increment it on each call to the servlet. See below:
private int counter;
private Object lock;
public void init() throws ServletException{
//init lock
lock = new Object();
// create variable
counter = 0;
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//increment your counter here
synchronized(lock){
counter++;
}
response.setContentType("text/html");
// here you can actually return the counter to the browser.
PrintWriter out = response.getWriter();
out.println("<h1>" + counter + "</h1>");
}
9
solved Why my servlet do not creates a new thread on a request? [closed]