A servlet is a piece of Java code that can extend the functionality of a Web server. Each servlet runs in a process, and a new request to the servlet runs as a thread in the servlet process.
The servlet container is responsible for connecting the Web server and the servlet process. The servlet is responsible for providing the servlet's run-time environment and loading and invoking these servlet at the appropriate time. The servlet container can be run in the same process as the Web server, or it can run separately in a process. Even the servlet container itself is a Web server, like Tomcat.
The servlet container passes the user's request to the servlet, and then converts the servlet's response to the HTTP reply message back to the customer.
each servlet process has a servlet context. This servlet context corresponds to a Web application. The Servlet context associates a URL path prefix.
Typically, the servlet receives a request from the client delivered by the servlet container, processes some business logic, and then writes the HTML code into the response stream. But this approach makes the business logic and the Web UI mix, difficult to maintain, and make it impossible for programmers and web designers to work separately.