In my impression, the server should be a cold big iron cabinet, waiting in the computer room with constant temperature and humidity, so what is Tomcat? Why is it also called server?

Learn skills Tomcat

Tomcat, a familiar stranger. All of our projects run on it, and we tend to ignore it. In reality, too, there is air all around us, we breathe it all the time, but you never care about it. Similarly, Tomcat is only something we look at when we create an environment or run a project and there are errors.

On the other side of the ocean in the 1990s, a company called SUN created a new language called Java. After just a few years of development, it has become the hottest language on the market. Then I realized the “Java13 Stunts”, which is the so-called JavaEE specification: JDBC, JNDI, EJB, RMI, JSP, Servlets, XML, JMS, Java IDL, JTS, JTA, JavaMail, JAF.

On the other side of the ocean there is another company, an organization to be exact, that specializes in open source, called Apache. The company came up with a server called Tomcat. That’s a great name. In China, there is a term called “three-legged cat”, which is used to make fun of people who are incapable of kung fu. As it happens, Tomcat doesn’t fully implement the JavaEE specification either. Tomcat implements only two of the 13 core technologies: Servlet and JSP. Other servers like JBoss, Weblogic, etc are fully supported. As a result, people tend to call Tomcat a lightweight server, but also a Servlet/JSP container.

When you hear this, you can’t help Shouting: no, I remember writing a program that uses JDBC and can run!

Ah, that’s because you have access to JDBC packages… But have you installed the Servlet/JSP package after Tomcat? No! Somebody else implemented the Servlet/JSP specification, all the whole to their own source code.

When I say that, I’m in tears, too. Because I realized I’m a three-legged cat, too. I have also learned JDBC/XML/JSP/Servlet… So I prefer to call myself a JavaWeb programmer rather than a JavaEE programmer. JavaEE is actually very heavy. I can’t carry it. Excuse me.

Why do we need a server?

In my opinion, there are two essential functions of a server:

  • Expose your resources
  • Coordinate with various transmission protocols for response output

Suppose I had a question:

Given two computers, how can you transfer a picture from one computer to the other without using Bluetooth /QQ/ wechat, or a removable device such as a web disk or USB as an intermediary?

Hearing this question, I estimate that most of the non-class friends will be confused. Because if we did not deliberately learn computer network in the later period, our understanding of the network was basically limited to the 4-hour “network programming” explanation in the basic class. And we usually too used to address bar type www.baidu.com, brainless a car on the Internet surfing. Now suddenly you have to access the computer next door, it is a little helpless.

There are several possible ways to solve this problem, but here’s one: access through the server.

Understand the following three concepts:

  • IP: Unique identifier of an electronic device (computer) on a network. An IP address corresponds to a physical computer
  • Port: Unique identification of an application program on a computer. A port can only be occupied by a unique program
  • Transport protocol: Rules for data transmission

Each of China’s 1.4 billion people has a unique identification card: an identity card that pinpoints an individual. Similarly, there are billions of computers on the network, and each computer has its own unique set of IP addresses (which can be the same across different lans), meaning that an IP represents a specific physical computer. For example, in Tang Pak Hu Lights Autumn Heang, Hua an’s number is 9527, while the butler in Washington never calls him by his name, but directly calls him “9527”. Because 9527 is Hua ‘an.

We can pinpoint a computer based on its IP, but it’s not enough to give us access to that computer. It’s like you know my house number, but I didn’t open the door. The so-called door is a port, and behind the port is the application.

In general, one port can locate one piece of software, but one piece of software can occupy multiple ports (the door of your home, which only belongs to your home, but can have front and back doors). In real life, if your door is occupied or blocked, guests can’t get in. In a program, if two programs have the same port, there will be a port conflict, which is called “port occupation.” The consequence of a port occupation is often that a program cannot start, let alone run.

About ports, another example: wechat and QQ are both owned by Tencent, and you have both installed on your computer. Why I use QQ to send you a message, your wechat can not receive? What seems silly to normal people is not so simple.

Each application has its own port number (possibly multiple) that it listens for once it runs. Each program is a prisoner of the computer, unable to see the outside world, and the port is the window to deliver food to these prisoners. Applications spend their days hiding in their computers staring at their ports, begging for a giant drumstick (Request) when another computer comes by.

In fact, QQ software belongs to C/S architecture, which has shielded too much of the bottom layer for us. What IP and port are all automatically encapsulated. The B/S architecture is more intuitive. For example, using a browser to visit Baidu:

To access a server, you must know its IP. But we humans aren’t very good at remembering long numbers, so we developed so-called domain names to point to IP. But the actual request, or eventually converted into IP access. In general, there are two ways of conversion:

  • Hosts file on the host
  • The DNS server

I don’t know if any careful friends noticed the details below:

Even after the DNS resolves the domain name to obtain the corresponding IP address, the Request will still contain host. Why is that?

Because: domain name! = IP.

In fact, one IP address can correspond to multiple domain names. That is to say, a physical server (iron cabinet), in theory can have multiple domain names (virtual host). Physical servers and websites are two different things. IP addresses correspond only to physical servers, while domain names correspond to specific websites. For example, although the IP address 115.239.210.27 of baidu server is completely equivalent to.baidu.com, it is possible that the server corresponding to this IP address is configured with two virtual hosts: www.baidu.com and tieba.baidu.com. Therefore, even if the server entity corresponding to the IP is found, the Request should still be accompanied by the host name to determine which virtual host it is.

