TU BCA Advanced Java Master Guide: Servlets, JSP, Hibernate ORM & Spring Boot REST API
Author: Bhuban Subedi | Subject: Advanced Java Programming (CACS354) | Semester: Sixth Semester
Enterprise banking backends, payment switches, and high-load web applications predominantly rely on the Java Enterprise ecosystem. In the Tribhuvan University BCA sixth semester, Advanced Java Programming (CACS354) elevates students from desktop Swing development into enterprise distributed backends: Java Servlets, JSP (JavaServer Pages), Hibernate ORM, and Spring Boot RESTful microservices.
In the final 60-mark TU board examination and the 40-mark external laboratory examination, examiners consistently test the Servlet Lifecycle, Session Tracking techniques, JSP Model-View-Controller (MVC) architecture, and building Spring Boot @RestController APIs.
In this guide, I will provide production-ready Java code and architectural patterns.
1. The Java Servlet Lifecycle
A Servlet is a Java class running on a web container (e.g., Apache Tomcat) that receives HTTP requests and generates dynamic responses.
[Client HTTP Request]
│
▼
[Servlet Instance Loaded in Tomcat]
│
▼
init() ───► (Executed ONCE on first request)
│
▼
service() ───► (Dispatches to doGet() or doPost())
│ (Executed for EVERY request in a thread)
▼
destroy() ───► (Executed ONCE during server shutdown)
2. Java Servlet Implementation: User Authentication
package np.com.sbhuwan.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String user = request.getParameter("username");
String pass = request.getParameter("password");
if ("bhuban".equals(user) && "tu_bca_2026".equals(pass)) {
// Establish an HTTP Session
HttpSession session = request.getSession();
session.setAttribute("user", user);
session.setMaxInactiveInterval(30 * 60); // 30 mins timeout
response.sendRedirect("dashboard.jsp");
} else {
out.println("<h3 style='color:red;'>Invalid Username or Password!</h3>");
request.getRequestDispatcher("login.html").include(request, response);
}
}
}
3. Hibernate ORM Entity & JPA Annotations
Hibernate maps Java POJO classes directly to relational SQL tables using JPA annotations:
package np.com.sbhuwan.entities;
import javax.persistence.*;
@Entity
@Table(name = "bca_students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "student_id")
private Long id;
@Column(name = "full_name", nullable = false, length = 100)
private String fullName;
@Column(name = "email", unique = true, nullable = false)
private String email;
@Column(name = "gpa")
private Double gpa;
// Constructors, Getters, and Setters
public Student() {}
public Student(String fullName, String email, Double gpa) {
this.fullName = fullName;
this.email = email;
this.gpa = gpa;
}
public Long getId() { return id; }
public String getFullName() { return fullName; }
public void setFullName(String fullName) { this.fullName = fullName; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public Double getGpa() { return gpa; }
public void setGpa(Double gpa) { this.gpa = gpa; }
}
4. Modern Spring Boot REST API Controller
package np.com.sbhuwan.controllers;
import np.com.sbhuwan.entities.Student;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
@RestController
@RequestMapping("/api/v1/students")
@CrossOrigin(origins = "*")
public class StudentRestController {
// GET: /api/v1/students
@GetMapping
public ResponseEntity<List<Student>> getAllStudents() {
List<Student> students = Arrays.asList(
new Student("Bhuban Subedi", "[email protected]", 3.92),
new Student("Aayush Adhikari", "[email protected]", 3.80)
);
return ResponseEntity.ok(students);
}
// POST: /api/v1/students
@PostMapping
public ResponseEntity<String> registerStudent(@RequestBody Student student) {
// Business logic & DB save via Spring Data JPA Repository
return new ResponseEntity<>("Student registered successfully!", HttpStatus.CREATED);
}
}
Frequently Asked Questions (FAQ)
Q1: What are the 4 main Session Tracking mechanisms in Java Web?
- Cookies (Client-side key-value pairs).
- HttpSession API (Server-side session storage with
JSESSIONID). - URL Rewriting (Appending
;jsessionid=xyzto URLs). - Hidden Form Fields (
<input type="hidden" name="session_id" ...>).
Q2: What is Inversion of Control (IoC) in Spring Framework?
Inversion of Control is a software design pattern where the framework container manages object creation, configuration, and lifecycle injection (@Autowired) instead of developers instantiating dependencies manually via new.



