servlet 是应用程序
Web container is responsible for managing execution of servlets and JSP pages for Java EE application.
Web容器负责管理Java EE应用程序的servlet和JSP页面的执行。
When a request comes in for a servlet, the server hands the request to the Web Container. Web Container is responsible for instantiating the servlet or creating a new thread to handle the request. Its the job of Web Container to get the request and response to the servlet. The container creates multiple threads to process multiple requests to a single servlet.
当收到对Servlet的请求时,服务器会将请求移交给Web容器。 Web容器负责实例化servlet或创建一个新线程来处理请求。 Web容器的工作是获取对Servlet的请求和响应。 容器创建多个线程来处理对单个servlet的多个请求。
Servlets don't have a main() method. Web Container manages the life cycle of a Servlet instance.
Servlet没有main()方法 。 Web容器管理Servlet实例的生命周期。
Servlet工作原理快速修订 (Quick Revision on How a Servlet works)
User sends request for a servlet by clicking a link that has URL to a servlet.
用户通过单击具有指向Servlet的URL的链接来发送对Servlet的请求。
The container finds the servlet using deployment descriptor and creates two objects :
容器使用部署描述符找到servlet并创建两个对象:
- HttpServletRequestHttpServletRequest
- HttpServletResponseHttpServletResponse
Then the container creates or allocates a thread for that request and calls the Servlet's
service()
method and passes the request, response objects as arguments.然后,容器为该请求创建或分配线程,并调用Servlet的
service()
方法并将请求,响应对象作为参数传递。The
service()
method, then decides which servlet method,doGet()
ordoPost()
to call, based on HTTP Request Method(Get, Post etc) sent by the client. Suppose the client sent an HTTP GET request, so theservice()
will call Servlet'sdoGet()
method.然后,
service()
方法根据客户端发送的HTTP请求方法 (Get,Post等)确定要调用的servlet方法doGet()
或doPost()
。 假设客户端发送了一个HTTP GET请求,那么service()
将调用Servlet的doGet()
方法。Then the Servlet uses response object to write the response back to the client.
然后,Servlet使用响应对象将响应写回到客户端。
After the
service()
method is completed the thread dies. And the request and response objects are ready for garbage collection.在
service()
方法完成之后, 线程死亡。 并且请求和响应对象已准备好进行垃圾回收 。
翻译自: https://www.studytonight.com/servlet/how-a-servlet-application-work.php
servlet 是应用程序