Can anyone tell me how to explicitly persist the value of the counter variable ??
Two ways to persist the Counter value that I know
1. Save counter value to a flat file on disk
OR
2. Save counter value to a Database table
Assuming
a.you have Servlet/JSP application running on any Web container
b. you want to persist value of Counter
Your alogrithm can be
Step 1 -> Treat your counter as Application/ServletContext level variable (rather than any servlet class variable).
Step 2 ->
Implement ServletContextListener interface and override its two methods
Step 3 -> After creating the listener class, you register same in web.xml file as listener.
Sample code is shown below (CustomServletContextListener is the class which implements ServletContextListener )
Step 4 -> When ever you want to increase Counter value, just get current value from Servlet Context and , increase that value and set it back in Servlet Context. In this way, counter will always show current value on your
JSP.
Oracle certified JPA Developer (1Z0-898),Oracle certified Java 8 Programmer I (1Z0-808), Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist