servlet interfaces in Java EE Javax.servlet

Source: Internet
Author: User

Servlet interface

public interface Servlet

Its implementation classes are: Faceservlet, Genericservlet, HttpServlet

First, Introduction

The Servlet interface defines the methods that must be implemented by all servlets (classes that implement the interface).

What we typically call a servlet is a Java applet running on a server, which can be called a servlet program. They typically receive requests and respond from the Web client through the HTTP protocol.

If you want to implement this interface, you can create a generic, protocol-independent servlet by inheriting the Genericservlet class. or inherit httpservlet to create an HTTP protocol-based servlet.

The methods in the Servlet interface are used to initialize a servlet, some to serve the received request, and some to remove a servlet from the server. These methods are called life cycle methods, and they are called in the order of:

1. Create a servlet that initializes using the Init method;

2. The service method is used to process the customer's call;

3. The servlet is stopped, destroyed using the Destroy method, and then garbage collected and finalized.

In addition to these methods, the Servlet interface provides the Getservletconfig method used to obtain the boot information, the Getservletinfo method, which allows the servlet to return its basic information, such as author, version, copyright, etc.

Second, the method in the interface

1. void init (ServletConfig config) throws Servletexception

This method is called by the servlet container to indicate that a servlet is being put into use.

The servlet container calls this method once, after instantiating the servlet. The Init method must complete successfully before the servlet can receive any requests.

When the Init method is present, the servlet container cannot put the associated servlet into service:

    • Throw a servletexception;
    • No return within the time specified by the Web server

The Config object contains the configuration parameters and initialization parameters of the servlet.

2, ServletConfig Getservletconfig ()

Returns an ServletConfig object that contains the initialization and startup parameters of the servlet, and the ServletConfig object is the object that is passed into the Init method.

The implementation class of the Servlet interface is responsible for storing the ServletConfig object, so this method is able to return successfully.

3. Void Service (ServletRequest req,servletresponse res) throws Servletexception, IOException

Called by the servlet container, which causes the servlet to respond to the request received, and the service method will not be invoked until the Init method completes successfully.

The expected status code should always be set to raise or send the wrong servlet.

A servlet typically runs in a multithreaded servlet container that can handle multiple requests concurrently, and developers must be careful to synchronize access to any shared resources, such as files, network connections, and class and instance variables of the servlet. For more information on multithreaded programming, refer to the Muti-threaded programming section in Java Tutorial.

4. String Getservletinfo ()

Returns information about the servlet, such as author, version, and copyright.

The string returned by the method should be plain text rather than any tag type (such as html,xml, etc.).

5. void Destroy ()

Called by the servlet container to instruct the servlet that the servlet is stopping the service

This method is called only after all threads within the Servlet's service method have exited or timed out. And once the servlet container invokes this method, it will no longer raise its service method on this servlet.

servlet interfaces in Java EE Javax.servlet

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.