A significant difference between a servlet and a JSP is that the servlet can use the Web.xml file configuration to have the servlet start the servlet automatically when the Web container is started. You can use the unchanged data from this feature of the servlet to load the Web application server beforehand for caching.
Assuming that the users of our system have been created before the system is deployed and will not change much later, we can cache the Web application when it is started by caching it in the Web application server memory, which can be manually invoked if the user changes. Here we create this usercacheservlet through the servlet Wizard, which automatically downloads and caches all user IDs and user names for the system when the Web container starts:
1. Start the Create Servlet Wizard, fill in the servlet name
Start the first step of creating the Servlet Wizard by file->new...->web-> double-clicking the standard servlet icon, as shown in the following illustration:
Figure 2 Completing the servlet name
In class name, fill in the servlet name: Usercacheservlet, and fill in the package with Bookstore.servlet as the package name. Press next to the next step.
2. Select the method that the servlet will implement.
We have previously described the way in which the servlet responds to HTTP requests through different doxxx () methods, and you can choose which doxxx () methods you want to define in step 2nd of the wizard. By default, the Doget () method is checked to access the servlet through an HTTP GET request. When a servlet is accessed through a URL with a parameter, the servlet responds to the request with the Doget () method. Since we just assume that the user data is not constantly changing, not that it never changes, so we want to automatically load the user data into the cache through Usercacheservlet when the Web container is initialized, and when the user data of the database table T_user is changed, We can manually invoke Usercacheservlet to flush the user data in the cache.
The Usercacheservlet is initialized automatically when the Web container is started, when the init () method is invoked, we can load the user data through the init () method, and when the user refreshes the user data through the URL request, Usercacheservlet passes Doget () Method responds to this HTTP GET request. That is, we need to implement the Doget () method, so we accept the default settings for the wizard, as shown in the following illustration:
Figure 3 Selecting the servlet response method to overwrite
Press Next to skip step 3rd through step 4th of the wizard.