Management system based on Servlet (including server source code + database)

Data download link

introduce

A concise version of the management system based on the Servlet framework   ;

Realize  login  ,  registration  ,  addition  ,  deletion  ,  modification  and  query  ;

Can continue to improve and add front-end, verification, other functions, etc.;

Can be used as  the basic model for Servlet project  development exercises;

Curriculum design  ,  graduation design  development basis;

Any complex framework is a combination and extension of simple knowledge, and learning the basics is the most important thing;

This project is simple, easy to understand the basic principles, and lays the foundation for the development of complex projects such as Servlet, SSH, and SSM.

Environmental preparation

Development platform: Idea 2019

Database: MySQL 5.0.22

Server: Tomcat 9.0.37

Note: IDEA development platform, MySQL database, and Tomcat server need to be installed. The versions may not be exactly the same. Just do a good job in version adaptive configuration.

project structure

1. Database and Java code files 

Enter a picture description

2. Jsp and other front-end code 

Enter a picture description

3.Tomcat server configuration and operation

Enter a picture description

4. MySQL database 

Enter a picture description

Fundamental

1. Related concepts

Servlet is a technology for developing dynamic web pages, which is used to browse and modify data interactively and generate dynamic web content. Nowadays, Servlet is rarely used in Java Web development to develop Web applications, and more is integrated framework development. In fact, most frameworks are encapsulated based on basic tools, such as Struts is the encapsulation of servlet and filter, Hibernate is the encapsulation of JDBC and so on. Therefore, no matter what framework you learn, you must first understand the basic knowledge in depth, so as to have a rational understanding and solution to the problems that arise in the framework.

2. Fundamentals

2.1 Workflow

  1. Web Client sends Http request to Servlet container (Tomcat)

  2. Servlet container receives requests from Web Client

  3. The Servlet container creates an HttpRequest object and encapsulates the information requested by the Web Client into this object.

  4. Servlet container creates an HttpResponse object

  5. The Servlet container calls the service method of the HttpServlet object, and passes the HttpRequest object and the HttpResponse object as parameters to the HttpServlet object.

  6. HttpServlet calls related methods of HttpRequest object to obtain Http request information.

  7. HttpServlet calls the relevant methods of the HttpResponse object to generate response data.

  8. The Servlet container sends the response result of the HttpServlet to the Web Client.

Enter a picture description

Enter a picture description

2.2 Processing request process

  1. The user clicks on a link, which leads to a servlet instead of a static page.

  2. The container "sees" that the request is a Servlet, so it creates two objects, HttpServletRequest and HttpServletResponse.

  3. The container finds the correct Servlet according to the URL in the request, creates or allocates a thread for the request, and passes the request and response objects to the Servlet thread.

  4. The container calls the Servlet's service() method. Depending on the type of request, the service() method calls the doGet() or doPost() method. This assumes that the doGet() method is called.

  5. The doGet() method generates a dynamic page and "stuffs" this page into the response object. It should be noted that the container also has a reference to the response object!

  6. When the thread ends, the container converts the response object into an HTTP response, sends it back to the client, and deletes the request and response objects.

2.3 Working principle

​ 1. First, briefly explain the process of Servlet receiving and responding to customer requests. First, the customer sends a request, and the Servlet calls the service() method to respond to the request. It can be seen from the source code that the method of request in the service() method After matching, choose to call doGet, doPost and other methods, and then enter the corresponding method to call the method of the logic layer to realize the response to the customer. There are no methods such as doGet(), doPost(), etc. in the Servlet interface and GenericServlet. These methods are defined in HttpServlet, but they all return error information. Therefore, every time we define a Servlet, we must implement doGet or doPost etc these methods.

​ 2. Every custom Servlet must implement the Servlet interface. There are five methods defined in the Servlet interface, and the three most important methods involve the life cycle of the Servlet, which are the init() mentioned above, service (), destroy () method. GenericServlet is a generic, not specific to any protocol Servlet, which implements the Servlet interface. HttpServlet inherits from GenericServlet, so HttpServlet also implements the Servlet interface. So when we define Servlet, we only need to inherit HttpServlet.

3. The Servlet interface and GenericServlet are not specific to any protocol, while HttpServlet is a class specific to the HTTP protocol, so the service() method is implemented in HttpServlet, and the request ServletRequest and ServletResponse are forced to HttpRequest and HttpResponse.

2.4 Servlet life cycle

The servlet program is invoked by the web server, and the web server implements the management of the servlet life cycle. When your application loads and uses a Servlet, a series of events will occur from initialization to destruction of the Servlet. These events are called Servlet life cycle events (or methods).

The Servlet life cycle can be summarized as:

Servlet loading ---> instantiation ---> service ---> destruction

