Overview
This javaweb basic study notes are based on the notes summary of the 2022 javaweb tutorial released by Shang Silicon Valley. New content will be added later on this basis. Personal learning experience believes that after learning Javase and database, watching this video to learn JavaWeb is mostly understandable. However, the learning content of JavaWeb is indeed relatively broad and detailed, so if you want to deeply master it, you need to consolidate and summarize the learning content and practice it. It better reflects the current level of mastery.
Video address: javaweb tutorial
Servlet introduction
servlet=serve+applet
serve: server
applet: applet
means a small program on the server side. In web applications, servlet is mainly responsible for processing requests and coordinating scheduling functions. It is the controller in web applications.
Specific implementation details:
In the entire Web application, Servlet is mainly responsible for processing requests and coordinating scheduling functions. We can call Servlet the " controller " in Web applications
Learn about servlets in practice
First, the client requests the add.html page from the server, and the server returns the corresponding page and displays it on the browser. After displaying, click Submit Request in the page to send the request to the server. The server-side Servlets will process the request after recognizing the request. deal with.
1: Create the add.html page.
The following figure shows the created html page. The form tag represents the form. When the user clicks Add to submit, the form data will be sent to the server. Action refers to the request to be sent. The Servlet in the server determines whether it is the request to be processed based on the request name. Method refers to the method to send the request. Requests sent with the post method are all form-structured data. The differences between the various methods will be introduced later.
2: To establish the HttpServlet class,
you must first create the httpServlet class, and implement this class to process incoming requests. Since this class belongs to an abstract class, you can just inherit it. The specific implementation is as follows.
Since I am using the post method to send requests, we need to rewrite the dopost method in the newly created AddServlet class to process the corresponding data. The first formal parameter in this method is the request sent by the client. The request has been encapsulated into an object for operation. The data of the specified name parameter can be obtained through one of its getparameter() methods.
3. Establish a mapping relationship.
When the user sends a request with action=add, the server actually does not know which Servlet class should handle the request, so it needs to add a mapping relationship. The relationship needs to be added in web.xml, as follows:
The above logical relationship is:
1: The user sends a request: action=add
2: Find /add in the url-pattern and determine the Servlet-name to which it is mapped
3: Find the servlet with the same name in the Servlet tag
4: Find its corresponding class.
5: The user sends a post request (method=post), so tomcat will execute the dopost method in the servlet class.
4.Result display
5. Problem:
When the added data is English characters or numbers, the console can display the correct data, but when adding Chinese characters, garbled characters will appear, causing the data to be added incorrectly. In this case, the encoding form must be set to prevent Chinese garbled characters.
Solution:
1: In post mode, set the encoding to prevent Chinese garbled characters.
2: In Get mode, there is no need to set the encoding for tomcat8 and above, but for the following, you need to set the encoding.
Note: Both methods of setting encoding must be executed before all methods of obtaining parameters.