Here we will be boosting the web application’s ability to retrieve records from the database. To provide this, we save the table’s data in a collection and reuse that collection in our servlet. As a result, we aren’t repeatedly querying the database. We are enhancing our performance as a result of this. In order to begin, we must first generate the following table:
Implementation: In this case, we’ve generated the following six resources:
Two links in the index.html file transmit requests to the servlet. User.java is a straightforward bean class with three attributes and their associated getters and setters. The database table is represented by this class. The listener class is MainListenerGfg.java. The contextInitialized method of ServletContextListener is called by default when the project is deployed. We are retrieving the table’s records and saving them in the User class object, which has been added to the ArrayList class object. Lastly, the ArrayList class object will be used to hold all of the table’s records (collection).
Finally, we save the ArrayList object as an attribute in the ServletContext object so that we can utilize it in the servlet. Servlet1.java retrieves and outputs information from the servlet context object. Servlet2.java is identical to Servlet1.java. The information from the servlet context object is printed by this servlet. Information regarding servlets and listeners may be found in web.xml.
<a href="servlet1">This is first servlet</a>| //first link
<a href="servlet2">This is second servlet</a> //second link
Please Login to comment...