Assignments Servlet
Assignments Servlet
A servlet is a Java programming language class used to extend the capabilities of servers
that host applications accessed by means of a request-response programming model.
Servlets are server-side components that provide a powerful mechanism for developing
server-side programs. They are used to create dynamic web content, handle user requests,
and generate responses to those requests.
1. Loading and Instantiation: When a servlet container (such as Tomcat or Jetty) starts or
when a request is received for the servlet for the first time, the container loads the
servlet class and creates an instance of it.
2. Initialization: After the servlet is instantiated, the container initializes it by calling the
init() method. This method is called only once during the lifecycle of the servlet and is
used to perform any one-time initialization tasks, such as loading configuration
parameters or establishing database connections.
3. Servicing Requests: Once the servlet is initialized, it can service client requests. Each
time a client sends a request to the servlet, the container calls the service() method,
passing in the request and response objects. The service() method determines the type
of request (e.g., GET, POST, PUT) and dispatches it to the appropriate method (doGet(),
doPost(), etc.) for processing.
4. Request Handling: In this stage, the servlet processes the client's request, which
typically involves tasks such as reading parameters from the request, performing
business logic, accessing databases, and generating dynamic content for the response.
5. Destroying: When the servlet container shuts down or decides to remove the servlet
from service (e.g., due to lack of use or to conserve resources), it calls the destroy()
method of the servlet. This method allows the servlet to release any resources it has
acquired during its lifespan, such as closing database connections or releasing file
handles.
6. Finalization: After the destroy() method is called, the servlet instance is garbage
collected by the Java Virtual Machine (JVM) and its memory is reclaimed.
The life-cycle of a servlet ensures that it can be efficiently managed by the servlet
container, allowing for scalability, resource management, and ease of maintenance in web
applications.
The life-cycle methods for a servlet are defined by the javax.servlet.Servlet interface and
include the following methods:
1. init(ServletConfig config): This method is called by the servlet container to initialize the
servlet. It is called only once during the lifecycle of the servlet, usually when the servlet
is first loaded into memory. The ServletConfig object passed as a parameter provides
the servlet with configuration information from the deployment descriptor (web.xml).
Initialization tasks such as setting up resources like database connections or initializing
variables can be performed here.
2. service(ServletRequest request, ServletResponse response): This method is called by
the servlet container to process each client request. It handles the request and
generates an appropriate response. The ServletRequest and ServletResponse objects
passed as parameters represent the client's request and the servlet's response,
respectively. The container determines the specific HTTP method (GET, POST, etc.)
used in the request and calls the appropriate doXXX() method (e.g., doGet(), doPost())
based on that method.
3. destroy(): This method is called by the servlet container to indicate that the servlet is
being taken out of service. It allows the servlet to release any resources that it has been
holding (such as database connections, file handles, etc.). This method is called only
once during the lifecycle of the servlet, typically when the servlet container is shutting
down or when the servlet is being removed from service due to lack of use.
In addition to these methods, the GenericServlet class (a convenience base class for
servlets) provides default implementations for the init() and destroy() methods. Servlets
typically extend GenericServlet or its subclass HttpServlet to implement the servlet
functionality. The HttpServlet class further provides convenience methods such as doGet(),
doPost(), etc., which are called by the service() method based on the HTTP request method.
These methods can be overridden by the servlet to handle specific types of requests.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
Create a Servlet:
Create a Java class that extends HttpServlet and overrides the doGet method to handle
HTTP GET requests.
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
<servlet>
<servlet-name>NameServlet</servlet-name>
<servlet-class>NameServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NameServlet</servlet-name>
<url-pattern>/showName</url-pattern>
</servlet-mapping>
</web-app>
This setup will display "Hello, John Doe!" on the browser when you access the specified URL.
Adjust the name variable to display a different name.
HTTP GET HTTP POST
GET request is comparatively better than POST request is comparatively less better
Post so it is used more than the Post than Get method, so it is used less than the
request. Get request.
GET requests are only used to request data POST requests can be used to create and
(not modify) modify data.
GET request is comparatively less secure POST request is comparatively more secure
because the data is exposed in the URL because the data is not exposed in the URL
bar. bar.
Request made through GET method are Request made through POST method is not
stored in Browser history. stored in Browser history.
GET method request can be saved as POST method request can not be saved as
bookmark in browser. bookmark in browser.
Request made through GET method are Request made through POST method are not
stored in cache memory of Browser. stored in cache memory of Browser.