Java Servlets
In the client-server model, the client sometimes requests for a dynamic page i.e. created during run time. In these times Java servlets come in handy.
Servlet: Servlets are simple Java files that are used to create dynamic content on the server side. These servlets are stored in web containers such as TOMCAT, GLASS FISH and JBOSS.
Deployment descriptive file: A file named “web.xml” is also present in the web containers which is used to map the corresponding servlet to the request.
Servlet life Cycle:
The servlet life cycle contains three phases:
- Initialization
- Service
- Destruction
Initialization: In the initialization phase, the servlet is initialized using init() method which is used to load the resources required by the servlet such as DB parameters. __init() is defined in the servlet interface.
Service: The service phase in the servlet life cycle actually handles the requests by the clients. The service() method defined in the servlet interface accepts the requests and gives the responses back to the clients.The service phase contains serveral methods such as
dopost()- it is called by the server to handle the http post requests
Get BANTU CHETAN CHANDRA’s stories in your inbox
Join Medium for free to get updates from this writer.
doget()- it is called by the server to handle the http get requests
Destruction: In the destruction phase, the server calls destroy() method that is defined in the servlet interface to terminate the servlet gracefully and unload all the resources used by the servlet.
— → In the lifetime of a servlet life cycle init() and destroy() methods can be called only once but service() method can be called multiple times.
Example of a Java Servlet:
These files are stored in the web container to execute them properly.
Advantages of using Java servlets over CGI :
- Efficient
- portable
- secure
- user friendly
- cheaper
Conclusion :
Java Servlets has made the job of defining and deploying dynamic web pages to a sever easy and efficient. Sevlets provide robust techonology to process the requests to the server maintaining the client-server architecture properly working.





