"Reprint" The Role of web containers in web development (such as Tomcat)

Source: Internet
Author: User

When we talk about the Java applets that Servlets can understand about server-side processing of data, who is responsible for managing Servlets? At this point we need to use the Web container. It helps us manage the servlet and so on, so we just need to focus on the business logic.

what is a Web container?

The servlet has no main method, so how do we start a servlet, how to end a servlet, how to find a servlet, and so on, all controlled by another Java application, which we call the Web container.

Our most common tomcat is such a container. If the Web server application obtains a request to a servlet, the server does not hand the servlet to the servlet itself, but instead to the container that deploys the servlet. A container is required to provide HTTP requests and responses to the servlet, and the methods of the servlet, such as Dopost or doget, are called by the container.

the role of the Web container

The servlet needs to be managed by a web container, so what is the benefit of adopting such a mechanism?

    • Communication support
      Using the method provided by the container, you can simply implement the servlet's dialogue with the Web server. Otherwise you will have to set up a server search-and-paste, listen to the port, create a new stream and so a series of complex operations. The presence of a container helps us encapsulate this complex set of operations. Enables us to focus on the implementation of the business logic in the servlet.

    • Life Cycle Management
      The container is responsible for the entire life cycle of the servlet. How to load classes, instantiate and initialize Servlets, invoke servlet methods, and enable servlet instances to be garbage collected. With containers, we don't have to spend our energy thinking about these resources to manage garbage collection.

    • Multithreading support
      The container automatically creates a new Java thread for each servlet request that is received, and after the servlet finishes running, the container automatically ends the thread.

    • Declarative implementation Security
      With containers, you can use XML deployment profiles to configure security without having to hard-code them into a servlet.

    • JSP support
      The container translates the JSP into java!

how the container handles requests
How the Container handles a request01. Png

The client clicks on a URL with a URL that points to a servlet rather than a static interface.


How the Container handles a request02. Png

The container recognizes that the request is for a servlet, so create two objects:

    • HttpServletRequest
    • HttpServletResponse

03.PNG

The container finds the corresponding servlet based on the URL in the request, creates or assigns a thread for the request, and passes two object requests and response to the servlet thread.


Paste_image.png

The container invokes the Servlet's service () method. Depending on the type of request, the service () method calls the Doget () or Dopost () method.


Paste_image.png

The Doget () method generates a dynamic page and then fills the page into the response object, at which point the container still has a reference to the response object.


Paste_image.png

The thread ends. The container converts the Response object into an HTTP response, returns the client, and destroys the response and request objects.

URL and servlet mapping mode
<servlet>    <servlet-name>Ch1Servlet</servlet-name>    <servlet-class>ch1Servlet.Ch1Servlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>Ch1Servlet</servlet-name>    <url-pattern>/Ch1Servlet</url-pattern>  </servlet-mapping>

The servlet has three names:

    • URL name that the customer knows<url-pattern>/Ch1Servlet</url-pattern>
    • The internal name of the secret that the Deployer knows<servlet-name>Ch1Servlet</servlet-name>
    • Actual file name<servlet-class>ch1Servlet.Ch1Servlet</servlet-class>

"Reprint" The Role of web containers in web development (such as Tomcat)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.