Advance Java Question Bank Sol
Advance Java Question Bank Sol
Unit – 1
Ans.1:
In Java, TCP/IP client sockets are typically implemented
using the java.net.Socket class. This class represents a
client-side socket that can be used to establish connections
to servers over TCP/IP networks. Below is an explanation of
TCP/IP client sockets and their methods in Java:
Socket Creation: To create a TCP/IP client socket in Java, you
typically instantiate a Socket object. This is done using one
of the constructors provided by the Socket class.
For example: java Copy code
Socket socket = new Socket();
Connection Establishment: After creating the socket object,
you need to establish a connection to the server. This is
achieved by calling the connect() method, passing in an
instance of SocketAddress representing the server's
address.
For example: java Copy code
SocketAddress serverAddress = new
InetSocketAddress("serverIPAddress", portNumber);
socket.connect(serverAddress);
Data Transmission: Once the connection is established, you
can send data to the server using the output stream
obtained from the socket. This is typically done using the
getOutputStream() method to retrieve an OutputStream
object, which can be used to write data to the server.
For example: java Copy code
OutputStream outputStream = socket.getOutputStream();
outputStream.write(dataBytes);
Data Reception: Similarly, you can receive data from the
server using the input stream obtained from the socket.
This is typically done using the getInputStream() method to
retrieve an InputStream object, which can be used to read
data from the server. For example: java Copy code
InputStream inputStream = socket.getInputStream(); byte[]
buffer = new byte[1024]; int bytesRead =
inputStream.read(buffer);
Connection Termination: When the client no longer needs
the connection, it can close the socket to release any
allocated resources. This is done by calling the close()
method on the socket.
For example: java Copy code
socket.close();
Here's a summary of the methods associated with TCP/IP
client sockets in Java:
Socket(): Creates a new socket object.
connect(SocketAddress address): Connects the socket to
the specified server address.
getOutputStream(): Returns an output stream for writing
data to the server.
getInputStream(): Returns an input stream for reading data
from the server.
close(): Closes the socket and releases any allocated
resources.
Ans.2:
In Java, TCP/IP server sockets are typically implemented
using the java.net.ServerSocket class. This class represents a
server-side socket that listens for incoming connections
from clients over TCP/IP networks. Below is an explanation
of TCP/IP server sockets and their methods in Java:
Socket Creation: To create a TCP/IP server socket in Java,
you instantiate a ServerSocket object. This is done using
one of the constructors provided by the ServerSocket class,
typically specifying the port number on which the server
will listen for incoming connections.
For example: java Copy code
ServerSocket serverSocket = new
ServerSocket(portNumber);
Connection Acceptance: After creating the server socket, it
starts listening for incoming connection requests from
clients. This is achieved by calling the accept() method on
the server socket. The accept() method blocks until a client
connection request is received, at which point it returns a
new Socket object representing the client connection.
For example: java Copy code
Socket clientSocket = serverSocket.accept();
Data Transmission: Once a client connection is accepted,
you can send data to the client using the output stream
obtained from the client socket. This is typically done using
the getOutputStream() method to retrieve an
OutputStream object, which can be used to write data to
the client.
For example: java Copy code
OutputStream outputStream =
clientSocket.getOutputStream();
outputStream.write(dataBytes);
Data Reception: Similarly, you can receive data from the
client using the input stream obtained from the client
socket. This is typically done using the getInputStream()
method to retrieve an InputStream object, which can be
used to read data from the client. For example: java Copy
code
InputStream inputStream = clientSocket.getInputStream();
byte[] buffer = new byte[1024]; int bytesRead =
inputStream.read(buffer);
Connection Termination: When the server no longer needs
the client connection, it can close the client socket to
release any allocated resources. This is done by calling the
close() method on the client socket. For example: java Copy
code
clientSocket.close();
Server Socket Termination: When the server no longer
needs to accept new connections, it can close the server
socket to release any allocated resources. This is done by
calling the close() method on the server socket.
For example: java Copy code
serverSocket.close();
Here's a summary of the methods associated with TCP/IP
server sockets in Java:
ServerSocket(int port): Creates a new server socket object
and binds it to the specified port number. accept(): Listens
for incoming connection requests and returns a new socket
object representing the client connection.
getOutputStream(): Returns an output stream for writing
data to the client.
getInputStream(): Returns an input stream for reading data
from the client.
close(): Closes the server socket or client socket and
releases any allocated resources.
Ans.3:
Ans.4:
Ans 5:
The InetAddress class in Java networking is used to
represent IP addresses and perform domain name
resolution. :
Purpose: The InetAddress class is used to represent both
IPv4 and IPv6 addresses and to perform
hostname-to-IP address resolution and vice versa.
Functionality:
• Provides methods for creating instances representing
IP addresses and hostnames.
• Supports methods for querying hostname
information from an IP address and vice versa.
• Offers utility methods for checking reachability and
getting the hostname and IP address of the
• local machine.
Key Methods:
• getByName(String host): Returns an InetAddress
instance representing the specified hostname
• or IP address.
• getHostAddress(): Returns the IP address string in
textual presentation.
• getHostName(): Returns the fully qualified domain
name for this IP address.
Usage:
• Used in networking applications to resolve
hostnames to IP addresses and vice versa.
• Facilitates communication between networked
devices by converting between hostnames and IP
• addresses.
Example :
import java.io.*;
import java.net.*;
public class InetAddress_Example
{
public static void main(String args[])
{
Try
{
InetAddress
ip=InetAddress.getByName("https://cutshort.io/resources/
case-studies");
System.out.println("Host Name: "+ip.getHostName());
System.out.println("IP Address: "+ip.getHostAddress());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Ans.6:
URL Class :
The URL class in Java networking is used to represent and
manipulate Uniform Resource Locators
(URLs).
Purpose: The URL class is used to parse, construct, and
represent URLs, which are addresses used to
locate resources on the internet or within a network.
Functionality:
Parses URLs into their components such as protocol, host,
port, path, query parameters, and
fragment.
Constructs URLs from their individual components.
Provides methods for accessing and manipulating
different parts of the URL.
Key Methods:
URL(String spec): Constructor for creating a URL object from
a string representation of the URL.
openConnection(): Opens a connection to the URL and
returns a URLConnection object for further communication.
String getProtocol(), String getHost(), int getPort(), String
getPath(), etc.: Methods to retrieve specific components of
the URL.
String toString(): Returns the string representation of the
URL.
Usage:
Used in web development, networking, and various
applications requiring URL handling. Allows developers to
work with URLs, access resources over the internet, and
establish connections to
remote servers.
Example :
import java.net.*;
public class URL_Example
{
public static void main(String args[])
{
Try
{
URL url=new
URL("https://cutshort.io/blog/category/career-advice");
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
System.out.println("File Name: "+url.getFile());
}
catch(Exception e)
{
E.printStackTrace();
}
}
}
URLConnection Class :
The URLConnection class in Java networking facilitates
communication with a remote resource
identified by a URL.
Purpose: URLConnection represents a communication link
between the application and a URL forreading from or
writing to the resource referenced by the URL.
Functionality:
Provides an abstract base class for specific protocol
implementations (e.g., HTTP, HTTPS, FTP)
to handle communication with remote servers.
Supports various types of connections, including input
and output streams, for reading from and
writing to the resource.
Key Methods:
InputStream getInputStream(): Returns an input stream
that reads from the resource referenced by
the URL.
OutputStream getOutputStream(): Returns an output
stream that writes to the resource referenced
by the URL.
void connect(): Establishes the connection to the remote
resource.
void setRequestProperty(String key, String value): Sets a
general request property for the
connection.
Usage:
Utilized in applications requiring interaction with remote
resources identified by URLs, such as
web scraping, file downloading, or accessing web APIs.
Allows developers to retrieve data from web servers or
upload data to remote servers programmatically.
Example :
import java.io.*;
import java.net.*;
public class URLConnection_Example
{
public static void main(String args[])
{
Try
{
URL U1=new
URL("https://cutshort.io/blog/category/hiring");
URLConnection U2=U1l.openConnection();
InputStream IS=U2.getInputStream();
int i;
while((i=IS.read())!=-1)
{
System.out.print((char)i);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
Ans.7 and 8:
Remote Method Invocation :
RMI (Remote Method Invocation) architecture in Java
networking allows objects residing on one Java
Virtual Machine (JVM) to invoke methods on objects
residing on another JVM.
Purpose: RMI facilitates communication between
distributed Java applications, enabling remote
method calls between objects in different JVMs.
Usage:
• Used in distributed applications where components
need to communicate across network boundaries.
• Facilitates building client-server applications,
distributed systems, and remote services in Java.
In an RMI application, we write two programs, a server
program (resides on the server) and a client program
(resides on the client).
Inside the server program, a remote object is created and
reference of that object is made available for the client
(using the Registry).
The client program requests the remote objects on the
server and tries to invoke its methods.
The following diagram shows the architecture of an RMI
application.
Let us now discuss the components of this architecture.
• Transport Layer − This layer connects the client and the
server. It manages the existing connection and also sets
up new connections.
• Stub − A stub is a representation (proxy) of the remote
object at client. It resides in the client
• system; it acts as a gateway for the client program.
Skeleton − This is the object which resides on the
server side. stub communicates with this
• skeleton to pass request to the remote object.
RRL(Remote Reference Layer) − It is the layer which
manages the references made by the
client to the remote object.
Working of an RMI Application :
• When the client makes a call to the remote object, it is
received by the stub which eventually passes this
request to the RRL.
• When the client-side RRL receives the request, it
invokes a method called invoke() of the object
• remoteRef. It passes the request to the RRL on the
server side.
• The RRL on the server side passes the request to the
Skeleton (proxy on the server) which finally
• invokes the required object on the server.
• The result is passed all the way back to the client.
Unit-2
Ans.10
Definition of JDBC(Java Database Connectivity) JDBC is an
API(Application programming interface) used in Java
programming to interact with databases. The classes and
interfaces of JDBC allow the application to send requests
made by users to the specified database. Components of
JDBC There are generally four main components of JDBC
through which it can interact with a database. They are as
mentioned below:
1. JDBC API: It provides various methods and interfaces for
easy communication with the database. It provides two
packages as follows, which contain the java SE and Java EE
platforms to exhibit WORA(write once run anywhere)
capabilities. It also provides a standard to connect a
database to a client application.
2. JDBC Driver manager: It loads a database-specific driver
in an application to establish a connection with a database.
It is used to make a database-specific call to the database to
process the user request.
3. JDBC Test suite: It is used to test the operation ( such as
insertion, deletion, updation) being performed by JDBC
Drivers.
4. JDBC-ODBC Bridge Drivers: It connects database drivers
to the database. This bridge translates the JDBC method call
to the ODBC function call. It makes use of the sun.jdbc.odbc
package which includes a native library to access ODBC
characteristics.
Ans.11:
Ans.12:
Ans.13:
Ans.14:
Ans.15:
Ans.18:
Ans.19:
Ans.20:
Ans.22:
Ans.23:
Ans.25:
Ans.26:
Ans.27:
Ans.28:
Ans.29:
Ans.31:
https://www.geeksforgeeks.org/httpservlet-class-in-java/
Ans.32
Ans.33:
Ans.34:
Ans.36:
Ans.37:
Ans.38:
Index.jsp
Ans.39:
https://www.geeksforgeeks.org/jsp-architecture/
Ans.40:
Ans.41:
https://www.geeksforgeeks.org/life-cycle-of-jsp/
Ans.42:
Ans.43:
Ans.44:
https://www.geeksforgeeks.org/jsp-scriptlettag/
Ans.45:
Ans.47
Ans.48: