Java Programming & Web Design Guide
Java Programming & Web Design Guide
received webpage, which is fully sending a datagram. The sender can simply
rendered and ready for viewing encapsula:e the data in a datagram and
transmit it to the destination.
Server-slde web pages are often used when
he content needs to be dynamically generated Fixed Maximum Size: Datagrans have a
Ased on user lnput, database queries, or other t:xed maximum size, determined by the
ikerer-side operations. network protocol or technology being used
transmitted
(e.g., IPv4, IPv6). If the data to bedivided
Eramples of server-side technologies include into
PHPR, Python with Django, Ruby on Rails, and Java exceeds this size. it needs to be
with Spring. multiple datagrams.
datagram
"Independent Processing: Each receiving
Section - B is processed independently by the
state information from
system, and no
(Short Answer Questions) previous datagrams is
retained.
fkAttempt
the
any Two question out of
following Three questions. Each Datagram Socket: network
-kestion carries 7.5 marks. datagram socket is
[7.5 x 3= 15] the Internet
communication endpoint used in and receive
06. What is Datagram ? Give its Protocol (UP) communication to send
haracteristics.
cket. Also, explain datagram datagramS.
Datagram sockets provide an interface
for
Aas, Datagram : to use
connectionless communicalion.
In omputer applications scenarios where reliaoility
suitable for
a datagram refers to making them requirement, and data can be
networking,
trafnsmi
contratiended,
independent unit of data that is not a strict
over a network It is a packet of
Semester/1Jave Programming & Dynamie Webpage Design / 2022 / 139
BCA Veh
is not handling
the exception itself, Q8. Discuss the key features supported
emethod handling the exception is
responsibility
thstthe
of by object Oriented programming
method or the caller of the languages.
d the to the calling
syntax for Msing "hrows" is as Ans. Object-Oriented Program ming :
delegated
method. The Object Oriented Programming or O0P is the
ollows. demnonstrate the working technique to create programs based on the real
program to world. Unlike procedural programming. the OOP
IJava keyword io exception handling
thros
[Link].:
programming model programs are organized around
objects and data rather than actions and logic.
import [Link].: Objects represent sorme concepts or things and like
public classGFG{ any other objects in the real Objects in programming
writeToFile() throws language have certain behavior, properties, type, and
aublic static void identity. In OOP based language the principal zim
Exception
is to find out the objects to manipulate and their
Buffered Writer relation between cach other. OOP offers greater
Buffered Writer bw = new
new FileWriter([Link]"): lexibility and compatibility and is popular in
[Link]("Test"); developing larger application. Another inportant
work in 0OP is totoclassify objects into different
behavior.
bw.close0: types according to their properties and
args) throws So OOP based software application development
public static void main(String(] includes the analysis of the problem, preparing a
Exception solution, coding and finally its maintenance.
{ Java is an object oriented programming and to
in Java we
try { understand the functionality of OOP
fundamentals
writeToFile); first need to understand several
related to objects. These include class, method,
inheritance. encapsulation, abstraction,
catch (Exception e) { polymorphism etc.
e.printStackTrace0:
Features of Object Oricnted Programming :
1. Emphasis is a data rather than procedure.
known as
2. Programs are divided into what are
Outpat objects. such that they
[Link]: access 3. Data structures are designed
enied (java. io. FilePermission" "[Link]" characterize the objects.
accessed by
"write" 4. Data is hidden and can't be
external functions.
at GFGwriteToFile(GFGjava:10) with cach other
In brief, throw is used to manually throw an 5. Objects may communicate
through functions.
ception within a method, while "throws" is used can be casily added
ethod signature to indicate that the method 6. New data and functions
hay throw certain checked exceptions, and the whenever necessary.
the data ofan object
esponsibility
ntothe ofhandling thosec exceptions is passed 7. Functions that operate ondata structure.
calling method. are tied together in the
in program
8. Follows bottom-up approach
design.
oGA Vth Semester / Java Programmlng &Dvnank Wubpage Design / 2022 /@ 134
than other driver types.
Section - C
(Detailed Answer Questions) . Not recommended for production use
Note : Attempt any Three questions out Tne 2: Native-API, Partly Java Drive
of the following Five
question carry 15 [Link]. Each
(15 x 3 = 45]
" This driver uses native code
libraries
(wTiIten
in the native language of the DBMe
Q9. Explain JDBC
application communicate with the database
architecture. What are the various types It requires the client-side libraries
of JDBC drivers. Write steps to
database with the web application using connect the database management system. specific o
Q11. What is Serviet? Explain its life cycle. Give its characteristic.
Ans. Servlet :
A 'Servlet' is a Java-based server-side component that dynamically generates and proca
web content. It runs on a web server and extends the capabilities of web servers to handle requs
responses in a way that allows dynamic content generation. Servlets are a key component of a
(Java Platform, Enterprise Edition) and are commonly used for building web applications.
Characteristics of Servlets:
reçue
Server- side Execution : Servlets run on the server-side, enabling them to handle client
and generate dynamic responses before sending the result back to the client.
Platforn Independence: Servlets are written in Java and are platform-independent. They
in on any server that supports the Java Servlet API, making them highly
Multithreading :Servlets
:8 are designedto handle multiple portable. multithrea
Yusing
requests concurrently
/ Java Programming &Dynamic Webpage Design /202n/n 132
v Semester
transrnited without the overhead of establishing and finally block (or both).
maintaining a connection. Catch: The catch block is used to haed
specific type of exception that may be
In Java, Datagram Socket is a class that
represents a datagram socket, allowing Java
thtown
within the corresponding try block. It cat
the exception and pertorms the necestan
applications to use UDP (User Dalagram
Protocol) for datagram-based communication. actions to recover from the exceptional
situation.
The DatagramSocket class provides methods for
sending and receiving datagrams over the network. . Finally: The finally block contains code tht
To send data using a Datagram Socket, thc will be executed regardless of whether a
application necds to create DatagramPacket exception occurred or not. lt is typically uset
containing the data to be sent, along with the to release resources or perform cleanun
destination IP address and port number. Then, the operations.
DatagramPacket is sent using the
Differentiate between throw" and "throw*
DatagramSocket's send) method. in Java :
To receive data, the application uses the
DatagramSocket's receive) method, which blocks Throw :
The "throw'" keyword is used to manually throw
until a datagram is received. The received data is
encapsulated in a DatagramPacket, which includes an exception from within a method ora block of
the sender's IP address and port number. code. It is used when we want to explicitly raise an
exception to indicate that an abnormal condition has
Q7. What is an Exception? How the occurred. The syntax for using "throw" sa
exception is handied in Java. follows:
DIfferentiate between throw and throws. IIJava program to demonstrate the working
Ans. Exception : of throw keyword in exception bandling
In programming, an exception is an public class GFG{
MNexpecled or exceptional event that occars
during the execution of a program and disrupts public static void main(String[] args)
the normal fNow of its instructions.
Exceptions can arise due to various reasons, such IIUse of unchecked Exception
as invalid input, network issues, file VO errors, and try {
so on. In Java, exceptions are objectsprovide that
represent these abnormal conditions and ldouble x=3/0;
a mechanism for handling them ina structured throw new ArithmeticException);
MaNKEer.
Exception Handling in Java : catch (ArithmeticException e) {
Java provides a robust exception handling [Link]);
mechanism to deal with exceptions. When an
exceptional condition occurs in a Java program, it
throws an exception. This exception is then
propagated up the call stack until it is caught and
handled by an appropriate exception handler. Throws :
The maln components of Java exception The "throws" keyword is used in a metho
handling are:
exception is signature to indicate that the method might thto
" Ty: The code that may raise an is certain types of checked exceptions. Wncu
try block
placed within a try" block. The blocks or a method is declared with a throws" clause, it ric
followed by one or more catch
Webpage Design/2022/@ 138
BCA Vh Semoster lJave Programming &Dynamlc
the service method of the Servlet interface is given below
response)
public void service(ServletRequest request, ServletResponse
throws ServletException, IOException
5. Destroy method is invoked :
The web container calls the destroy method before removing the servlet instance from the service. h
gives the servlet an opportunity to clean up any resource for exanple memory, thread etc. The syntax of
the destroy method of the Servlet interface is given below :
public void destroy(0
During the servlet life cycle, multiple client requests can be handled by the same servlet instance whi
enhancess performance by avoiding the overhead of creating a new instance for each request.
Q12. what do you mean by JSP? Explain the architecture of JSP. How JSP provide
better performance.
Ans. Java Server Page (JSP):
JSP which stands for Java Server Pages'. JSP (JavaServer Pages) is a technology used for
building dynamic web content using Java. It is a server-side technology. It is used for creating web
applications. It allows developers to embed Java code, along with HTML, XML, or other markua
languages, directly into web pages. JSP pages are compiled into servlets by the web container during
the first request and are executed on the server-side to generate dynamic content that is sent to the client
JSP is first converted intoa servlet by JSP container before processing the client's request.
JSP Architecture :
JSParchitecture gives ahigh-level view ofthe working of JSP. JSP architecture is a3 tier architecture
It has a Client, Web Server, and Database. The client' is the web browser or application on the user
side. "Web Server' uses a JSP Engine i.e.; a container that processes JSP. For example, Apache Tomca
has a built-in JSP Engine. JSP Engine intercepts the request for JSP and provides the runtime environment
for the understanding and processing of JSP files. It reads, parses, build Java Servlet, Compiles and
Executes Java code, and returns the HTML page to the client. The webserver has access to the 'Database'.
Following diagram shows the architecture of JSP :
Web Server DB
JSP Request
JSP
Clicnt
HTML Pag Engine
Eschrequest
is processed in its own
thread, allowing the server to handle multiple requests
simultaneously.
EmclentResource Management: Servlets are created and initialized by the container when the
" requestist received, and they remain in memory to handle subsequenttrequests. This reduces
first Overhead and improves performance.
resource e: Servlets cani be extended and customized to support various functionalities, making
Extensible:
adaptableeto different application requirements.
themnhighly
of a Servlet (Servlet Life
Cycle) :
Cycle
Ljie containerrmaintains the life cycle ofa servlet instance..Following is the life cycle of the servlet
The web class is loaded.
Servlet is created.
instance
) Servlet invoked.
is
t initmethod
method is invoked.
Aservice
destroymethod is invoked.
S.
1. Load servlet class
[Link] servlet instance
[Link] the init(-) method
@Override
public void run() {
int number - isEven ?2 : 1;
while (number < max) {
if (isEven && number %2 ==0) | (!isEven && number % 2 != 0)) {
[Link]([Link]()-getName() + ":"+ number);
number += 2; // Increment by 2 to print next even/odd number
try {
[Link](100); /lA small delay to simulate some work
}catch (InterruptedException e) {
[Link]():