This article has been included at github.com/yessimida/y… Here is a summary of all my articles. Welcome star!

The following code I believe you are familiar with, college learning Java Web have written such code.

For a long time after I first encountered servlets, I didn’t understand what a Servlet was.

Why servlets?

Why a Servlet container?

What is a Web container, HTTP server?

Today, let’s take a look at the architecture and framework design routines.

After reading, you may have a further understanding of interfaces and abstractions.

Come on, get in!

The body of the

First, the browser initiates an HTTP request, like in the early days when only static resources were requested, requiring a server to process the HTTP request and return the corresponding static resources.

This server is called the HTTP server.

Simply put is to parse the request, and then find the server on which folder under which name of the static file, find and return.

With the development of the Internet, interaction becomes more and more important, and a static file simply cannot meet the demand.

The business became complex, requiring us to write code to handle a lot of business.

Different business logic needs to be invoked in response to the HTTP request, but our business code cannot be coupled to the HTTP server.

You can’t decide which business class needs to be called in the HTTP server implementation, can you?

This makes a strong correlation between non-business and business.

So a layer of abstraction is required to separate HTTP parsing from the concrete business.

Essentially, the requirement is to find the corresponding business implementation class based on the HTTP request and then execute the logic and return.

And the business is tens of thousands, so need to specify an interface, so business classes implement this interface so that easy docking.

That’s what interfaces are, like USB.

This interface is a Servlet, in the narrowest sense of the word.

Servlets are Server applets, or Java Servlets, which refer to server-side programs written in Java.

Instead, they refer to the business classes that implement the Servlet interface.

That’s where servlets come in.

And the Servlet container actually manages and loads these Servlet classes, and then gets the HTTP request and finds the corresponding Servlet class and that’s what the Servlet container does.

Does this make you feel like you can pull another layer? Because it doesn’t seem to have anything to do with the specific business implementation, right?

Yes, there’s still room for another layer.

There is no need to couple what the Servlet container does to a specific business, which is implemented as a Servlet interface anyway, so that the Servlet container can load it and manage it.

We can also abstract the relationship between the request and the Servlet, which is called web.xml. We can tell the Servlet container the relationship in the configuration.

The business implementation in my diagram corresponds to our normal WAR package, which is the decoupling of the business from the Servlet container.

You’ve probably heard of the Servlet specification, but the whole Servlet interface and Servlet container set, including directory naming and all, is called the Servlet specification.

All the relevant middleware was implemented according to the Servlet specification, and we implemented the business code according to the Servlet specification so that we could choose different Web middleware for different scenarios.

Anyway, the purpose of the specification is to facilitate docking and reduce docking costs.

The HTTP server, servlets, and Servlet containers should now be clear.

The Web container is actually HTTP server + Servlet container, because the Servlet container alone does not parse HTTP requests, communication, and other related functions.

Therefore, the implementation of Tomcat, Jetty and other functions including HTTP server and Servlet container are called Web container.

From our analysis layer by layer, layer by layer of abstraction, I believe you have a further understanding of the Web, LET me draw a Tomcat analysis diagram, should be very clear.

From the above step by step analysis, we can see that the design of an architecture is a series of related abstractions.

First, HTTP services are abstracted to communicate and parse protocols.

Because of the complexity of the business, another layer of Servlet is abstracted to decouple from the HTTP service.

The Servlet loads and manages the Servlet to control the forwarding of requests to the specified Servlet implementation class.

Then we can focus on developing the business.

Because of the abstraction, it is flexible and extensible. For example, an HTTP1.1 service can be replaced with HTTP 2.

Tomcat is now used as the Servlet container, or Jetty can be used instead.

We now use native implementation servlets to do business, or we can switch to SpringMVC.

Feel free to change it, because it’s all abstracted out, it’s easy to replace, as long as you follow the convention of the interface implementation.

Frame design of a routine

After looking at the pattern of architecture design, let’s talk about the pattern of framework.

Interfaces and abstract classes.

This is a must for all middleware design, as well as our own code.

Start by defining an interface to agree on some actions that you can do.

Then define an abstract class to implement this interface, which is used to implement some general logic, so as to achieve code reuse.

Then make some common implementation classes inherit abstract classes, convenient for developers to use.

The rest is up to developers to expand.

The abstract classes then use the template method, which defines the process to execute, and the implementation logic is left to the subclasses themselves.

This is the way to go.

Interface constraints, abstract class code reuse, implementation of common implementation classes easy to use, the rest of their own extension.

Taking servlets as an example, first define the Servlet interface.

public interface Servlet { void init(ServletConfig config) throws ServletException; ServletConfig getServletConfig(); Void service(ServletRequest req, ServletResponse Res) throws ServletException, IOException; String getServletInfo(); void destroy(); }Copy the code

Then I made a generic abstract class, GenericServlet, but this abstract class has simpler logic.

public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable { ................ Omit some............. @Override public ServletConfig getServletConfig() { return config; } @Override public ServletContext getServletContext() { return getServletConfig().getServletContext(); } @Override public void init(ServletConfig config) throws ServletException { this.config = config; this.init(); }... Omit some..................... }Copy the code

Then we made a common HttpServlet that inherits GenericServlet.

public abstract class HttpServlet extends GenericServlet { private static final long serialVersionUID = 1L; private static final String METHOD_DELETE = "DELETE"; private static final String METHOD_HEAD = "HEAD"; private static final String METHOD_GET = "GET"; . }Copy the code

Then the interviewer will ask you questions about interfaces and abstract classes, and you will be able to answer them.

The last

I think you all got it.

You’ve all heard that every problem in computer science can be solved with a layer of indirection.

Yes, almost any problem can be solved at one level of abstraction.

If one layer is not enough, then two.

Welcome to add my friends for in-depth communication, note “into the group”, pull you into the exchange & push group.

The interview questions on weekdays encounter difficulties, or look at a knowledge point to turn over the data of the whole network or feel very vague, not thorough, can private chat with me, give me a message.

I will arrange and write an article if I meet the right one. I will not ask others to complete it.

Don’t ask your boss about detailed situations at work 🙂

Shoulders of giants

In-depth Dismantling of Tomcat & Jetty li double


Wechat search “yes training level guide” full of dry goods, or to pinch me, reply [123] a 20W word algorithm brush notes waiting for you to get. More articles can see my article summary: github.com/yessimida/y… Welcome to star!