A major difference between Servlet and JSP is that Servlet can enable the Servlet to automatically start the Servlet when the web Container starts through the configuration of the Web. xml file. You can use this feature of Servlet to load unchanged data to the Web application server in advance for caching.
Assume that the users of our system have been created before the system deployment and will not change frequently in the future, then we can download and cache the Web application to the Web application server memory when it is started. If the user changes, we can manually call this Servlet for Refresh. The user cache Servlet is created in the Servlet wizard. It automatically downloads and caches all user IDs and usernames of the system when the Web Container starts:
1. Start the Servlet creation wizard and enter the Servlet name
2. Select the Servlet implementation method
We have already introduced Servlet's response to HTTP requests through different doXxx () methods. You can select the doXxx () method to be defined in step 1 of the wizard. By default, the doGet () method is selected to access the Servlet through an http get request. When you access the Servlet through a URL with parameters, the Servlet uses the doGet () method to respond to this request. Because we only assume that user data does not change frequently, it does not mean that user data will never change. Therefore, we hope to automatically load user data to the cache through the user cache Servlet during Web Container initialization, when the user data of the database table T_USER changes, we can manually call the user cache Servlet to refresh the user data in the cache.
The user cache Servlet is automatically initialized when the Web Container starts. At this time, the init () method is called. We can use the init () method to load user data. When a user refreshes user data through a URL request, the user cache Servlet responds to this http get request using the doGet () method. That is to say, we need to implement the doGet () method, so we accept the default settings of the wizard.
3. Specify the Servlet access path
◆ Name: user cache Servlet, the Name of the Servlet in the web. xml configuration file
◆ URL pattern:/user cache Servlet, matching path for accessing this Servlet. After this access path is specified, assume that the Web application is deployed under http: // localhost: 8080/webModule: 8080/webModule/user cache Servlet access Servlet.
Directly press Finish to create the Servlet.
Open the web. xml file, and you can find the user cache Servlet declaration and access deployment description:
◆ <Servlet> node: Describes the Servlet name and class name.
◆ <Servlet-mapping> node: Describes the Servlet access matching path.
Double-click the webModule node of the Resource Tree in the project pane. In the content pane, JBuilder opens the webModule node used to edit the web. the xml file's Web Module DD editor). The structure pane displays the web. xml file structure
Use the User List in the init () initialization method. the fill User () method downloads and caches User record information from the database, and also references the User List in the doGet () method. fill User (): when a user accesses the User cache Servlet through a URL, the doGet () method is called, the cached user data is refreshed, and the "Refresh successful" prompt is displayed. The Code is as follows:
- <web-app>
- …
- <servlet>
- <servlet-name>usercacheservlet</servlet-name>
- <servlet-class>bookstore.servlet.UserCacheServlet</servlet-class>
- <load-on-startup>2</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>usercacheservlet</servlet-name>
- <url-pattern>/usercacheservlet</url-pattern>
- </servlet-mapping>
- …
- </web-app>
- DoFilter method in Servlet
- Configure Servlet Filter
- Install Servlet and JSP development tools
- Java Servlet getting started
- What is a Servlet filter?