Practical-3
AIM:
3.1 Servlet Programming Servlet Execution on Tomcat.
Objective(s):
To understand how to configure and run a simple servlet using the Apache Tomcat server.
To understand how to create a dynamic web project.
Outcome:
You will be able to set up Tomcat, deploy a servlet, and access it through a browser.
Problem Statement:
Configure the Apache Tomcat Server and execute a simple servlet application that responds
with a basic message, proving the setup is successful.
Algorithm:
Install JDK and set environment variables (JAVA_HOME).
Download and extract Apache Tomcat to a directory.
Set environment variable CATALINA_HOME for Tomcat.
Start Tomcat server using startup.bat or startup.sh.
Create a dynamic web project or folder structure for the servlet.
Write a servlet class by extending HttpServlet and overriding doGet().
Configure servlet mapping in web.xml or use @WebServlet.
Compile the servlet and place class files under WEB-INF/classes.
Deploy the application into Tomcat’s webapps directory.
Access the servlet in browser using http://localhost:8080/YourApp/YourServlet.
Verify output – the servlet should respond with the basic message.
Program:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
Enrollment no: 2303031460137 Page no: 9
</head>
<body>
<h1>Welcome to Servlet</h1>
</body>
</html>
The structure of dynamic web project of request details.
OUTPUT:
Enrollment no: 2303031460137 Page no: 10
AIM:
3.2 A Servlet program to execute Hello World.
Objective:
Learn the basic structure and response mechanism of a servlet.
Outcome:
Understand the basic servlet life cycle and response writing using HttpServletResponse.
Problem Statement:
Write a servlet program that responds with the message "Hello World" when accessed
via a browser using a GET request.
Algorithm:
Install and configure Apache Tomcat and JDK.
Create a Java class that extends HttpServlet.
Override the doGet() method.
Set response content type and print "Hello World" using PrintWriter.
Compile the servlet with servlet API in the classpath.
Configure servlet in web.xml or use @WebServlet annotation.
Deploy the servlet in Tomcat’s webapps directory.
Start Tomcat and access the servlet via browser using the defined URL.
Program:
package com.parul.controller;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
Enrollment no: 2303031460137 Page no: 11
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Hello World</title></head>");
out.println("<body>");
out.println("<h1>Hello, World!</h1>");
out.println("</body>");
out.println("</html>");}}
The structure of dynamic web project of Hello World
OUTPUT:
Enrollment no: 2303031460137 Page no: 12
AIM:
3.3 A Servlet Program to Execute Request Details.
Objective:
To learn how to:
• Create a servlet that handles GET requests.
• Retrieve and display request details.
• Accept and display form parameters (username and age).
Outcome:
Able to create a servlet that receives parameters and displays request metadata and user input
on a web page.
Problem Statement:
Design a servlet named example2 that:
• Accepts username and age parameters.
• Displays HTTP method, request URI, protocol, headers, and user input.
Algorithm:
Create a servlet class named example2 extending HttpServlet.
Override the doGet() and/or doPost() method as needed.
Retrieve parameters username and age from the request using request.getParameter().
Extract request details:
• HTTP method → request.getMethod()
• Request URI → request.getRequestURI()
• Protocol → request.getProtocol()
• Headers → Use request.getHeaderNames() and request.getHeader()
Set content type of response to text/html.
Use PrintWriter to display:
• HTTP method, URI, protocol
• Request headers (loop through)
• User input (username and age)
Configure servlet mapping in web.xml or use @WebServlet("/example2").
Deploy to Tomcat and access via browser with parameters in URL or via form.
Enrollment no: 2303031460137 Page no: 13
Program:
package com.parul.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/example2")
public class example2 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String username = request.getParameter("username");
String age = request.getParameter("age");
pw.println("<h2>Request Details</h2>");
pw.println("<p>Method: " + request.getMethod() + "</p>");
pw.println("<p>Request URI: " + request.getRequestURI() + "</p>");
pw.println("<p>Protocol: " + request.getProtocol() + "</p>");
pw.println("<h3>Headers:</h3>");
Enumeration<String> headers = request.getHeaderNames();
while (headers.hasMoreElements()) {
String header = headers.nextElement();
pw.println("<p>" + header + ": " + request.getHeader(header) + "</p>");
}
pw.println("<h3>Parameters:</h3>");
if (username == null || age == null) {
pw.println("<p>No parameters found. Try using ?username=Leela&age=21 in the
URL.</p>");
} else {
pw.println("<p>Username: " + username + "</p>");
pw.println("<p>Age: " + age + "</p>");
}
Enrollment no: 2303031460137 Page no: 14
}
}
The structure of dynamic web project of request details.
Enrollment no: 2303031460137 Page no: 15
OUTPUT:
Enrollment no: 2303031460137 Page no: 16
AIM:
3.4 A Servlet program to handle User form.
Objective:
• Understand the difference between doGet() and doPost() in servlets.
• Learn how to handle form data submitted via the HTTP POST method.
• Retrieve parameters from the request object and display them dynamically.
Outcome:
After successfully completing this practical, you will be able to:
• Understand and implement the doPost() method in a Java Servlet.
• Collect and process user input from an HTML form securely via the POST method.
• Retrieve and validate parameters using HttpServletRequest.
• Dynamically display responses based on input validation.
• Demonstrate basic form handling and server-side response generation using Servlets.
Problem Statement:
Design a login form in HTML that collects username and password. Create a servlet that uses
the doPost() method to process this form data and display a welcome message if credentials
are correct, else display an error message.
Algorithm:
Create an HTML form with username and password input fields.
Set form method to POST and action to the servlet URL (e.g., login).
Create a servlet class that extends HttpServlet.
Override the doPost() method in the servlet.
Retrieve form
data using request.getParameter("username") and request.getParameter("password").
Validate credentials against predefined values (e.g., hardcoded).
Set content type of the response to text/html.
Use PrintWriter to display:
• Welcome message if login is successful
• Error message if credentials are incorrect
Enrollment no: 2303031460137 Page no: 17
Configure servlet mapping using web.xml or @WebServlet("/login").
Deploy the application in Tomcat and test the login form in the browser.
Program:
#HTML CODE
@login.html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h1>Welcome to dynamic project</h1>
<a href='loginform'>Login</a><br/>
</body>
</html>
@java code
package com.parul.controller;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/loginform")
public class loginform extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw = response.getWriter();
response.setContentType("text/html");
Enrollment no: 2303031460137 Page no: 18
pw.write("<h2>Login Dialog</h2>");
pw.write("<form method='post' action='loginform'>");
pw.write("<label>User name:</label>");
pw.write("<input type='text' name='txtuname' required/><br>");
pw.write("<label>Password:</label>");
pw.write("<input type='password' name='txtupass' required/><br>");
pw.write("<input type='submit' value='Login'/>");
pw.write("</form>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw = response.getWriter();
response.setContentType("text/html");
String username = request.getParameter("txtuname");
String password = request.getParameter("txtupass");
if ("admin".equals(username) && "password".equals(password)) {
pw.write("<h1 style='color:green;'>Welcome, " + username + "!</h1>");
} else {
pw.write("<h1 style='color:red;'>Invalid credentials</h1>");
}
}
}
Enrollment no: 2303031460137 Page no: 19
The structure of dynamic web project to perform login.
OUTPUT:
Enrollment no: 2303031460137 Page no: 20
Enrollment no: 2303031460137 Page no: 21