Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a Servlet container

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "what is a Servlet container". In the operation of practical cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, the browser initiates a HTTP request, like in the early days, only some static resources are requested, which requires a server to process the HTTP request and return the corresponding static resources.

This server is called HTTP server.

To put it simply, parse the request, and then know which folder and which name of the static file above the server, and find and return it.

With the development of the Internet, interaction is becoming more and more important, and simple static files can not meet the needs.

The business becomes complex and requires us to write code to handle a lot of business.

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

It is impossible to determine which business class needs to be called in the specific implementation of the HTTP server, can it?

This makes non-business and business strongly related.

So we need to do a layer of abstraction to separate the parsing of HTTP from the specific business.

The essential requirement is to find the corresponding business implementation class according to the HTTP request and then execute the logic before returning.

And the business is tens of thousands, so it is necessary to specify an interface, so the business classes all implement this interface in order to be easy to dock.

This is what an interface means, just like USB.

This interface is Servlet, which is, of course, the narrowest interpretation.

Servlet is actually Server Applet, the full name Java Servlet, refers to the server program written in Java.

It actually refers to the business classes that implement the Servlet interface.

This is the origin of Servlet.

The Servlet container is actually used to manage and load these Servlet classes. Find the corresponding Servlet class according to the HTTP request, which is what the Servlet container does.

Do you think you can smoke another layer when you see this? Because it seems to have nothing to do with the specific business implementation?

Yes, one more floor can be drawn.

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

The corresponding relationship between the request and which Servlet is also abstracted, which is web.xml. We can tell the Servlet container the corresponding relationship in the configuration.

The business implementation in my diagram actually corresponds to our usual war package, which is the decoupling of the business and the Servlet container.

You must have heard of the Servlet specification. In fact, the whole set of Servlet interfaces and Servlet containers, including directory naming, is called the Servlet specification.

All the related middleware is implemented according to the Servlet specification, and we also implement the business code according to the Servlet specification, so we can choose different Web middleware in different scenarios.

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

At this point, the HTTP server, Servlet, and Servlet containers must be clear.

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

So Tomcat, Jetty and other implementations include the functions of HTTP server and Servlet container, which is called Web container.

From our analysis layer by layer of stripping, layer by layer of abstraction, I believe you have a better understanding of Web, I draw a Tomcat analysis map, should be very clear.

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

First, the HTTP service is abstracted and used to communicate and parse the protocol.

Because of the complexity of the business, another layer of Servlet is abstracted in order not to couple with HTTP services.

The Servlet is loaded and managed by Servlet to control the forwarding of requests to the specified Servlet implementation class.

Then we can develop the business with peace of mind.

Because of abstraction, it is flexible and easy to extend. For example, it is now a HTTP1.1 service, which can be replaced with HTTP 2.

Now use Tomcat as the Servlet container, or you can replace it with Jetty.

Now you can use the native implementation Servlet to do business, or you can replace it with SpringMVC.

Change at will, because it's all abstracted, it's easy to replace, as long as you follow the agreed interface implementation.

A routine of frame design

After reading the architectural design routines, let's talk about the framework routines.

Interfaces and abstract classes.

All the necessary routines for middleware design, and of course our own code will do the same.

First define an interface to agree on some actions, what can be done.

Then an abstract class is defined to implement this interface, which is used to implement some general logic and reuse the code.

Then make some commonly used implementation classes inherit abstract classes to facilitate the use of developers.

The rest is left to developers to expand on their own.

Then the abstract class uses the template method, that is, to define the process of execution, and the specific implementation logic is implemented by the subclass itself.

This is the necessary routine.

Interface constraints, code reuse of abstract classes, easy to use common implementation classes, and self-expansion of the rest.

Take Servlet 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 ();}

Then I set up a general abstract class GenericServlet, but this abstract class is relatively simple in 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. }

And then made a common HttpServlet to inherit 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"

.

}

`

The routine is such a routine, and then the interviewer asks you questions about interfaces and abstractions, and I'm sure you can answer them.

This is the end of what is the Servlet Container. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report