What is Tomcat?

Tomcat is the Web container. When you do a web project, most of you need HTTP protocol, which is based on request and response, for example, if you enter a line of content to search baidu, how does baidu server handle this request? It needs to create servlets to handle this, servlets are actually Java programs. Just on the server side, Java servlet intercepts your request through a configuration file, processes it, and shows you the interface. How do you create a servlet?

This is where Tomcat comes in. It’s the thing that helps you create servlets, so it’s also called the Web container, and you can’t run web projects without it. There are many corresponding Web containers, such as JBOSS,WEBLOGIC, etc.

Tomcat is an application server. It can run Java programs that you have written according to the Servlet specification in J2EE.

Simply put, it is a Web site running container, put your written site into it can run.

To share this set of Tomcat video tutorial, novice can see, explain very detailed.

www.bilibili.com/video/BV1yx…

Information download:

www.bjpowernode.com/javavideo/1…

Network protocol/Network protocol package

In Internet communication, information transmitted over the network is binary.

The recipient must parse the message once it is received.

However, due to the relatively large amount of information transmitted, it is more difficult for the receiver to parse

Browser: http://localhost:8080/myWeb/index.html

1010101010101010101011111000110101010

Network protocol Package:

A regular set of binary data in fixed space. Stores specified data in a specified space to reduce the difficulty of receiving data

0000 Server COMPUTER IP address 0000 Http server port number 0000 Resource file

Two, Http network protocol package:

In the B/S communication structure, the browser sends the request information and the server returns the resource file content through Http network protocol package

Http network protocol package classification:

1.Http request protocol package:

1) Be responsible for generating an Http request protocol package when the browser is ready to send the request

2) When the browser is ready to send the request, it is responsible for writing the request information into the Http request protocol package

3) The browser is responsible for pushing the Http request protocol package

2.Http response protocol package:

1) Be responsible for generating an Http response protocol package when the Http server is ready to return results

2) The Http server invokes the output stream to write the file contents (text, numbers, pixels, frames, commands) in binary form to the Http response protocol package

3) The Http server is responsible for pushing the Http response protocol packet back to the browser that initiated the request

Iv. Internal structure of Http request protocol package: Divided into 4 layers from top to bottom [Back]

  1. Request:
【 URL: request address method: request method 】Copy the code

2. Request header

If the browser sends a request in GET mode, the request parameters are stored in the request header.Copy the code

3. Blank line:

【

】
Copy the code

4. Request body:

If the browser sends a request in POST mode, the request parameters are stored in the request body.Copy the code

Internal structure of Http response protocol package is divided into 4 layers from top to bottom.

  1. The status line:
[Http status code]Copy the code
  1. Header:
Content-type: specifies that the browser uses the corresponding parser or compiler to parse or compile the returned binary data.Copy the code
  1. Blank lines:
【

】
Copy the code

4. Response body:

[Contents of documents required this time (stored in binary form)]Copy the code

Introduction to Http server

1. All Http servers develop applications in the Java programming language

2. The Http server can receive Http request packets initiated by the browser

3. The Http server can parse the binary data of the Http request packet to understand user requests

4. The Http server can automatically locate the accessed resource file

5. The Http server automatically writes the accessed file content or file running result to the response body of the Http response protocol package in binary form through the output stream

6. The Http server can automatically push the Http response packet back to the browser that initiates the request

Http server classification:

1. Most powerful –Websphere

2. Latest features –GlassFish

3. The lightest — Tomcat

8. Tomcat server installation and configuration

1. Download the Tomcat server from tomcat.apache.org/

2. Tomcat installation: Tomcat 9.0 is a green installation-free version

3. The tomcat configuration:

1) JAVA_HOME environment variable: Used to provide the Tomcat server with the location of the JDK on the current computer

JAVA_HOME = C: \ Program Files \ Java \ jdk1.8.0 _101

2) Environment variable CATALINA_HOME: informs the location of the Tomcat server

CATALINA_HOME=C:\tomcat9

9. Tomcat server startup and shutdown

1. A managed object is generated when the Tomcat server is started. If manually closed, a long time.

The managed object will go out of administration, and tomcat cannot be started again

2. Location of management commands: Tomcat installation address /bin folder

3. Run the startup command to start tomcat

4. Run shutdown to shutdown tomcat

Causes of tomcat server startup Failure

1. Forget to configure JAVA_HOME

Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

At least one of these environment variable is needed to run this program

2. The JAVA_HOME address is incorrect

The JRE_HOME environment variable is not defined correctly

This environment variable is needed to run this program

CATALINA_HOME address is incorrect

The CATALINA_HOME environment variable is not defined correctly

This environment variable is needed to run this program

4. The default port number of the Tomcat server is 8080

java.net.BindException: Address already in use: JVM_Bind

11. Tomcat server working file

Bin: tomcat management command

Conf :tomcat configuration file. Server. XML is the Tomcat core configuration file

3. Lib: Third-party JAR that the Tomcat application depends on at runtime

4. Logs: Stores Tomcat run logs

5. Temp: Stores temporary files generated during tomcat running

6. Webapps: By default, Tomcat locates resource files in Webapps after receiving the request

7. Work: Tomcat, by default, edits JSP files to Java files in the work file and compiles them to class files

The first BS communication simulation

1. Create a website under Webapps

Web site: a web resource site that stores resource files that can be accessed by browsers

2. Start tomcat

3. Start the browser and send a request to Tomcat

Enter the resource file location command (URL) in the address bar:

http://server COMPUTER IP address: HTTP server port number/website name/resource name. suffix

http://localhost:8080/myWeb/one.html

13. Internal structure of standard website

|

| _________src folder: Java files and dynamic resource files

|

| _________web folder: static resource files/third jar/site core configuration file

|

| ________WEB – INF: third jar/site core configuration file

|

Third jar | ______lib folder (manual)

|

| ______web. XML web core configuration file

|

| alarm static resource files

Simulate tomcat calling dynamic resource files

1. For a site, Tomcat can only call the Servlet interface implementation class in the site

2. After receiving the request, Tomcat locates the accessed Servlet interface implementation class according to the file name in the URL.

1) Tomcat uses the class. forName method to load the hard disk Class files into memory

2) Tomcat tells the JVM to create instance objects of the current class through the invert mechanism

3) Tomcat initializes instance objects by calling init method on instance objects

4) Tomcat calls the Service method through the instance object to process the current user request

5) Tomcat writes the result of the service method processing to the response body through the output stream and returns it to the requesting browser

15. Adapter design mode

1. Function: reduce the difficulty of interface implementation class development. An interface implementation class can be given the freedom to override methods in the interface

2. Solutions:

1) Abstract classes are used to implement interfaces, and abstract methods are not welcome in the docking port for empty implementation

2) Create interface implementation class inheritance and abstract class, at this time interface implementation class development as long as rewrite the interested method

16. Template design mode:

1. Purpose: Subclasses only need to consider the related method rewrite implementation, and do not need to consider method call scenarios. Subclass method invocation rules are controlled by the parent class