Create a Java Servlet
Overview
A programmer can create a Java Servlet using many approaches. The different ways to create a servlet are as follows:
- By implementing the Servlet interface
- By extending the GenericServlet base class
- By extending the HttpServlet base class
The Servlet interface and the GenericServlet are defined in the javax.servlet package. The GenericServlet and the HttpServlet are abstract base classes. The HttpServlet abstract class is defined in the javax.servlet.http package.
Examples
Implementing Servlet Interface
public class SampleServlet implements Servlet {
…
}
Extending GenericServlet abstract class
public class SampleServlet extends GenericServlet {
…
}
The GenericServlet defines the protocol-independent servlet. It implements the Servlet and ServletConfig interfaces. Subclasses extending the GenericServlet should override the abstract service method.
Extending HttpServlet abstract class
public class SampleServlet extends HttpServlet {
…
}
To develop a protocol-specific servlet like HTTP, we can extend HttpServlet. HttpServlet is HTTP protocol-specific servlet to service HTTP requests and provide HTTP responses to the client.
—
Java Tutorials
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/
For more information on Java, visit the official website :