Updated April 5, 2023
Introduction to Servlet Types
Java program similar to an applet that runs on a webserver to access the databases as per the requirements of the user and sends information back to the server is called Servlet. A single process is used as a thread for each user request so that the requests are completed in sequence at faster pace. The URL created for the request gives a result of Java servlet which has additional modules to run in Apache, Microsoft Internet Information, and Netscape Enterprise servers. Traffic is reduced as it connects through a direct route with the web server through the port number mentioned in the URL.
Types
There are two main types of Servlet. They are Generic and HTTP servlets. We can use the constructor method to initialize the Servlets with the help of init() and the destructor method to remove the servlet from the resources using destroy(). There is a separate method called service() to handle the servlet requests but they are handled in a different manner in both the servlets. Common Gateway Interface is used to connect with the server from the webpage so that the requests can be understood and responses can be given back to the user.
Generic Servlet:
• Generic Servlet is a direct division of Servlet type. The servlet is written in this form
javax.servlet.GenericServlet
This Servlet does not follow any protocol or is not bound to any form of protocol rules as to how to write the servlet and in which order. This results in having no support from any HTTP protocol from the system. We can write servlets easily in Generic Servlet making it common for any methods used in the system. Log method is also used in Generic Servlet which is defined in ServletContext Interface so that all the changes made in the servlet can be monitored easily.
With no specific protocol to support, we can use Generic Servlet with any protocols and there are no restrictions to be followed while using it with protocols. Specifically speaking, when developers develop the web page and have not decided which protocol to be used, Generic Servlet can be used to connect with servers and fetch the required information.
When we must handle multiple requests for running servlet requests, we should override service() method to handle the requests as it is not specific to any protocols. Also, while running in a routine, service() method cannot run with its usual timing and it is needed to override the same. This makes the service method used in abstract form and cannot help any protocol directly. Only init and destroy methods are used in the ServletConfig Interface making it easy for beginners to learn to write Generic Servlet.
- Generic Servlet helps in the configuration of any servlet application and implementation of any interface to help in using any method in the servlet as it is protocol independent. Any kind of request can be handled in Generic Servlet along with extending the uses of applications used in the server.
- Javax Servlet package is used and helps in extending java.lang.object to implement Generic Servlets along with Servlet Config which makes initialization parameters to be passed through XML language in the servlet.
- This is not preferred by developers as most of the time, HTTP protocol is used which works well with HTTP Servlet.
- Data is read directly from the browser. If it does not belong to HTTP protocol, it is managed with the help of Generic Servlet but if it is an HTTP web page, the request is sent to HTTP servlet. This data is processed with the help of any software or connecting with the database or getting information directly. The result is sent back to the browser either in txt or HTML format depending on the way the request is received. Also, a response is sent to the browser saying the type of document sent as data to the same.
HTTP Servlet:
HTTP Servlet is a subclass of Generic Servlet. The servlet is called as javax.servlet.HttpServlet.
- As the name suggests, they have HTTP support and can work in any HTTP environment with the help of servers. When there are HTTP-specific methods to be used, we can continue with HTTP servlet.
- HTTP Servlet can be used only with HTTP protocol and for other protocols, we should use Generic Servlet. Service methods should specify the HTTP protocol in the services handled making it most specific only to HTTP methods.
- Service methods are handled altogether with the help of several other commands. To post requests, doPost() is used, and to fetch the requests, doGet() is used. Other methods used in HTTP servlet are doTrace(), doDelete() and getServiceInfo(). With these sorts of commands, the service method of Generic Servlet can be replaced and used. Based on the transfer method, it redirects to the corresponding method and fetches the information for the servers. Service method is not abstract and it is evident for the users as to which commands to be used in each stage.
- Javax servlet Http package is used where Generic Servlet is extended to implement java.io.serializable along with the public class of HTTP servlet class. HTTP servlet has the properties of both Generic and HTTP servlet so that developers can make use of all the functionalities in the servlet.
- Void service is provided which is both public and private making the protocol to understand the server methods and work based on the same. Override service methods need not be used as most of the service methods are either used in Generic or HTTP servlets.
- Any Java library can be used in Servlet as the base language is Java and helps in communicating with applets and databases or any other software as per the developer requirement.
- Any application type can be developed with the help of Servlet that helps to maintain the speed of the application. As it is platform-independent, we should focus only on the server-side and connect with the application. We can use either Generic or HTTP servlet to write the application or can be written with the help of Servlet interface.
Recommended Articles
This is a guide to Servlet Types. Here we discuss the Introduction, Two main Types of Servlet respectively. You may also have a look at the following articles to learn more –