What is a Servlet?
A Servlet is a Java class that handles HTTP requests and generates responses in a
web-based application. It runs on a web server or application server like Apache
Tomcat, Jetty, etc.
What is the lifecycle of a Servlet?
The lifecycle of a servlet is managed by the servlet container (e.g., Tomcat). It
consists of the following phases:
Instantiation: Servlet is instantiated once.
Initialization: init() method is called.
Request Handling: service() method is called for every client request.
Destruction: destroy() method is called before the servlet is unloaded.
What is the difference between doGet() and doPost() methods?
doGet(): Handles HTTP GET requests, typically used for fetching data from the
server.
doPost(): Handles HTTP POST requests, generally used for submitting data (like form
data) to the server.
What are the advantages of using Servlets?
Performance: Servlets are faster than CGI because they are loaded once and can
handle multiple requests.
Platform Independence: Servlets are written in Java and can run on any platform
that supports a servlet container.
Scalability: Servlets can efficiently handle a large number of concurrent requests.
What is the difference between a Servlet and JSP (Java Server Pages)?
Servlet: A servlet is a Java class that processes requests and generates responses.
It is more code-intensive and provides a programmatic approach to web development.
JSP: A JSP is a text-based page that is compiled into a servlet, and it is designed
to simplify the presentation layer with HTML embedded in Java code.
What is the web.xml file in a web application?s
The web.xml (deployment descriptor) file is used to configure servlet mappings,
initialize parameters, security settings, and more in a web application.
What is a ServletContext?
The ServletContext is an interface that allows servlets to interact with the
servlet container. It provides information about the environment in which the
servlet is running and is used for sharing data between servlets in the same web
application.
What is the difference between forward() and redirect() in Servlets?
forward(): It forwards the request from one servlet to another servlet or JSP
within the same server, without the client knowing.
redirect(): It sends a new request to a different URL and the client is aware of
this change as the URL in the browser is updated.
What is the purpose of init() method in a servlet?
The init() method is called once when the servlet is loaded into memory by the
servlet container. It is typically used for initial setup (like creating database
connections or loading configuration data).
What is a session in Servlets?
A session is a mechanism to maintain state between client requests. The servlet
container creates a session for each client, typically stored in a cookie
(JSESSIONID) or URL rewriting.
How can you handle session management in a servlet?
Sessions can be managed using HttpSession in a servlet:
Creating a session: HttpSession session = request.getSession();
Storing data in session: session.setAttribute("key", value);
Retrieving data from session: session.getAttribute("key");
Invalidating a session: session.invalidate();
What is a filter in Servlet?
A filter is an object that performs filtering tasks on either the request to a
servlet or the response from a servlet. Filters are typically used for logging,
authentication, authorization, input validation, and more.
What is the Filter interface and how do you use it?
The Filter interface is used to define filtering logic. It contains three main
methods:
init(): Initializes the filter.
doFilter(): Performs the filtering tasks.
destroy(): Cleans up resources when the filter is destroyed.
What is the difference between doGet() and doPost() in terms of security?
doGet(): Data sent through URL query parameters is visible in the browser's address
bar, making it less secure for sensitive data.
doPost(): Data sent in the body of the HTTP request is not visible in the URL,
making it more secure for transmitting sensitive information.
What are servlet filters and how do they differ from interceptors?
Servlet Filters: A Filter is an object that performs filtering tasks on either the
request to a servlet or the response from a servlet, usually in a web application.
Interceptors: Interceptors are used in frameworks like Spring, typically used for
pre- and post-processing of methods in a controller.
Explain the use of ServletConfig and ServletContext in servlets.
ServletConfig: Used to provide initialization parameters specific to a servlet.
ServletContext: Used to provide a global configuration for the entire web
application.
What is the @WebServlet annotation in Servlets?
The @WebServlet annotation is used to declare a servlet without needing to
configure it in web.xml. It simplifies servlet creation by providing an easier way
to specify servlet attributes like urlPatterns.
What is the purpose of the destroy() method in a Servlet?
The destroy() method is called when the servlet is being unloaded from memory. It
is used for cleanup tasks like closing database connections or releasing other
resources.
What is the difference between HttpServlet and GenericServlet?
HttpServlet: Extends GenericServlet and is designed to handle HTTP requests (GET,
POST, etc.). It provides convenience methods like doGet() and doPost().
GenericServlet: A generic protocol-independent servlet that can handle any type of
request and does not provide HTTP-specific methods.
What are some of the common performance optimization techniques for Servlets?
Use connection pooling for database connections.
Enable HTTP session clustering.
Implement caching mechanisms to reduce database load.
Avoid creating objects unnecessarily in the doGet() or doPost() methods.
Minimize I/O operations and optimize data retrieval.
How do you deploy a Servlet in a web application?
A servlet can be deployed by either:
Configuring it in the web.xml (deployment descriptor) file.
Using annotations (@WebServlet) to declare it directly in the servlet class.
What are the various HTTP status codes a Servlet might return?
200 OK: Successful request.
301 Moved Permanently: Resource has been permanently moved.
302 Found: Resource has been temporarily moved.
404 Not Found: Requested resource is not found.
500 Internal Server Error: Server encountered an error while processing the
request.