Q.2) Answer the following.
(2marks)
a) What is Entity Bean?
An Entity Bean is a server-side component in Enterprise Java Beans (EJB) that represents a persistent object stored
in a database.
It is mainly used to map Java objects to relational database tables, allowing automatic storage and retrieval of data
without writing complex SQL queries.
b) What are the advantages of servlet over CGI?
1. Better performance – Servlets run inside a single JVM and are faster than CGI.
2. Platform independent – Written in Java, so runs on any system with JVM.
3. Reusable and portable code.
c) What is the use of JDBC API?
JDBC (Java Database Connectivity) API is used to connect Java applications to relational databases like MySQL,
Oracle, or PostgreSQL.
In simple terms: JDBC allows Java programs to perform CRUD (Create, Read, Update, Delete) operations easily with
databases.
d) What is JSP Page Directive?
JSP Page Directive is used to provide instructions to the JSP compiler.
Example: <%@ page language="java" contentType="text/html" %>
It controls page-level settings like error page, import classes, and buffering.
e) What are the advantages of Spring?
1. Lightweight and modular.
2. Supports Dependency Injection (DI) for easy object management.
3. Easy integration with Hibernate, JDBC, and other frameworks.
f) What are the JSTL core Tags?
Some common JSTL core tags are:
<c:out> – for output
<c:if> – for conditional checking
<c:choose>, <c:when>, <c:otherwise> – for decision making
<c:forEach> – for looping
g) What are the features of Spring Framework?
1. Dependency Injection (DI) and Inversion of Control (IoC)
2. Modular and lightweight
3. Integration with other Java frameworks
4. Transaction management and MVC support
h) Explain Dependency Injection (DI) and its types.
Dependency Injection (DI) is a design pattern where objects get their dependencies from external sources rather
than creating them themselves.
Types:
1. Constructor Injection
2. Setter Injection
i) What is Collection Mapping in Hibernate?
Collection Mapping in Hibernate is used to map Java collection types (like List, Set, Map, Bag) to database tables or
relationships.
For example:
A Set of phone numbers or
A List of subjects for a student
can be stored in separate database tables linked to the main entity.
j) What are the advantages of JSTL?
1. Simplifies JSP code by removing Java code from JSP pages.
2. Improves readability and maintenance.
3. Provides standard reusable tags for looping, conditions, and SQL operations.
Q.3) A) Attempt the following. (5marks)
a) What is Servlet? What is the use of Servlet? How servlet is loaded?
1. What is Servlet:
A Servlet is a Java program that runs on a web server and is used to create dynamic web pages.
It acts as a middle layer between the client (browser) and the database or application logic on the server.
2. Use of Servlet:
To process client requests (like form data).
To generate dynamic web content.
To interact with databases and other resources.
To handle business logic of web applications.
3. How Servlet is loaded:
When a servlet is requested for the first time, the Web Container (like Apache Tomcat) loads it.
The container:
1. Loads the servlet class into memory.
2. Creates an instance of the servlet.
3. Calls init() method to initialize the servlet.
4. Then it handles requests through the service() method.
In short:
Servlet is loaded once, stays in memory, and handles multiple requests efficiently.
b) Explain TPH (Table Per Hierarchy) inheritance mapping with example.
Definition:
In Hibernate, inheritance mapping is used to define how Java class inheritance (like superclass–subclass) is
represented in the database tables.
One common strategy is Table Per Hierarchy (TPH), also known as Single Table Inheritance.
In this strategy, a single database table is created for all the classes in the inheritance hierarchy.
The table includes columns for all fields from the superclass and its subclasses.
To differentiate between subclasses, Hibernate uses a discriminator column that stores a value (like “Car”, “Bike”,
etc.) representing the class type.
How It Works:
1. All attributes of the base class and derived classes are stored in one table.
2. A discriminator column (special column) identifies which subclass a particular row belongs to.
3. If a subclass does not use some columns, those cells remain NULL in that row.
Advantages:
1. Simple and easy to implement — only one table for all related classes.
2. Better performance because only one table is queried.
3. Easy to maintain and manage relationships.
Disadvantages:
1. Wasted space — many NULL values for unused columns.
2. Difficult to enforce constraints specific to subclasses.
3. Not suitable when subclass structures differ greatly.
c) Explain the life cycle of JSP.
The JSP (Java Server Page) life cycle defines how a JSP page is created, processed, and destroyed by the server.
Phases of JSP Life Cycle:
1. Translation Phase:
o The JSP file is translated into a Servlet class by the JSP engine.
2. Compilation Phase:
o The generated servlet is compiled into a .class file.
3. Initialization Phase (jspInit()):
o The container calls the jspInit() method once when the JSP is loaded to initialize resources.
4. Execution Phase (_jspService()):
o This method is called every time a request is made to the JSP page.
o It contains the main logic to handle requests and generate responses.
5. Destroy Phase (jspDestroy()):
o The container calls jspDestroy() when the JSP is removed from memory to release resources.
Diagram (for writing in exam):
JSP Page
Translation → Compilation → Initialization → Execution → Destruction
In short:
JSP is first converted into a servlet, compiled, executed for requests, and destroyed when the server stops.
B) Attempt the following. (6marks)
Expression, Declaration, and Scriptlet in JSP
In JSP (Java Server Pages), Java code can be written inside special tags.
These tags allow us to add dynamic content and logic inside HTML pages.
The three main types of scripting elements are:
1. Expression Tag (<%= %>)
Definition:
The Expression tag is used to output the result of an expression directly to the web page.
Syntax:
<%= expression %>
Example:
<%= "Welcome " + request.getParameter("user") %>
Explanation:
Whatever is written inside <%= %> is evaluated and displayed on the browser.
You don’t need to use out.print() here.
Use:
Used to print values like variables, results, or function outputs on the web page.
2. Declaration Tag (<%! %>)
Definition:
The Declaration tag is used to declare variables and methods that can be used throughout the JSP page.
Syntax:
<%! declaration %>
Example:
<%! int count = 0;
String greetUser(String name) {
return "Hello " + name;
%>
Explanation:
Code inside <%! %> goes outside the _jspService() method in the generated servlet.
Used for member variable declarations and methods that need to be shared across requests.
Use:
To define reusable code, functions, or initialization logic in JSP.
3. Scriptlet Tag (<% %>)
Definition:
A Scriptlet tag allows you to write normal Java code (like loops, if-else, etc.) inside a JSP page.
Syntax:
<% java code %>
Example:
<%
int n = 5;
for(int i=1; i<=n; i++) {
out.println("Number: " + i + "<br>");
%>
Explanation:
The code inside <% %> is placed inside the _jspService() method of the servlet created by the JSP engine.
You can access JSP implicit objects like request, response, session, out, etc.
Use:
To perform logic or computations that need to be executed before displaying data.
In short:
Expression → Used for displaying values.
Declaration → Used for defining variables/methods.
Scriptlet → Used for writing logic or control statements.
Q.4) A) Answer the following. (4marks)
b) Explain implicit objects in JSP.
Definition:
Implicit objects are predefined objects provided by the JSP container that can be used directly in JSP pages without
any declaration.
They make it easy to access request data, session information, output stream, and configuration details.
List of JSP Implicit Objects:
Object Description
request Used to get data from the client (like form input).
response Used to send data back to the client.
out Used to print output to the web page.
session Used to store user-specific data across multiple requests.
application Used to store data for all users (global scope).
pageContext Provides access to all other implicit objects.
config Used to access servlet configuration data.
page Refers to the current JSP page (like this keyword).
exception Used to handle exceptions (only in error pages).
Example:
<%= "Welcome " + request.getParameter("username") %>
c) Explain JSTL SQL Tags with example
Definition:
JSTL SQL tags are part of the JavaServer Pages Standard Tag Library (JSTL).
They are used to perform database operations (like connecting, querying, and updating) directly from JSP without
writing Java code.
Common JSTL SQL Tags:
Tag Description
<sql:setDataSource> Sets the database connection.
<sql:query> Executes a SELECT query.
<sql:update> Executes INSERT, UPDATE, or DELETE.
<sql:param> Sets parameters in a query.
B) Attempt the following. (8marks)
Explain the types of JDBC Drivers.
Definition:
JDBC (Java Database Connectivity) drivers are software components that help Java applications connect and interact
with databases.
They translate Java database requests into commands that the database can understand.
There are four types of JDBC drivers, also known as JDBC driver types 1, 2, 3, and 4.
1⃣ Type 1 – JDBC-ODBC Bridge Driver
Converts JDBC calls to ODBC calls.
Uses ODBC driver to connect to database.
Example: sun.jdbc.odbc.JdbcOdbcDriver
Advantage: Easy to use.
Disadvantage: Slow and platform dependent. (Removed in Java 8)
2⃣ Type 2 – Native API / Partly Java Driver
Converts JDBC calls into native database API calls (like Oracle C libraries).
Example: Oracle OCI driver.
Advantage: Faster than Type 1.
Disadvantage: Platform dependent (needs native library).
3⃣ Type 3 – Network Protocol / Middleware Driver
Uses middleware server to translate JDBC calls to database-specific protocol.
Advantage: Platform independent, no client library needed.
Disadvantage: Needs middleware, slightly slower.
4⃣ Type 4 – Thin Driver / Pure Java Driver
Written in pure Java, communicates directly with database.
Example: com.mysql.cj.jdbc.Driver, oracle.jdbc.driver.OracleDriver.
Advantage: Fast, platform independent, most widely used.
Disadvantage: Each database needs its own driver.
Q.5) Answer the following. (8marks)
a) What is Spring Framework? Explain the architecture of Spring Framework with suitable diagram.
Definition:
Spring Framework is a lightweight, open-source Java framework used to build enterprise-level applications easily.
It provides inversion of control (IoC) and dependency injection (DI) features, helping developers manage objects and
their dependencies efficiently.
Architecture of Spring Framework:
Spring Framework is made up of seven well-defined layers (modules).
Each layer provides different functionalities.
Main Modules:
1. Core Container:
o Contains Core, Beans, Context, and Expression Language (SpEL) modules.
o Provides the Dependency Injection (DI) and BeanFactory functionality.
2. Data Access / Integration Layer:
o Includes JDBC, ORM (Hibernate, JPA), Transactions, and JMS support.
o Simplifies database access and transaction management.
3. Web Layer:
o Provides the Spring MVC (Model-View-Controller) framework for web applications.
4. AOP (Aspect-Oriented Programming):
o Supports cross-cutting concerns like logging, security, and transactions.
5. Test Module:
o Provides support for testing Spring components using JUnit or TestNG.
In short:
Spring Framework follows a modular architecture, allowing developers to use only the parts they need.
It is widely used for building secure, modular, and maintainable enterprise applications.
b) Explain Hibernate Annotation with suitable example.
Definition:
Hibernate Annotations are metadata added to Java classes, methods, or fields to define object-relational mapping
(ORM) directly in the source code — instead of using XML configuration files.
Common Hibernate Annotations:
Annotation Description
@Entity Marks a class as a Hibernate entity (mapped to a table).
@Table(name="table_name") Specifies the table name.
@Id Defines the primary key.
@GeneratedValue Specifies auto-generation of IDs.
@Column(name="column_name") Maps a class field to a table column.
Advantages of Annotations:
1. No need for XML mapping files.
2. Easier to read and maintain.
3. Code and database mapping stay together.
IMP QUESTIONS.
1⃣ Advantages of JSP over Servlet
JSP (Java Server Pages) is easier and more convenient than Servlets for web development.
Advantages:
1. Easier to write and maintain:
JSP allows mixing of HTML and Java code, while Servlets require complex out.println() statements.
2. Automatic translation:
JSP pages are automatically converted into Servlets by the container.
3. Separation of logic and design:
JSP focuses on presentation (HTML), while Servlets focus on business logic.
4. Implicit objects available:
JSP provides predefined objects like request, response, session, out, etc.
5. Custom tags and JSTL:
JSP supports tag libraries (JSTL) for cleaner and reusable code.
✅ In short: JSP is more designer-friendly and suitable for building dynamic web pages than Servlets.
2⃣ Hibernate Architecture
Definition:
Hibernate is an Object-Relational Mapping (ORM) framework that maps Java classes to database tables and handles
database operations automatically.
Main Components of Hibernate Architecture:
1. Configuration:
Reads configuration file (hibernate.cfg.xml) for DB settings and mappings.
2. SessionFactory:
Creates and manages multiple database sessions.
3. Session:
Represents a single connection with the database; used to perform CRUD operations.
4. Transaction:
Manages commit and rollback operations.
5. Query:
Used to fetch data using HQL (Hibernate Query Language).
6. Database:
The actual relational database where data is stored.
Simple Diagram:
Application
Configuration → SessionFactory → Session → Transaction → Database
✅ In short: Hibernate simplifies database operations by automatically converting Java objects to SQL tables.
3⃣ Spring Model
Definition:
Spring follows the MVC (Model-View-Controller) architecture used to design web applications efficiently.
Spring MVC Components:
1. Model:
Holds application data and business logic (Java classes, POJOs).
2. View:
Represents user interface — JSP, HTML, or Thymeleaf pages.
3. Controller:
Handles user requests, interacts with Model, and returns data to View.
Flow:
Client → Controller → Model → View → Client
Advantages:
Clear separation of logic and presentation.
Easy to test and maintain.
Reusable and modular components.
✅ In short: Spring MVC divides the app into Model, View, and Controller for better structure and reusability.
4⃣ Difference Between Generic Servlet and HTTP Servlet
Feature GenericServlet HttpServlet
Package javax.servlet javax.servlet.http
Protocol Protocol independent Works with HTTP protocol only
Methods Has only service() method Has doGet(), doPost(), etc.
Usage For any protocol (FTP, SMTP) For web applications
Example Custom network service Web apps handling HTTP requests
✅ In short:
GenericServlet is general-purpose; HttpServlet is specifically for web-based applications using HTTP.
5⃣ Inheritance Mapping in Hibernate
Definition:
Inheritance mapping defines how class inheritance (superclass and subclass) is represented in the database tables.
Types of Inheritance Mapping:
1. Table Per Hierarchy (Single Table):
All classes stored in one table using a discriminator column.
o Simple and fast but may create many NULL values.
2. Table Per Subclass (Joined Strategy):
Each subclass has its own table joined with parent table.
o Reduces NULLs but slower joins.
3. Table Per Concrete Class:
Each subclass has a separate table containing all its fields.
o No joins, but duplicate data.
✅ In short: Hibernate supports three strategies to store inherited class data into database tables.
6⃣ Session and Session Tracking Mechanisms
Session:
A session represents a period of interaction between a client (browser) and a web server.
It helps in storing user data (like login info) across multiple requests.
Session Tracking:
It is a way to maintain user data between multiple requests.
Session Tracking Mechanisms:
1. Cookies:
Small text files stored on client’s browser.
2. URL Rewriting:
Session ID added to the URL as a parameter.
3. Hidden Form Fields:
Hidden fields in HTML forms carry session data.
4. HTTPSession Object:
Java object used in Servlets/JSP to store and retrieve session data.
✅ In short: Session tracking keeps user data alive during multiple interactions with a website.
7️⃣ Spring JDBC
Definition:
Spring JDBC is a module in Spring Framework that simplifies database operations using JDBC.
Main Classes:
1. JdbcTemplate:
Core class that executes SQL queries easily.
2. DataSource:
Manages database connection pooling.
3. RowMapper:
Maps rows from a ResultSet to Java objects.
Example:
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.update("INSERT INTO student VALUES (?, ?)", 1, "Riya");
Advantages:
Less boilerplate code than standard JDBC.
Handles exceptions automatically.
Easier to manage transactions.
✅ In short: Spring JDBC helps in performing CRUD operations easily using JdbcTemplate and reduces repetitive
code.