The Servlet life cycle defines how a Servlet is loaded, initialized, and how it receives requests, responds to requests, and provides services.

Enter a picture description

1. Create a Servlet instance 

The web container is responsible for loading the Servlet. When the web container starts or when the Servlet is used for the first time, the container will be responsible for creating the Servlet instance, but the user must specify the location of the Servlet through the deployment descriptor (web.xml) , which is the name of the class where the Servlet resides. After successful loading, the web container will instantiate the Servlet through reflection. 

2. The WEB container calls the Servlet's init() method to initialize the Servlet. 

After the Servlet is instantiated, the Servlet container will call the init() method to initialize the object, mainly to allow the Servlet object to complete some tasks before processing customer requests. Initialization work, for example, establishing a database connection, obtaining configuration information, etc. For each Servlet instance, the init() method can only be called once. The init() method has a parameter of type ServletConfig, and the Servlet container passes configuration information to the Servlet through this parameter. The Servlet uses the ServletConfig object to obtain initialization parameters provided in the form of name-value pairs from the configuration information of the Web application. In addition, in the Servlet, the ServletContext object describing the Servlet operating environment can also be obtained through the ServletConfig object. Using this object, the Servlet can communicate with its Servlet container. No matter how many clients access the servlet, init() will not be executed repeatedly. 

3. After the Servlet is initialized, it will always exist in the container, service() responds to the client request 

① If the client sends a GET request, the container calls the Servlet’s doGet method to process and respond to the request 

② If the client sends a POST request, the container calls the Servlet’s The doPost method processes and responds to the request 

③ or uniformly uses the service() method to process and respond to user requests

service () is the core of Servlet, responsible for responding to customer requests. Whenever a client requests an HttpServlet object, the object's Service() method will be called, and a "request" (ServletRequest) object and a "response" (ServletResponse) object will be passed to this method as parameters. Service() method already exists in HttpServlet. The default service function is to call the do function corresponding to the method of the HTTP request. It should be noted that before the service() method is called by the container, it must be ensured that the init() method is completed correctly. The container will construct a request object (type ServletRequest) representing the client request information and a response object (type ServletResponse) for responding to the client as parameters to the service() method. In the service () method, the Servlet object obtains the relevant information and request information of the client through the ServletRequest object, and after processing the request, calls the method of the ServletResponse object to set the response information. 

4. When the WEB container decides to destroy the Servlet, it first calls the destroy() method of the Servlet, and usually destroys the Servlet before closing the web application

destroy() is executed only once, and this method is executed when the server is stopped and the Servlet is unloaded. When the container detects that a servlet object should be removed from service, the container calls the object's destroy() method so that the servlet object can release the resources it uses and save data to persistent storage devices, for example, Save the data in memory to the database, close the connection to the database, etc. When the memory needs to be released or the container is closed, the container will call the destroy() method of the Servlet object. Before the Servlet container calls the destroy() method, if there are other threads executing in the service() method, the container will wait for these threads to finish executing or wait for the timeout value set by the server to arrive. Once the destroy() method of the Servlet object is called, the container will not send other requests to the object. If the Servlet is needed to serve the client again, the container will regenerate a Servlet object to handle the client's request. After the destroy() method is called, the container will release the Servlet object, and in the following time, the object will be recycled by Java's garbage collector. 

Note 

① Under normal circumstances, the Servlet will only be initialized once, and the processing service will be called multiple times, and the destruction will only be called once; but if a Servlet is not used for a long time, it will be automatically destroyed by the container, and if it needs to be used again The operation of initialization will be re-initialized when the time comes, that is, in special cases, the initialization may be performed multiple times, and the destruction may also be performed multiple times. 

② After the servlet instance is created, before the servlet can provide services for customer requests, the container will call the init() method on the servlet instance. If you have initialization code, you should override the init() method of the servlet class, otherwise the init() method of GenericServlet will be called. And for each client request (no matter who, whether it is the same person or not, only for the request), the container will create a new pair of request and response objects, and create a new thread/stack. There will never be multiple instances of any servlet class, except for one special case (SingleThreadModel).

③ The four cycles of the servlet life cycle are summarized as follows: 
a. The process of instantiating and loading servlet, new 

b. Initializing init (ServletConfig). 

c. To process the request, call the service, doget, and dopost methods of the servlet to pass the Request and Response as parameters. 

d. Exit the service and call the destroy method to release resources.

page display

1. Login page

Enter a picture description

2. Registration page 

Enter a picture description

3. Home page 

Enter a picture description

4. Display page

Enter a picture description

5. Add new page 

Enter a picture description

6. Delete page 

Enter a picture description

7. Modify the page 

Enter a picture description

Guess you like

Origin blog.csdn.net/tianqiquan/article/details/132339037