Home  >  Article  >  Java  >  What is the function of Servlet

What is the function of Servlet

(*-*)浩
(*-*)浩Original
2019-05-06 14:57:1410217browse

Servlet is a very complicated thing. In fact, servlet is a Java interface, interface! Open idea, ctrl shift n, search for servlet, and you can see that it is an interface with only 5 methods!

What is the servlet for?

It’s very simple, what is the role of the interface? Standardize it!

The servlet interface defines a set of specifications for processing network requests. All classes that implement servlet need to implement its five methods,

the most important of which are the two life cycle methods. init() and destroy(), and a service() that handles requests, that is to say,

All classes that implement the servlet interface, or in other words, all classes that want to handle network requests,

You need to answer these three questions:

What do you do when you initialize

What do you do when you destroy

You accept What to do when requesting

This is a specification given by Java! Just like Asimov's three laws of robotics and Rick's three questions in The Walking Dead, standards!

Servlet is a specification. Can a class that implements servlet handle requests?

The answer is, no.

You can Google a servlet hello world tutorial, and it will ask you to write a servlet. Believe me, you will never write any code in the servlet to listen to the 8080 port. The servlet will not directly communicate with the client. Deal!

How does the request come to the servlet?

The answer is a servlet container, such as our most commonly used tomcat. Similarly, you can Google a servlet hello world tutorial, which will definitely let you deploy the servlet into a container, otherwise your The servlet won't work at all.

Tomcat is the guy who directly deals with the client. He listens to the port. After the request comes, based on the URL and other information, it determines which servlet to hand over the request to handle, and then calls the service method of that servlet. The service method returns a response object, and tomcat returns this response to the client.

The above is the detailed content of What is the function of Servlet. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn