Getting Started with Servlets

Source: Internet
Author: User

What is 1.servlet?

The Java servlet is designed to respond to HTTP requests in response to a Web application context.

The subclass HttpServlet is used when creating a servlet, and the methods in that class allow you to access the request and response wrappers (wrapper), which you can use to process requests and create responses.

Of course, the HTTP protocol is not Java-specific. It is just a specification that defines the approximate design of service requests and responses. Java servlet Classes Wrap those lower-level structures in Java classes that contain convenient methods that make them easier to handle in the Java language environment. As defined in the configuration file for the specific servlet container that you are using, when a user makes a request through a URL, the Java servlet classes convert it to one HttpServletRequest and send it to the destination that the URL points to. When the server-side finishes its work, the Java Runtime Environment (Java Runtime Environment) wraps the result in one and HttpServletResponse sends the original HTTP response back to the client that made the request. When interacting with a WEB application, it is common to make multiple requests and get multiple responses. All of this is in a conversational context, and the Java language wraps it in an HttpSession object. When you process a response, you can access the object and add an event to it when you create the response. It provides a number of cross-request contexts.

containers , such as Tomcat, will manage the runtime environment for the servlet. You can configure the container to customize how the EE server works, and you must configure it to expose the servlet to the outside world. As we will see, through the various configuration files in the container, you build a bridge between the URL (entered by the user in the browser) and the server-side component that will handle the request that you need to convert the URL. When you run the application, the container loads and initializes the servlet to manage its life cycle .

When we say that a servlet has a life cycle, it simply means that when the servlet is invoked, things happen in a predictable way. In other words, methods created on any servlet are always called in the same order. Here is a typical scenario:

    • The user enters a URL in the browser. The WEB server configuration file determines whether the URL points to a servlet managed by a servlet container running on the server.
    • If an instance of the servlet has not been created (an application has only one instance of the servlet), then the container loads the class and instantiates it.
    • The container calls on the servlet init() .
    • The container is called on the servlet and is service() passed in and in the wrapper HttpServletRequest HttpServletResponse .
    • The servlet typically accesses the elements in the request, represents other server-side classes to execute the requested service, and accesses resources such as databases, and then populates the response with that information.
    • If necessary, at the end of the useful life of the servlet, the container invokes the servlet destroy() to clear it.

Original link: http://www.ibm.com/developerworks/cn/education/java/j-intserv/index.html

Getting Started with Servlets

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.