Improving Servlet performance to fetch records from databaseIn this example, we are going to improve the performance of web application to fetch records from the database. To serve this, we are storing the data of the table in a collection, and reusing this collection in our servlet. So, we are not directly hitting the database again and again. By this, we are improving the performance. To run this application, you need to create following table with some records. Example to Improve the performance of servlet to fetch records from databaseIn this example, we have created 6 pages.
This html file contains two links that sends request to the servlet. 2) User.java This is simple bean class containing 3 properties with its getters and setters. This class represents the table of the database. 3) MyListener.javaIt is the listener class. When the project will be deployed, contextInitialized method of ServletContextListener is invoked by default. Here, we are getting the records of the table and storing it in the User class object which is added in the ArrayList class object. At last, all the records of the table will be stored in the ArrayList class object (collection). Finally, we are storing the ArrayList object in the ServletConext object as an attribute so that we can get it in the servlet and use it. 4) MyServlet1.javaThis servlet gets the information from the servlet context object and prints it. 5) MyServlet2.javaIt is same as MyServlet1. This servlet gets the information from the servlet context object and prints it. 6) web.xmlHere we are containing the information about servlets and listener. download this example (developed using Myeclipse IDE) download this example (developed using Eclipse IDE) download this example (developed using Netbeans IDE) |