[JavaWeb] Overview of Servlet

Table of contents

1. What is Servlet

Two, use Servlet

1. Introduction to Servlet

2. Servlet execution process

3. The implementation relationship of Servlet

4. Using Servlet templates in Eclipse

1. New Servlet

2. Complete the configuration of the Servlet

5. Servlet life cycle

1. What is the life cycle

2Servlet life cycle

3 code demonstration Servlet life cycle

6. Loading at startup of Servlet

1. Why use boot-time loading

2. What is load at startup

3. Load when the configuration is complete and start

7. Servlet access path configuration

1. The configuration method

2. Access priority

8. ServletConfig object

1. Get the ServletConfig object:

2. API of ServletConfig object

3. Demonstrate the API of ServletConfig

9. Obtain project information of ServletContext object

1. What is ServletContext

2. The role of the ServletContext object

10. The ServletContext object accesses data as a domain object

1. What is a domain object


1. What is Servlet

Servlet is actually a small Java program running on a web server, which is used to process requests sent from web clients and respond to requests.

Two, use Servlet

1. Introduction to Servlet

  1. Write a Java class that implements the Servlet interface
  2. Configuring Servlets

1.1 Create packages and classes

1.2 Implement the Servlet interface

In this, I want to talk about why the browser can print out the string Hello Servlet..., among which there is a getWriter() method in the ServletResponse class, which returns a PrintWriter object that can send character text to the client, and the PrintWriter There is a println(String s) method in the class. This method can print the string and then terminate the line change, so it explains why the browser can output the string.

1.3 Configure this class in web.xml

 Note here that the name of the first Servlet above can be written casually, but the Servlet name in the mapping below should be the same as above, that is, it should be consistent.

1.4 Accessing Servlets

http://localhost:8080/web_test/hello

2. Servlet execution process

Let me explain the process. First, the browser enters the URL, followed by /hello. After the browser sends the request, it will find the configuration access path /hello in web.xml, which is step 1, and then it will know the name of the Servlet in the mapping. , which is step 2, and then according to 2, find the Servlet with the same name above, which is step 3, and then find the java class we wrote according to the full path of the Servlet, and then execute the service method inside to output the content to the browser.

3. The implementation relationship of Servlet

Servlet interface                      

         |

         |

The GenericServlet class is a generic Servlet, which is a protocol-independent Servlet

         |

         |

HttpServlet class Http dedicated Servlet

At the beginning of SUN's design, it was ambitious, thinking that in the future, the Internet will not only use the http protocol, but also can be realized through GenericService. HttpServlet is a protocol-related Servlet, which is specially used to process HTTP protocol requests. Usually, a Servlet is written, and the Servlet is usually inherited from HttpServlet, and then the service method is rewritten.

Inside the service method, different doXXX methods are executed according to different request methods (the get request executes the doGet method, and if it is a post request, the doPost method is executed).

Therefore, after inheriting HttpServlet, there is no need to rewrite the service method, only the doGet and doPost methods need to be rewritten, and the code of the content of the request processing is often the same, so it is necessary to let doGet and doPost call each other to simplify the code.

4. Using Servlet templates in Eclipse

1. New Servlet

2. Complete the configuration of the Servlet

5. Servlet life cycle

1. What is the life cycle

Life cycle: the process of an object from creation to destruction.

2Servlet life cycle

Servlet life cycle: the process of a Servlet object from creation to destruction.

  1. When is the servlet created and when is it destroyed?
  2. There are init (), service (), and destroy () methods in Servlet . These methods are called methods related to the Servlet life cycle.
  3. The Servlet is instantiated when it is accessed for the first time. As long as the Servlet is instantiated, the init method in the Servlet will be executed (init will only be executed once). For any request sent from the client, the service method in the Servlet will be executed (different doXXX methods are called within the service method according to the different request methods). When the Servlet is removed from the server or the server is shut down, the Servlet object is destroyed, the destroy method inside will be executed, and then garbage collection will recycle it.
  • 3 code demonstration Servlet life cycle

6. Loading at startup of Servlet

1. Why use boot-time loading

The Servlet object will be created when it is accessed for the first time, and the init method will be executed. Assume that some time-consuming operations are done in the init method (for example: some configuration files are loaded and parsing may take 3 seconds). When the first user accesses this Servlet for the first time, it needs to wait for 3 seconds. How to make it not take so long for the first user to visit for the first time?

2. What is load at startup

By default, the Servlet is an object created when it is accessed for the first time. Now, through a configuration, the instantiation process of the Servlet is placed when the server starts (let the server create a Servlet object when it starts ). If you do this now, the time spent before will be spent together when the server starts. For users, there is no need to spend this extra time.

3. Load when the configuration is complete and start

Let me explain here that the smaller the number in load-on-startup, the higher the priority. Since the tomact server is 1, the priority is the highest, so only 2 can be written here. 

7. Servlet access path configuration

1. Configuration method of <url-pattern>

The first full path matching: start with /, for example: /ServletDemo1 or /aaa/ServletDemo2

The second type of directory matches:

Start with / and end with /* For example: /* /aaa/* /aaa/bbb/*

Here * stands for any character.

The third extension matches

Do not start with /, but start with * For example: *.action *.do *.jsp

As long as the suffix is ​​the same as the one behind *, it can be accessed, such as aa.action, aa.do, etc.

2. Access priority

full path match > directory match > extension match

8. ServletConfig object

ServletConfig is used to obtain Servlet-related configuration objects.

1. Get the ServletConfig object:

2. API of ServletConfig object

  1. Get the initialization parameters of the Servlet
  2. Get the ServletContext object
  3. Get the name of the servlet

3. Demonstrate the API of ServletConfig

Configure initialization parameters:

accomplish:

9. Obtain project information of ServletContext object

1. What is ServletContext

ServletContext: Servlet context object. The ServletContext object knows both before and after the Servlet. There is only one of this object per web project. A separate ServletContext object is created for each web project at server startup.

2. The role of the ServletContext object

2.1 Function 1: Used to obtain web project information

Because a web project has only one ServletContext object, this object knows about the relevant content of the entire project.

2.1.1 Get the MIME type of the file:

2.1.2 Obtain the project name of the web project request:

2.1.3 Get the initialization parameters of the web project:

 2.2 Function 2: Read the files under the web project

You can read files (in java projects) using IO streams before. Now it is a web project, and the web project needs to be published under tomcat to be accessed. If you use traditional IO to obtain files under the web project, there will be problems (reason: the relative path is used in the path, which is relative to the JRE environment).

In the web project, you need to use an absolute path.

2.2.1 Code implementation for reading files

10. The ServletContext object accesses data as a domain object

1. What is a domain object

Domain object: refers to storing data in the domain object, and this data will have a certain scope of action. A domain refers to a certain scope of action.

ServletContext as an API for domain objects:

The method of storing data:

 Method to get data:

To remove data:

The scope of ServletContext as a domain object:

ServletContext creates a ServletContext object for each web project when the server starts. The ServletContext object is destroyed when the web project is removed from the server, or when the server is shut down. The data saved in the ServletContext always exists (the ServletContext object is destroyed when the server is closed, and then the data inside will become invalid). Scope: The entire web application.

ServletContext domain object code demo:

Here, two servlet files are created. After using ServletContext to assign an object in one of them, the value can also be obtained in the other Servlet file, which shows that the role of the ServletContext object is the entire web application. 

Guess you like

Origin blog.csdn.net/wang_qiu_hao/article/details/126494471