Every servlet must implement the javax.servlet.Servletinterface. Most servlets
Every servlet must implement the javax.servlet.Servletinterface. Most servlets implement it by extending one of two special classes: javax.servlet.GenericServletor javax.servlet.http.HttpServlet. A protocol-independent servlet should subclass GenericServlet, while an HTTP servlet should subclass HttpServlet, which is itself a subclass of GenericServletwith added HTTP-specific functionality. Unlike a regular Java program, and just like an applet, a servlet does not have a main()method. Instead, certain methods of a servlet are invoked by the server in the process of handling requests. Each time the server dispatches a request to a servlet, it invokes the servlet’s service()method. A generic servlet should override its service()method to handle requests as appropriate for the servlet. The service()method accepts two parameters: a request object and a response object. The request object tells the servlet about the request, while the response object is used to return a response. Figure 2-1 shows how a generic servlet handles requests. Figure 2-1. A generic servlet handling a request In contrast, an HTTP servlet usually does not override the service()method. Instead, it overrides doGet() to handle GET requests and doPost()to handle POST requests. An HTTP servlet can override either or both of these methods, depending on the type of requests it needs to handle. The service()method of HttpServlethandles the setup and dispatching to all the doXXX()methods, which is why it usually should not be overridden. Figure 2-2 shows how an HTTP servlet handles GET and POST requests. Figure 2-2. An HTTP servlet handling GET and POST requests An HTTP servlet can override the doPut()and doDelete()methods to handle PUT and DELETE requests, respectively. However, HTTP servlets generally don’t touch doHead(), doTrace(), or doOptions(). For these, the default implementations are almost always sufficient.
Note: If you are looking for good and quality webspace to host and run your java application check professional java hosting services