Servlet basic tutorial

Read(15528) update time(2022-04-14)

Servlet Tutorial Servlets provide a component-based, platform-independent approach to creating web-based applications without the performance limitations of CGI programs. Servlets have access to all Java APIs, including the JDBC API to enterprise databases. This tutorial explains how to use Java Servlets to develop web-based applications. Start learning Servlets now!


Servlet (Server Applet), full name Java Servlet, has no Chinese translation. It is a server-side program written in Java. Its main function is to browse and modify data interactively and generate dynamic Web content. Servlet in the narrow sense refers to an interface implemented by the Java language, and Servlet in the broad sense refers to any class that implements this Servlet interface. Generally, people understand Servlet as the latter.

Servlet runs in an application server that supports Java. From an implementation perspective, Servlets can respond to any type of request, but in most cases Servlets are only used to extend Web servers based on the HTTP protocol.

The first to support the Servlet standard was JavaSoft's Java Web Server. Since then, some other Java-based Web servers have begun to support standard Servlets.

Tips: Before continuing to learn Servlet, you need to have a certain understanding of Java knowledge.

Servlet working mode

  • The client sends a request to the server

  • The server starts and calls the Servlet, and the Servlet The client requests to generate response content and transmits it to the server

  • The server returns the response to the client

  • Others

Tips: Our Servlet tutorial will help you learn Servlet knowledge from beginner to advanced. If you have any questions, please go to the PHP Chinese website Servlet Community to ask your question, and enthusiastic netizens will answer it for you.

Life cycle

When the servlet is deployed in the application server (the part of the application server used to manage Java components is abstracted into a container), the container controls the life cycle of the servlet . Unless otherwise specified, the servlet will not be loaded when the container is started. The servlet will only be loaded and instantiated on the first request.

Once a servlet is loaded, it is generally not deleted from the container until the application server is shut down or restarted. But when the container performs memory recycling actions, the servlet may be deleted. It is for this reason that the time spent on the first access to a servlet is much longer than the time spent on subsequent accesses.

The server's running life cycle is that the servlet is loaded and executed an initialization method at the first request (or its entity is accessed after memory garbage collection), followed by the formal running method, and then It is resident and directly executes the formal run method every time it is requested, until the entity is destroyed after executing the destruction method once when the server is shut down or being cleaned up.

The relationship between servlet and JSP

Java Server Page (JSP) is an extension of HttpServlet. Since HttpServlet is mostly used to respond to HTTP requests and return Web pages (such as HTML, XML), it is inevitable that a large amount of HTML content will be involved when writing servlets, which greatly affects the writing efficiency and readability of servlets. Big obstacle, JSP was born on this basis.

Its function is to use the HTML writing format and add Java code snippets at appropriate places to free programmers from complex HTML and focus more on the content of the servlet itself.

JSP is converted into a servlet by the application server when it is accessed for the first time. In subsequent runs, the container directly calls this servlet without accessing the JSP page. The essence of JSP is still servlet.

Content covered by this servlet tutorial manual

This servlet tutorial covers all basic and advanced knowledge of servlets, including servlet environment settings, servlet life cycle, servlet form data, servlet exception handling, and servlet cookies Processing, session tracking, servlet database access, etc.

Tips: Each chapter of this tutorial contains many servlet examples. You can directly click the "Run Example" button to view the results online. These examples will help you better understand and use servlets.

Latest chapter


Servlet 国际化 2016-10-17
Servlet 调试 2016-10-17
Servlet 包 2016-10-17
Servlet 发送电子邮件 2016-10-17
Servlet 自动刷新页面 2016-10-17
Servlet 点击计数器 2016-10-17
Servlet 网页重定向 2016-10-17
Servlet 处理日期 2016-10-17