Department of Computer Engineering
Academic Year: 2025-26
Year: SE Semester: III Subject: JAVALab
Name of Student: Priyangee Kha
Roll No.: 31
Moodle Id: 24102108
Date of Performance: 1 9 - 9 - 2 5
Date of Submission: 19-9-25
Name of Instructor: Prof. Kadambari
EXERCISE NO. 5 -1
Aim: To perform a simple query printing names and phone numbers of all employees.
Program:
[Link]-
<web-app>
<servlet>
<servlet-name>EmployeeServlet</servlet-name>
<servlet-class>EmployeeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EmployeeServlet</servlet-name>
<url-pattern>/employees</url-pattern>
</servlet-mapping>
</web-app>
[Link]-
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class EmployeeServlet extends HttpServlet {
// Database connection details
private static final String URL = "jdbc:mysql://localhost:3306/companydb";
private static final String USER = "root"; // Change as needed
private static final String PASS = "123456"; // Change as needed
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html><head><title>Employee List</title></head><body>");
[Link]("<h2>Employee Names and Phone Numbers</h2>");
[Link]("<table border='1' cellpadding='10'>");
[Link]("<tr><th>Name</th><th>Phone</th></tr>");
try {
// Load MySQL JDBC driver
[Link]("[Link]");
// Connect to the database
Connection con = [Link](URL, USER, PASS);
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT name, phone FROM employees");
// Print result set rows
while ([Link]()) {
[Link]("<tr><td>" + [Link]("name") + "</td><td>"
+ [Link]("phone") + "</td></tr>");
}
// Close connections
[Link]();
[Link]();
[Link]();
} catch (Exception e) {
[Link]("<p style='color:red;'>Error: " + [Link]() + "</p>");
}
[Link]("</table></body></html>");
[Link]();
}
}
Sql-
CREATE DATABASE companydb;
USE companydb;
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
phone VARCHAR(15)
);
INSERT INTO employees (name, phone) VALUES
('Alice', '9876543210'),
('Bob', '9123456780'),
('Charlie', '9988776655');
Output:
EXERCISE NO. 5 -2
Aim:
To Develop a web-based student registration system where students can enter their name and choose a
course through a JSP [Link] a Java Servlet to process the form data and store it in a MySQL database via
[Link] servlet mappings in [Link] to handle requests and display a confirmation after successful
registration.
Program:
[Link]-
<web-app>
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
</web-app>
[Link]-
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register Student</title>
</head>
<body>
<h2>Register Student</h2>
<form action="register" method="post">
Name: <input type="text" name="studentName"><br><br>
Course: <input type="text" name="studentCourse"><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>
[Link]-
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class RegisterServlet extends HttpServlet {
private static final String URL = "jdbc:mysql://localhost:3306/student_db";
private static final String USER = "root"; // Change to your MySQL username
private static final String PASS = "123456"; // Change to your MySQL password
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
String studentName = [Link]("studentName");
String studentCourse = [Link]("studentCourse");
try {
[Link]("[Link]");
Connection con = [Link](URL, USER, PASS);
String sql = "INSERT INTO students (name, course) VALUES (?, ?)";
PreparedStatement pstmt = [Link](sql);
[Link](1, studentName);
[Link](2, studentCourse);
int rowsAffected = [Link]();
[Link]("<html><body>");
if (rowsAffected > 0) {
[Link]("<h2>Student registered successfully!</h2>");
} else {
[Link]("<h2>Student registration failed.</h2>");
}
[Link]("</body></html>");
[Link]();
[Link]();
} catch (Exception e) {
[Link]("<html><body><h2 style='color:red;'>Error: " + [Link]() +
"</h2></body></html>");
} finally {
[Link]();
}
}
}
Output:
Conclusion:
Thus we have studied that JDBC, JSP, and Servlets together form a robust foundation for building
dynamic, database-driven Java web applications.
JDBC enables efficient interaction with relational databases, while Servlets handle server-side
request processing. JSP with Servlets simplifies the creation of dynamic web pages with embedded
Java code. Their integration ensures a scalable, maintainable, and platform-independent web
application architecture.