Lab 7: JSP Registration and CRUD with Session Management
This lab covers workbook experiments 13 and 14.
Workbook Alignment
- Experiment 13: Store student registration details in a database using JSP and Servlet
- Experiment 14: Insert, update, and delete student records using linked JSP pages and session management
Learning Objectives
By the end of this lab, you will be able to:
- create a registration form using JSP
- insert student data into a database through a servlet
- implement basic CRUD flow for student records
- protect pages using session validation
- understand redirect-based workflow in Java web applications
Software and Files Required
- completion of Lab 6
- Java 17 or later
- Apache Tomcat 10 or later
- MySQL 8 or later
- Maven or a Dynamic Web Project setup
- a project folder for JSP, servlet, and CRUD pages
Experiment 13: Registration Form with Database Insert
Problem Statement
Create a student registration form using JSP and Servlet, and store submitted data in a database.
Folder Structure
Lab07-StudentCRUD/
src/main/java/com/gcet/RegisterServlet.java
src/main/webapp/register.jsp
src/main/webapp/success.jsp
Procedure
- Use the same
bmc201_labdatabase created in the previous lab. - Create a
studentstable to store registration data. - Create
register.jspto collect student information. - Write
RegisterServlet.javato receive the form submission. - Insert the submitted values into the database using
PreparedStatement. - Redirect the user to a success page after insertion.
Database Setup
USE bmc201_lab;
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
course VARCHAR(30) NOT NULL
);register.jsp
<!DOCTYPE html>
<html>
<head>
<title>Student Registration</title>
</head>
<body>
<h1>Student Registration</h1>
<form action="register" method="post">
<input type="text" name="name" placeholder="Name" required>
<input type="email" name="email" placeholder="Email" required>
<input type="text" name="course" placeholder="Course" required>
<button type="submit">Save</button>
</form>
</body>
</html>RegisterServlet.java
package com.gcet;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");
String course = request.getParameter("course");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/bmc201_lab", "root", "root");
PreparedStatement statement = connection.prepareStatement(
"INSERT INTO students(name, email, course) VALUES (?, ?, ?)");
statement.setString(1, name);
statement.setString(2, email);
statement.setString(3, course);
statement.executeUpdate();
statement.close();
connection.close();
response.sendRedirect("success.jsp");
} catch (Exception exception) {
response.getWriter().println("Error: " + exception.getMessage());
}
}
}Expected Result
After submitting the registration form, the student record should be inserted into the database and the browser should open a success page.
Experiment 14: CRUD with Session Management
Problem Statement
Create linked JSP pages that allow insert, update, delete, and view operations only for authenticated users.
Procedure
- Create a login mechanism that stores
usernamein the session. - Add a session check at the top of each protected JSP page.
- Create navigation links for insert, update, delete, and view operations.
- Write the JDBC code for each operation using prepared statements.
- Test the pages with and without an active session.
Session Logic
Use an authenticated session before allowing CRUD operations:
<%
if (session.getAttribute("username") == null) {
response.sendRedirect("login.jsp");
return;
}
%>Example Delete Query
PreparedStatement statement = connection.prepareStatement(
"DELETE FROM students WHERE id = ?"
);
statement.setInt(1, Integer.parseInt(request.getParameter("id")));
statement.executeUpdate();Expected Result
Only authenticated users should be able to open the CRUD pages, and each database operation should update the student records correctly.
Observation Questions
- What is session management in JSP and Servlet applications?
- Why is a redirect used after successful insert?
- What is the difference between forward and redirect?
- How can SQL injection be prevented in CRUD applications?
Viva Questions
- Why is session checking required on every protected page?
- What will happen if the session expires?
- Why is
PreparedStatementpreferred in insert, update, and delete queries? - How can you display all student records in
view.jsp?
Submission Checklist
register.jspRegisterServlet.javalogin.jspdashboard.jspinsert.jsp,update.jsp,delete.jsp, andview.jsp- screenshots of registration success and CRUD operations
- short answer file for observation and viva questions
Extension Task
Add logout support that invalidates the session and returns the user to the login page.