
public class SimpleCounter extends HttpServlet{ int counter = 0; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); counter++; pw.println("At present the value of the counter is " + counter); } }
In the above example, which keeps track how many times the servlet has been accessed, Can I intitialize counter variable in init() method? How will it be different than global initialization(as shown above)?

Hi Friend,
Please visit the following links:
http://www.roseindia.net/servlets/SimpleCounterInServlet.shtml
http://www.roseindia.net/servlets/counter.shtml
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.