Java
Java
b) Explain the interfaces used in JDBC with example. b) Write a program create connection in JDBC.
JDBC provides several key interfaces for database import [Link].*;
operations: public class JDBCConnection {
DriverManager: Manages a list of database drivers and public static void main(String[] args) {
establishes a connection to the database. try {
Connection: Represents a session with a specific database, // Load and register JDBC driver
allowing SQL statement execution. [Link]("[Link]");
Statement: Used to execute static SQL queries. // Create connection object
PreparedStatement: Used for executing precompiled SQL Connection con = [Link](
queries with parameters. "jdbc:mysql://localhost:3306/mydatabase",
ResultSet: Represents the result set of a query and allows "username", "password");
data retrieval. [Link]("Connection Established
CallableStatement: Used to execute stored procedures. Successfully.");
Example: Using JDBC Interfaces // Close connection
import [Link].*; [Link]();
public class JdbcExample { } catch (Exception e) {
public static void main(String[] args) throws Exception { [Link]();
// Load driver (optional in modern Java) }
// [Link]("[Link]"); }
Connection con = [Link]( }
"jdbc:mysql://localhost:3306/mydb", "user",
"password");
Statement stmt = [Link]();
Q9) a) Explain various va Networking Terminologies.
1) IP Address (Internet Protocol Address): A unique Q10) a) What is Datagram? Write a note on Datagram
numerical label assigned to each device connected to a Server & Client.
network. It identifies the device’s location on the network Datagram: A datagram is a basic transfer unit associated
and allows communication between devices. with a connectionless packet-switched network, such as the
2) Port Number: A logical endpoint in network User Datagram Protocol (UDP).
communication that identifies a specific process or service It is a self-contained, independent packet of data that
on a device. For example, port 80 is typically used for HTTP carries enough information to be routed from the source to
traffic. the destination without relying on earlier exchanges.
3) Protocol: A set of rules and standards that define how Unlike TCP, datagrams do not guarantee delivery, order, or
data is transmitted and received over a network. Examples error-checking — making them faster but less reliable.
include TCP (Transmission Control Protocol), UDP (User Datagram Server:
Datagram Protocol), and HTTP (HyperText Transfer A Datagram Server listens on a specific port for incoming
Protocol). datagram packets.
4) Socket: An endpoint for communication between two It receives datagrams sent by clients, processes them, and
machines. A socket is identified by an IP address and a port can send a response back.
number, allowing data to be sent and received. Since UDP is connectionless, the server does not establish a
5) DNS (Domain Name System): A system that translates persistent connection with clients.
human-readable domain names (like [Link]) into Datagram Client:
IP addresses, making it easier to access websites. A Datagram Client sends datagram packets to the server’s
6) MAC Address (Media Access Control Address): A unique IP address and port.
identifier assigned to a network interface card (NIC) used The client creates a datagram packet with data and
for communication on the physical network segment. It destination address and sends it using a DatagramSocket.
operates at the data link layer.
7) LAN (Local Area Network): A network that connects
computers within a limited area such as a home, office, or b) Explain any three classes in [Link] package.
building. The [Link] package provides classes and interfaces for
implementing networking applications in Java. Below are
three important classes commonly used:
1. InetAddress Class
Represents an IP address (either IPv4 or IPv6).
b) What is URL? Explain various URL connection Class Used to get IP addresses or host names.
Methods. Common Methods:
URL (Uniform Resource Locator) is a reference (address) getByName(String host): Returns the IP address of the given
used to access resources on the internet. host.
It specifies the location of a resource as well as the protocol getHostName(): Returns the host name.
to access it. getHostAddress(): Returns the IP address as a string.
Example: [Link] Example: InetAddress address =
Various Methods of the URLConnection Class [Link]("[Link]");
URLConnection is a Java class used to represent a [Link]([Link]());
connection to a URL. It provides methods to interact with 2. Socket Class
the resource referred to by the URL. Used for client-side TCP communication.
connect() : Establishes a connection to the resource pointed It establishes a connection to a server over a specific port.
to by the URL. Common Methods:
getInputStream() : Returns an InputStream to read data getInputStream(): Reads data from the server.
from the URL resource. getOutputStream(): Sends data to the server.
getOutputStream(): Returns an OutputStream to send data close(): Closes the socket.
to the URL resource (used mainly for HTTP POST). Example: Socket socket = new Socket("localhost", 1234);
getContentLength() : Returns the length (in bytes) of the 3. ServerSocket Class
content received from the resource. Used for server-side TCP communication.
getContentType(): Returns the MIME type of the content It listens for incoming connection requests on a specified
(e.g., text/html, image/png). port.
getHeaderField(String name): Returns the value of a specific Common Methods:
HTTP header field from the resource. accept(): Waits for and accepts a connection from a client.
close(): Closes the server socket.
Example: ServerSocket server = new ServerSocket(1234);
Socket client = [Link]();
Q11). a) Write a note on Life cycle of Servlet Q12)a) Write a difference between Servlet & JSP.
The Servlet Life Cycle defines the process by which a servlet Servlet JSP
is created, initialized, serves requests, and is eventually
destroyed. The Servlet container (like Tomcat) manages this A text-based document
life cycle using three main methods of the A Java class that handles HTTP
combining HTML and
[Link] interface: requests and responses.
Java code.
1) init() Method : Called once when the servlet is first
loaded into memory. Used to perform initialization tasks
like opening database connections or loading configuration Pure Java code written using HTML with embedded
settings. classes and methods. Java code using JSP tags.
Signature: public void init(ServletConfig config) throws
ServletException Handling business logic and Presentation logic (user
2. service() Method : Called each time the servlet receives control flow. interface).
a request. Handles client requests and generates responses.
It is the core method where most logic (processing data, Code-heavy; separates HTML HTML-heavy; Java code is
calling business logic, sending responses) is written. using print statements. embedded in HTML.
Signature: public void service(ServletRequest req,
ServletResponse res) Requires writing a lot of code Easier for web designers;
3. destroy() Method : Called once when the servlet is being even for simple output. looks like an HTML page.
taken out of service e.g., during server shutdown. Used to
release resources like closing database connections, Directly compiled as a servlet Translated into a servlet,
stopping threads, etc. class. then compiled.
Signature: public void destroy()
Life Cycle Flow Summary:
Servlet is loaded → Servlet container loads the servlet class. b) Explain the architecture of Servlet
init() is called → Initializes the servlet. Servlet Architecture refers to the internal working and flow
service() is called for every request → Processes client of a servlet in a web application. A Servlet is a Java program
requests. that runs on a server and responds to client requests,
destroy() is called → Cleans up before servlet is removed usually over HTTP.
from memory. Servlets follow a request-response model and are managed
by a Servlet Container (like Apache Tomcat), which is part
b) Explain the architecture of JSP of a web server.
JSP (JavaServer Pages) is a server-side technology that Key Components of Servlet Architecture:
allows the creation of dynamic web pages using HTML and Client (Web Browser): Sends an HTTP request
Java code. The architecture of JSP defines how a JSP file is Web Server (Servlet Container): Receives the request from
processed and executed by the server. the client and forwards it to the appropriate servlet.
Key Components of JSP Architecture: Servlet Class: The core Java class that processes the
Client (Web Browser): Sends an HTTP request for a .jsp file request. Implemented by extending HttpServlet and
to the web server. overriding methods like doGet() or doPost().
Web Server / JSP Engine (Servlet Container): The JSP engine Servlet Request & Response Objects: HttpServletRequest
is part of a web server (like Apache Tomcat) that handles carries information from the client (headers, parameters).
JSP files. Servlet Life Cycle Methods:
Translation Phase: The JSP file is translated into a Java init() → Called once when the servlet is loaded.
servlet by the JSP engine. This servlet contains Java code service() → Called for each client request.
equivalent to the content and logic written in the JSP file. destroy() → Called when the servlet is removed from
Compilation Phase: The translated servlet is compiled into a memory.
.class file (bytecode) by the Java compiler. Architecture Flow (Step-by-Step):
Class Loading and Initialization: The servlet class is loaded Client sends request → via browser
into memory by the servlet container and initialized using Web server receives it and passes it to the Servlet
the init() method. Container.
Diagram :Client (Browser) Servlet Container checks if the servlet is already loaded:
v If not loaded: it loads the class, creates an instance, and
HTTP Request --> JSP Engine (Web Server) calls init().
| The container then calls the service() method, which
Translate JSP to Servlet -- Compile Servlet to Class internally calls:
| doGet() for GET requests or doPost() for POST requests.
Load and Execute Servlet Servlet processes the request, generates a response using
| HttpServletResponse.
Generate HTML Response The response is sent back to the client (e.g., HTML content).
| On server shutdown, destroy() is called to clean up
Client (Displays Output) resources.