1.1 interface Servlet
Today we are starting a new and interesting topic - servlets . It was the addition of servlets to Java that led to Java being the de facto standard for large server applications. 80% of all enterprise software in the world is written in Java. And in China, everything is 100%. So what are servlets?
A servlet is exactly what turns a Java program into a web service and allows it to process requests from clients. And it was like this...
In the 90s, immediately after the advent of the World Wide Web, web clients (browsers) and web servers appeared. Web servers usually simply distributed the file they had stored over the Internet: html pages, scripts, pictures, etc.
At some point, everyone came to the conclusion that it would be necessary to make both sides smarter. JavaScript was added to the HTML pages, and plugins were added to the servers - special scripts that were called in response to certain requests and made the server's behavior more flexible and smarter.
So a servlet is such a Java plugin that was built into Java web-server
and allowed it to execute Java code when requested for certain pages. And already this Java code, represented by a class inherited from the Servlet class, did what its developers intended.
And as you already know, the most popular Java web server is Tomcat . Named, by the way, in honor of the cat Tom from the cartoon “Tom and Jerry”.
How does Tomcat interact with servlets? In fact, this process is standardized and is called the servlet life cycle . In it, a servlet is a loadable object, and a web server is a servlet container .
If the servlet has not yet been loaded , then:
- The servlet class is loaded by the container.
- The container creates an instance of the class (object) of the servlet.
- The container calls a method
init()
on the servlet object. The method is called only once.
Standard work cycle - servicing a client request :
- Each request is processed in a separate thread.
- The container calls a method
service()
on the servlet and passes the ServletRequest and ServletResponse objects there. - To terminate the servlet, a method is called
destroy()
on the servlet object. It is called only once.
There can be many reasons why a servlet terminates:
- The programmer restarts the web server, it is necessary to gracefully shut down all servlets.
- The programmer loads a new version of the servlet, the old one must be unloaded correctly.
- And so on.
Remember the main thing: the web server and its servlets should work without failures and restarts for months, serving thousands of requests per minute. Therefore, the code for both loading, and working, and unloading a servlet should always be written very high quality.
1.2 HttpServlet class
The Servlet class exists to standardize how a servlet and a container work. Programmers do not work directly with this class. Well, they rarely work. The most commonly used class HttpServlet
is inherited from Servlet.
This class has several methods that will be useful to us. You will often use them:
Method | Description | |
---|---|---|
1 | init() |
Called once when the servlet is loaded |
2 | destroy() |
Called once when the servlet is unloaded |
3 | service(HttpRequest, HttpResponse) |
Called for every new request to the servlet |
4 | doGet(HttpRequest, HttpResponse) |
Called for every new GET request to the servlet |
5 | doPost(HttpRequest, HttpResponse) |
Called for every new POST request to the servlet |
6 | doHead(HttpRequest, HttpResponse) |
Called for every new HEAD request to the servlet |
7 | doDelete(HttpRequest, HttpResponse) |
Called for every new DELETE request to the servlet |
8 | doPut(HttpRequest, HttpResponse) |
Called for every new PUT request to the servlet |
init()
The and methods destroy()
are inherited from the Servlet class. Therefore, if you decide to override them in your servlet, you will also need to call their implementation from the base class. The command is used for this super.method name()
.
Servlet example:
public class FirstHttpServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Getting the parameter “secret” from request
String secret = request.getParameter("secret");
// Put parameter “secret” into Http-session
HttpSession session = request.getSession(true);
session.setAttribute("secret", secret);
// Print HTML as response for browser
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Header</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet example "+ secret +"</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
}
1.3 service(HttpServletRequest, HttpServletResponse) method
If you look at the processing of a client request from the point of view of a servlet, then things are something like this.
For each client request, the container (web server) creates HttpServletRequest
and objects HttpServletResponse
, and then calls a method service(HttpServletRequest request, HttpServletResponse response)
on the corresponding servlet. These objects are passed to it so that the method can take the necessary data from request
and put the result of the work into response
.
The method service()
has a default implementation. If it is not redefined, then it will be executed. That's what he does.
The method service()
determines the HTTP method type from request (GET, POST, ...) and calls the method corresponding to the request.
Method | Description | |
---|---|---|
1 | service(HttpRequest, HttpResponse) |
Called for every new request to the servlet |
2 | doGet(HttpRequest, HttpResponse) |
Called for every new GET request to the servlet |
3 | doPost(HttpRequest, HttpResponse) |
Called for every new POST request to the servlet |
4 | doHead(HttpRequest, HttpResponse) |
Called for every new HEAD request to the servlet |
5 | doDelete(HttpRequest, HttpResponse) |
Called for every new DELETE request to the servlet |
6 | doPut(HttpRequest, HttpResponse) |
Called for every new PUT request to the servlet |
In your class, you can either redefine one method service()
, or leave it alone, but then redefine methods doGet()
, doPost()
, ... as needed.
GO TO FULL VERSION