In addition, if two domain names correspond to the same IP address, then one of the domain names must be set as the default, otherwise the same server has two virtual hosts, who should I visit?

Finally, take Tomcat as an example. For example, I now have a laptop (a physical server) with a native IP of 192.168.112.1 on which I have Tomcat installed. If Tomcat is not modified, there is only one virtual host localhost (default host) by default. I then developed a JavaWeb application demo1 to deploy to Tomcat, and my colleague typed the following address into the browser:

192.168.112.1:8080 / not/index. HTMLCopy the code

Access my computer. There is no host, but localhost is the default, so access it.

However, the Baidu server above is just an example. Baidu Search and Baidu Tieba actually have different IP, which means they are not on the same server. Generally speaking, an IP corresponds to a server, there is only one host on the server, get the IP can basically determine which website to visit.

Three confusing little concepts

We often talk about “server” and “server”. In fact, “server” is a very ambiguous concept. I can think of three points:

  • Software – concept server and hardware – concept server
  • Web server? The Web container?
  • The Web applications we develop are half-baked!

Software – concept server and hardware – concept server

In software terms, as long as a computer is properly configured, equipped with an operating system, connected to the Internet with electricity, and installed with specific software, it can be called a server. For example, if you want to learn about databases, and you install MySQL server, then your computer is a MySQL server. Then you install SVN server, so your computer is both MySQL server and SVN server. The same is true for the Tomcat server.

In terms of hardware concept, the server is also a computer in essence, but with high configuration and ugly appearance, it is basically a cold big iron cabinet. Our laptops can watch movies and play games, and they’re basically dedicated to airplanes.

Web server? The Web container?

Actually, Tomcat server = Web server + Servlet/JSP container (Web container).

The function of a Web server is to receive requests from clients and respond to them. But obviously, there is more to a server than static resources, so if a client requests a dynamic resource, the Web server cannot simply respond to it (such as a JSP) because the browser only knows static resources. So for JavaWeb program, also need JSP/Servlet container, JSP/Servlet container basic function is to transform dynamic resources into static resources. We JavaWeb engineers need to work with aWeb server and JSP/Servlet container, and usually the two come together, such as Tomcat.

The Web server receives and responds to client requests, and the Web container loads servlets/JSPS to handle dynamic resources:

So just now we draw baidu server, in fact, the details can be more abundant:

The Web applications we develop are half-baked!

When we write code, we all know that it’s best to extract the same code into a common method and reuse it. Now, what do hundreds of thousands of Web applications have in common?

First, the resources must be different and cannot be extracted. For example, Youku focuses on videos, while Zhihu is basically text.

Secondly, the business is certainly different, for example, Baidu is mainly search, Taobao is e-commerce. But one thing is the same: these sites need to “receive user requests” + “respond to user requests”.

Huh? Bridge more sacks!! These two concepts, as if I have not seen! Yes, the Web server above. Think back, when we were developing JavaWeb, did you worry about receiving and responding to HTTP requests? Apparently not! Because you’ve been too busy debugging. So the Web application we develop in Java is a work-in-progress, like a plug-in, and the server is like a transceiver:

What are dynamic resources?

In fact, I don’t have a precise idea of what a dynamic resource is. It is sometimes difficult to tell what a thing is. Let’s start by saying what it isn’t.

Dynamic resources are not the same as dynamic pages. The so-called dynamic page, is the page will move, and the moving page is not necessarily a dynamic resource. For example, I can use JQuery to execute code that makes a Div zoom in and out, but it’s clearly still an HTML page.

Dynamic resources, in fact, the most significant feature is that it can dynamically generate HTML! For example, the JSP. Dynamic resources have a “special feature” : their data is “collectable” and “can change over time”. Here is an example of the sina server that claims to be able to resist the simultaneous cheating of 8 stars:

Suddenly, the column of Sina’s Playworld found out that it was Tong Liya, not Liu Yifei, who had an affair with Bravo1988.

At this point, fans open the Column again, see is: Tong Liya drunk bravo late at night. The above example nicely illustrates two features of dynamic resources (JSP) :

  • ${name}” Drunk at night bravo”
  • Change with time: Liu Yifei → Tong Liya

So why is HTML static? I can also modify the HTML page to make it change! Very good, very thoughtful. So please xiaobian first learn Linux, and then remote access to the server into the Tomcat directory to modify it.

Both dynamic and static resources reside on the server, but dynamic resources contain variables (” assemblable “features) that maintain the connection between the database and the program.

If a JSP is an electronic billboard, a variable is a wire that connects to a computer (a database server). So as long as the computer re-edit the text, the content of the billboard will change, this is called dynamic. A static resource is like a notice. It is what it is written at the beginning, and it will not change any more.

Implementing “Tomcat”

Finally, there is a boring question to ponder: in the JavaSE phase, whatever we do, we start by hitting main() first. After learning JavaWeb, how long has it been since you typed main()? Where did she go?

Implement Tomcat (online a good public class video) : www.bilibili.com/video/BV1AW…

I’m bravo1988. See you next time.

Scot ro si · Feeling the mezzo