I don't know how your links are created, but it looks like you will make a GET request to your servlets. Knowing this, each servlet should manage the counter hit for the page and since this value should be known for every user it will be best to save it in application scope rather than request or session. More of this here and How do servlets work? Instantiation, sessions, shared variables and multithreading.
I'll post a sample of jsp and servlet that handles a counter for a single link. You should be able to use it to handle your links as well.
index.jsp (other elements like <head>
and <html>
are wortheless for the example)
<body>
Hit the button to add a value to the application counter
<br />
<form action="HitCounterServlet" method="GET">
<input type="submit" value="Add counter hit" />
</form>
<br />
Total hits: ${applicationScope['counter']}
</body>
HitCounterServlet
@WebServlet(name = "HitCounterServlet", urlPatterns = {"/HitCounterServlet"})
public class HitCounterServlet extends HttpServlet {
private static final Object counterLock = new Object();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = request.getServletContext();
updateHitCounter(context);
String originalURL = "index.jsp";
//in case you want to use forwarding
//request.getRequestDispatcher(originalURL).forward(request, response);
//in case you want to use redirect
response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/" + originalURL));
}
private void updateHitCounter(ServletContext context) {
//since more than a request can try to update the counter
//you should protect the update using a synchronized block code
synchronized(counterLock) {
Integer counter = (Integer)context.getAttribute("counter");
if (counter == null) {
counter = 0;
}
counter++;
context.setAttribute("counter", counter);
}
}
}
Try this in different browsers and you will see how the counter maintains the same state across them.
In order to save the counter hit in your database, you should just change the code in the updateHitCounter
function for code that will connect to your database and execute the update statement to your database field.