Servlet Short Note
Introduction to Servlets
1. Servlets are Java programs that run on a web server, handling client requests and generating
dynamic responses.
2. Servlet used to process or store data submitted by an HTML form, provide dynamic content
like returning results from a database query, manage state information, etc.
Servlet Lifecycle
The lifecycle of a servlet is controlled by the container (e.g., Tomcat), and it goes through the
following stages:
1. Loading and Instantiation: The servlet class is loaded and an instance is created.
2. Initialization (init method): The init method is called once to initialize the servlet.
3. Request Handling (service method): The service method is called for each request to process
it.
4. Destruction (destroy method): The destroy method is called once before the servlet is
removed from service.
Setting Up the Environment
Required Tools
JDK: Java Development Kit
Apache Tomcat: Web server and servlet container
Eclipse/IntelliJ IDEA: Integrated Development Environment (IDE)
Configure tomcat with IDE
Creating a Simple Project
1. Eclipse Setup:
Open Eclipse and create a new Dynamic Web Project.
Set up Apache Tomcat in Eclipse.
Create a new servlet class.
Creating First Servlet
There are three ways to craete servlet
1. implementing Servlet Interface
2. extending GenericServlet class
3. extending HttpServlet class.
Example:
Copy
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
[Link]().println("<h1>Hello, World!</h1>");
run project , IDE will automatically deploy the servlet to web server.
Handling Requests and Responses
They handle HTTP requests from clients (browsers) and generate dynamic responses.
1. Client Sends Request:
A client (browser) sends an HTTP request to the web server containing information like:
Request Method (GET, POST, etc.)
URL of the requested resource (Servlet)
Headers (e.g., user agent, cookies)
Optional request body (for POST requests)
2. Servlet Container Intercepts:
The web server's Servlet container intercepts the request.
It identifies the corresponding Servlet based on the URL mapping defined in [Link].
3. Servlet Invoked (doGet/doPost):
The container invokes the appropriate method within the Servlet class:
doGet(HttpServletRequest request, HttpServletResponse response) for GET requests.
doPost(HttpServletRequest request, HttpServletResponse response) for POST
requests.
Other methods exist for less common HTTP methods (PUT, DELETE).
4. Processing Request:
Inside the doGet or doPost method, the Servlet retrieves information from the request
object:
Request Parameters: Use getParameter(String name) to access form data or query
string parameters.
Headers: Use getHeader(String name) to access request headers.
Request Body: Use getInputStream() for binary data or a BufferedReader for text
data (for POST requests).
5. Generating Response:
The Servlet processes the request information and generates the response content.
It uses the HttpServletResponse object to control the response:
Setting Content Type: Use setContentType(String type) (e.g., "text/html" for HTML
content).
Writing Response: Use getWriter() for text output or getOutputStream() for binary
data.
Setting Status Code: Use setStatus(int code) to indicate success (200 OK), redirect
(302 Found), or error (404 Not Found).
6. Sending Response:
The Servlet container packages the response object and sends it back to the client (browser).
Creating a Feedback Form Application with Servlets
1. Define the Feedback Form (HTML):
Create an HTML file ([Link]) with a form containing fields for user information (optional) and
feedback message:
Copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Feedback Form</title>
</head>
<body>
<h1>Feedback Form</h1>
<form action="submitFeedback" method="post">
<label for="name">Name (Optional):</label>
<input type="text" id="name" name="name"><br>
<label for="feedback">Feedback:</label>
<textarea id="feedback" name="feedback" rows="5" cols="30"></textarea><br>
<button type="submit">Submit Feedback</button>
</form>
</body>
</html>
2. Create a Feedback Servlet (Java):
Create a Java class ([Link]) extending HttpServlet.
Override the doPost method to handle form submissions:
Copy
public class FeedbackServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Get feedback message from the request
String name = [Link]("name"); // Optional
String feedback = [Link]("feedback");
// Process the feedback (e.g., store in database, send email)
[Link]("Received feedback from: " + name);
[Link]("Feedback message: " + feedback);
// (Optional) Send a success response
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h1>Thank you for your feedback!</h1>");
3. [Link] Configuration:
Create a [Link] file to map the URL pattern for the feedback form and the Servlet:
Copy
<web-app>
<servlet>
<servlet-name>FeedbackServlet</servlet-name>
<servlet-class>[Link]</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FeedbackServlet</servlet-name>
<url-pattern>/submitFeedback</url-pattern>
</servlet-mapping>
</web-app>
Session Management
Session management is crucial in web applications to maintain user state across multiple requests.
1. Using HttpSession:
The most common approach.
Servlets leverage the HttpSession object to store user-specific data.
Copy
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = [Link]("username");
String password = [Link]("password");
// (Assuming successful login logic)
HttpSession session = [Link]();
[Link]("loggedInUser", username);
// Redirect to a welcome page
[Link]("[Link]");
}
public class WelcomeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = [Link]();
String username = (String) [Link]("loggedInUser");
if (username != null) {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h1>Welcome, " + username + "!</h1>");
} else {
// Redirect to login page if not logged in
[Link]("[Link]");
2. Using Cookies:
Stores data on the client-side (user's browser).
Less secure than sessions for sensitive information (due to potential theft).
Copy
public class RememberMeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String rememberMe = [Link]("rememberMe");
if (rememberMe != null && [Link]("on")) {
// Create a cookie to store a user identifier
String userId = "12345"; // (Assuming user ID generation)
Cookie cookie = new Cookie("userId", userId);
[Link](60 * 60 * 24 * 7); // Expires in a week
[Link](cookie);
// Redirect to the main page
[Link]("[Link]");
3. URL Rewriting (Less Secure):
Encodes session ID in the URL.
Not recommended due to security concerns and potential for long URLs.
Copy
// (Pseudocode, not recommended for real applications)
String sessionId = [Link]().getId();
String targetUrl = "[Link]?sessionid=" + sessionId;
[Link](targetUrl);
Error Handling
Configure custom error pages in [Link].
Copy
<error-page>
<error-code>404</error-code>
<location>/[Link]</location>
</error-page>
ServletContext and ServletConfig
ServletContext
Definition: ServletContext is an interface provided by the servlet container to communicate
with its environment.
Purpose: It allows servlets to access web application parameters, communicate with other
servlets, and share resources.
Scope: The ServletContext is available to all the servlets and JSPs in a web application,
making it suitable for sharing global information.
Key Methods:
getInitParameter(String name): Returns the value of a context-wide initialization parameter.
getAttribute(String name): Retrieves an attribute stored in the context.
setAttribute(String name, Object object): Stores an attribute in the context.
getRealPath(String path): Returns the real path of a file on the server.
Copy
public class ContextExampleServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = getServletContext();
String paramValue = [Link]("globalParam");
[Link]().println("Global Param: " + paramValue);
[Link]("sharedData", "This is shared across the application");
String sharedData = (String) [Link]("sharedData");
[Link]().println("Shared Data: " + sharedData);
}
}
ServletConfig
Definition: ServletConfig is an interface that provides servlet-specific configuration
information to a servlet.
Purpose: It allows a servlet to access initialization parameters defined for it in the
deployment descriptor ([Link]) or annotations.
Scope: ServletConfig is unique to each servlet, meaning it contains initialization parameters
specific to the servlet it is associated with.
Key Methods:
getServletName(): Returns the name of the servlet.
getInitParameter(String name): Retrieves the value of a specific initialization parameter for
the servlet.
getServletContext(): Returns the ServletContext object for the web application.
Copy
public class ConfigExampleServlet extends HttpServlet {
private String initParam;
public void init(ServletConfig config) throws ServletException {
[Link](config);
initParam = [Link]("initParam");
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]().println("Init Param: " + initParam);
}
xml configuration
Copy
<servlet>
<servlet-name>ConfigExampleServlet</servlet-name>
<servlet-class>[Link]</servlet-class>
<init-param>
<param-name>initParam</param-name>
<param-value>This is an init parameter</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ConfigExampleServlet</servlet-name>
<url-pattern>/configExample</url-pattern>
</servlet-mapping>