Java Servlet

Q1. What is the Lifecycle of Java Servlet?

Java Servlet has a pre-defined lifecycle starting from initializer (memory allocation) until the object is destructed (memory deallocated). It takes place in following steps :

  1. init() – is called as soon as the request is received by the web server and a new servlet instance is initialized.
  2. service() – to handle client request and redirect the request to an appropriate doGet() or doPost()
  3. destroy() – called when the request is handled, response sent back to the client and finally the memory allocated to the servlet is deallocated.

Q2. How to configure a Java Servlet?

Configuration of Java Servlet is defined inside a deployment descriptor file – web.xml. Although, it is legacy now as most servlet based applications nowadays used Annotation based configuration to configure a Java Servlet using @WebServlet Annotation.

Example : @WebServlet(name = “MyServlet”, urlPatterns = “/myServlet”)

Q3. What is a Servlet Filter?

A Servlet Filter is an additional component for performing pre-post processing work on the web requests like logging, monitoring, debugging and troubleshooting.



Introduction to Java Servlets

Today, we all are aware of the need to create dynamic web pages i.e. the ones that can change the site contents according to the time or can generate the content according to the request received from the client. If you like coding in Java, then you will be happy to know that using Java there also exists a way to generate dynamic web pages and that way is Java Servlet. But before we move forward with our topic let’s first understand the need for server-side extensions. 

Similar Reads

What is Java Servlet?

Java Servlets are the Java programs that run on the Java-enabled web server or application server. They are used to handle the request obtained from the web server, process the request, produce the response, and then send a response back to the web server....

Java Servlets Architecture

Servlet Architecture can be depicted from the image itself as provided below as follows:...

What is CGI(Common Gateway Interface)?

CGI is actually an external application that is written by using any of the programming languages like C or C++ and this is responsible for processing client requests and generating dynamic content....

Difference between Java Servlets and CGI

Servlet CGI (Common Gateway Interface) Servlets are portable and efficient. CGI is not portable. In Servlets, sharing data is possible. In CGI, sharing data is not possible. Servlets can directly communicate with the webserver. CGI cannot directly communicate with the webserver. Servlets are less expensive than CGI. CGI is more expensive than Servlets. Servlets can handle the cookies. CGI cannot handle the cookies....

Servlets APIs

Servlets are built from two packages:...

The Servlet Container

Servlet container, also known as Servlet engine, is an integrated set of objects that provide a run time environment for Java Servlet components. In simple words, it is a system that manages Java Servlet components on top of the Web server to handle the Web client requests....

Conclusion

Java Servlets are crucial components for defining business logic and handling complex web requests. These components promote the dynamic development of a web site and has a lot of potential to change the application dynamics. Here are some of the key features we learn in this article:...

FAQs on Java Servlet

Q1. What is the Lifecycle of Java Servlet?...