Servlet (Server Applet) is the abbreviation of Java Servlet. It is called a small service program or service connector. It is a server-side program written in Java. It has the characteristics of being independent of platform and protocol. Its main function is to browse and generate interactively. data to generate dynamic web content.
In the narrow sense, Servlet refers to an interface implemented by the Java language. In the broad sense, Servlet refers to any class that implements this Servlet interface. Generally speaking, people understand Servlet as the latter. Servlets run in Java-enabled application servers. In principle, 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.
The life cycle of servlet
The life cycle of servlet is controlled by the servlet container. It is mainly divided into three stages: initialization, running and destruction. The servlet container loads Servlet, after instantiation, call the init() method for initialization, run the service() method when the request arrives, call the doget or dopost method according to the corresponding request, and call the destroy() method when the server decides to destroy the instance (release the resources occupied by the servlet : close the database connection, close the file input and output stream), during the entire life cycle, the initialization and destruction of the servlet will only occur once, and the number of times the service method is executed depends on the number of times the servlet is accessed by the client.
Characteristics of Servlet
Servlet is a single instance multi-threaded. It only creates a servlet object, but each request will start a thread and store it in its own thread stack memory. Execute the service method.
A Servlet instance will only execute the parameterless constructor and init() method once, and it will be executed on the first access.
Every time the user submits a request for the current Servlet, the service() method will be executed.
A Servlet instance will only execute the destroy() method once, when the application stops.
Since Servlet is a single instance and multi-threaded, in order to ensure its thread safety, it is generally not recommended to define modifiable member variables in the Servlet class, because each thread can modify this member variable , thread safety issues will arise.
By default, Servlet will not be instantiated when the web container starts.
package com.ntqn.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class IndexServlet */ @WebServlet("/IndexServlet.php") public class IndexServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public IndexServlet() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Hello,Sevlet
"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
The above is the detailed content of What is the servlet concept?. For more information, please follow other related articles on the PHP Chinese website!