Summary of Java Web servlet development (2)

1、 ServletConfig explanation

1.1. Configure servlet initialization parameters

In the servlet's configuration file web XML, you can use one or more < init param > tags to configure some initialization parameters for the servlet.

For example:

1.2. Get the initialization parameters of the servlet through ServletConfig

When the servlet is configured with initialization parameters, the web container will automatically encapsulate these initialization parameters into the ServletConfig object when creating the servlet instance object, and pass the ServletConfig object to the servlet when calling the init method of the servlet. Further, we can get the initialization parameter information of the current servlet through the ServletConfig object.

For example:

The operation results are as follows:

2、 ServletContext object

When the web container starts, it will create a corresponding ServletContext object for each web application, which represents the current web application. The reference of ServletContext object is maintained in ServletConfig object. When writing servlet, developers can use ServletConfig The getservletcontext method obtains the ServletContext object. Since all servlets in a web application share the same ServletContext object, the communication between servlet objects can be realized through the ServletContext object. ServletContext objects are also commonly referred to as context domain objects.

3、 Application of ServletContext

3.1. Multiple servlets realize data sharing through ServletContext objects

Example: servletcontextdemo1 and servletcontextdemo2 realize data sharing through ServletContext objects

First run servletcontextdemo1 to store the data in the ServletContext object, and then run servletcontextdemo2 to take out the data from the ServletContext object, so as to realize data sharing, as shown in the following figure:

3.2. Get initialization parameters of web application

On the web Use the < context param > tag in the XML file to configure the initialization parameters of the web application, as shown below:

Get the initialization parameters of the web application. The code is as follows:

Operation results:

3.3. Request forwarding with ServletContext

ServletContextDemo4

ServletContextDemo5

Operation results:

The access is servletcontextdemo4, but the browser displays the content of servletcontextdemo5, which is to use ServletContext to realize request forwarding

3.4. Use ServletContext object to read resource file

The project directory structure is as follows:

Code example: use ServletContext to read resource files

The operation results are as follows:

Code example: use the class loader to read the resource file

The operation results are as follows:

4、 Cache the output of the servlet on the client

For data that does not change frequently, a reasonable cache time value can be set in the servlet to avoid frequent requests from the browser to the server and improve the performance of the server. For example:

The above is the whole content of this article. I hope it will be helpful for you to master the development technology of Java Web servlet.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>