:
✅ 1. What is a Servlet?
A Servlet is a Java program that runs on a web server.
It helps to handle client requests (from a browser) and send back responses.
Works in a client-server model.
Example use: Form submission, data display, etc.
✅ 2. Why Use Servlets?
Servlets help in creating dynamic web pages.
They can handle multiple client requests at the same time.
They are faster and more secure than traditional CGI scripts.
✅ 3. Servlet Lifecycle (Simple Steps)
1. Loading – Servlet class is loaded by the server.
2. Initialization (init() method) – Called once to set up the servlet.
3. Service (service() method) – Called every time a client sends a request. Processes
the request and sends a response.
4. Destroy (destroy() method) – Called once when the server stops or unloads the
servlet to release resources.
✅ 4. Simple Example of a Servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Hello, this is a simple servlet response!</h1>");
}
}
✅ 5. What are HttpServletRequest and HttpServletResponse?
HttpServletRequest – Used to get data from the client (like form data).
HttpServletResponse – Used to send data back to the client (like an HTML page).
✅ 6. How to Run a Servlet?
1. Write servlet code.
2. Compile the servlet (javac).
3. Put .class files in WEB-INF/classes.
4. Map servlet in web.xml.
5. Run the server (like Apache Tomcat).
6. Access using
browser: http://localhost:8080/YourServletProject/HelloServlet
✅ 7. Simple Difference: Forward vs Redirect
Feature Forward Redirect
Who controls Server internally Client (browser)
URL in browser Same Changes
Number of requests One Two (Request and Redirect)
✅ 8. Session Management Basics
To track a user over multiple requests.
Example: Login session.
Use HttpSession object:
HttpSession session = request.getSession();
session.setAttribute("username", "student123");
Role of HttpServletRequest and HttpServletResponse
HttpServletRequest: Provides methods to access HTTP request data such as
parameters, headers, and attributes. Used to read data sent by clients (forms, query
strings, etc.).
HttpServletResponse: Provides methods to construct and send a response back to the
client, including setting content type, adding cookies, sending redirects, and writing
output using PrintWriter.
Together, they form the core of handling client-server communication in a servlet-based web
application.
Compiling and Building a Servlet-Based Web Application
1. Write servlet code implementing HttpServlet.
2. Configure deployment descriptor (web.xml) to map URLs to servlets.
3. Compile servlet classes using Java compiler (javac).
4. Place the compiled .class files in WEB-INF/classes directory.
5. Package the application as a .war (Web Application Archive).
6. Deploy the .war to a servlet container (e.g., Apache Tomcat).
7. The server extracts and runs the application, making servlets available for client
requests.
What is ServletConfig?
ServletConfig is an object that stores configuration data for a specific servlet.
It holds initialization parameters defined in web.xml.
Example use:
public void init(ServletConfig config) {
String dbName = config.getInitParameter("databaseName");
}
Purpose: Pass parameters to a servlet when it is first created.
10. What is ServletContext?
ServletContext is an object that allows communication between all servlets in the
same web application.
Example use:
ServletContext context = getServletContext();
context.setAttribute("globalData", "Shared across all servlets");
Purpose: Share data across multiple servlets in the same application.
ServletConfig vs. ServletContext
ServletConfig:
o Provides configuration information specific to a single servlet.
o Contains initialization parameters defined for that particular servlet
in web.xml.
o Obtained by getServletConfig().
ServletContext:
o Provides a global context across the entire web application.
o Allows access to shared resources like attributes and initialization parameters.
o Obtained by getServletContext().
Key Difference: ServletConfig is per-servlet, while ServletContext is application-wide.
Steps to Create and Deploy a Simple Servlet using NetBeans
1. Open NetBeans IDE and create a new project → Select Java Web → Web
Application.
2. Name the project and select a server (e.g., Apache Tomcat).
3. In the project structure, right-click on the project → New → Servlet.
4. Implement the servlet methods (doGet(), doPost()).
5. Build the project using Build option.
6. Deploy the application by running it → NetBeans deploys automatically to the server.
7. Access the servlet using a URL
like: http://localhost:8080/YourProject/YourServlet.
ServletInputStream and ServletOutputStream
ServletInputStream:
o Used to read binary data from a client request.
o Obtained by calling HttpServletRequest.getInputStream().
o Suitable for processing file uploads or binary data.
ServletOutputStream:
o Used to send binary data to the client.
o Obtained by calling HttpServletResponse.getOutputStream().
o Useful for sending images, PDFs, or binary content.
Difference between GenericServlet and HttpServlet
GenericServlet:
o Abstract class.
o Protocol-independent.
o Implements the Servlet interface.
o Requires implementation of the service(ServletRequest,
ServletResponse) method.
HttpServlet:
o Extends GenericServlet.
o Protocol-dependent (specifically HTTP).
o Provides methods like doGet(), doPost(), doPut(), doDelete(), etc.
o Simplifies handling HTTP requests and responses.
Lifecycle of a Servlet
The lifecycle of a servlet consists of several phases managed by the servlet container:
1. Loading and Instantiation – The servlet class is loaded by the servlet container
when requested or during server startup.
2. Initialization (init() method) – The init() method is called once to initialize the
servlet. A ServletConfigobject is passed to configure initialization parameters.
3. Request Handling (service() method) – For each client request, the servlet
container calls the service()method,
passing HttpServletRequest and HttpServletResponse objects.
4. Destruction (destroy() method) – The destroy() method is called once when the
servlet is unloaded, allowing cleanup of resources.
[Servlet Container] → load() → instantiate() → init() → service() ← multiple
requests → destroy()