Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use user cache Servlet

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use user cache Servlet". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

A major difference between Servlet and JSP is that Servlet allows Servlet to start Servlet automatically when the Web container starts through the configuration of the web.xml file. Data that does not change with this feature of Servlet can be loaded into the Web application server in advance for cache use.

Assuming that the users of our system have been created before the system deployment and do not change frequently in the future, then we can download and cache the Web application into the memory of the Web application server when it starts, and if the user changes, we can call this Servlet to refresh it manually. Let's create this user cache Servlet through the Servlet wizard, which automatically downloads and caches all system users'Id and usernames when the Web container starts:

1. Launch the create Servlet wizard and fill in the Servlet name

2. Select the method to be implemented by Servlet

We have previously introduced how Servlet responds to HTTP requests through different doXxx () methods, and you can choose which doXxx () methods need to be defined in step 2 of the wizard. By default, the doGet () method is checked to access the Servlet through a HTTP GET request. When Servlet is accessed through the URL with parameters, the Servlet responds to the request with the doGet () method. Since we only assume that the user data does not change often, it does not mean that it will never change, so when we initialize the Web container, we want to automatically load the user data into the cache through user cache Servlet. When the user data of the database table T_USER changes, we can manually call user cache Servlet to refresh the user data in the cache.

The user cache Servlet is initialized automatically when the Web container starts, and the init () method is called. We can load the user data through the init () method. When the user refreshes the user data through the URL request, the user cache Servlet responds to the HTTP GET request with the doGet () method. That is, we need to implement the doGet () method, so we accept the wizard's default settings

3. Specify the path to access Servlet

The name given by ◆ Name:user cache Servlet,Servlet in the web.xml configuration file

◆ URL pattern:/user cache Servlet, the matching path to access this Servlet. After you specify this access path, assume that the Web application is deployed under http://localhost:8080/webModule, then access the Servlet through http://localhost:8080/webModule/user cache Servlet.

Press Finish directly to create Servlet.

Open the web.xml file and you can find deployment description information about user cache Servlet claims and access:

◆ < Servlet > node: the name and class name that describe the Servlet.

◆ < Servlet-mapping > node: describes the Servlet access matching path.

Double-click the webModule node of the project pane resource tree, and JBuilder opens the Web module DD editor (Web Module DD editor) for editing the web.xml file in the content pane, and the structure pane shows the structure of the web.xml file.

In the init () initialization method, the User List.fill User () method is used to download and cache user record information from the database, and User List.fill User () is also referenced in the doGet () method. When a user accesses user cache Servlet through URL, the doGet () method is called, refreshes the cached user data, and displays a "refresh successful" prompt. 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 > that's all for "how to use user cache Servlet". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report