wt-unit3
wt-unit3
Unit 3 2024-25
B.TECH.(CSIT) SEMESTER -V
1: Java script: Introduction, documents, forms, statements, functions, objects.
2. Introduction to AJAX
3. Internet Addressing, InetAddress, Factory Methods, Instance Methods, TCP/IP Client Sockets,
URL, URL Connection, TCP/IP Server Sockets, Datagram.
Faculty
Dr. Aadarsh Malviya
(Associate Professor Department of CSE)
Affiliated to
1
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]
2
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]
firstName: "John",
lastName: "Doe",
age: 30,
greet: function() {
return "Hello, " + this.firstName;
}
};
Accessing Object Properties:
javascript
Copy code
console.log(person.firstName); // Outputs: John
console.log(person['lastName']); // Outputs: Doe
Object Methods: Objects can have methods, which are functions stored as object properties.
javascript
Copy code
person.greet(); // Outputs: Hello, John
Conclusion
JavaScript is an essential language for creating interactive and dynamic web pages. Its core concepts, such as
manipulating the DOM, handling forms, using statements and loops, defining functions, and working with
objects, allow developers to build responsive and interactive web applications.
2. introduction to AJAX
Introduction to AJAX
AJAX (Asynchronous JavaScript and XML) is a web development technique that allows web pages to
update asynchronously by exchanging data with a server behind the scenes. This means that parts of a web
page can be updated without refreshing the entire page, providing a smoother, faster, and more interactive
user experience.
Though originally designed to exchange XML, modern AJAX typically uses JSON (JavaScript Object
Notation) for data exchange due to its simplicity and compatibility with JavaScript.
Partial Page Updates: Only certain parts of the page (like a form or a data table) are updated without
needing a full page refresh.
AJAX Workflow
Client-Side (Browser): A JavaScript function initiates an AJAX request.
AJAX Request: A request is sent to the server via an HTTP method like GET or POST.
Server-Side: The server processes the request, typically accessing data from a database, and returns the
3
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]
requested data.
Client-Side (Browser): JavaScript processes the server’s response and updates the relevant part of the
page.
AJAX Example Using XMLHttpRequest
javascript
Copy code
// Create a new XMLHttpRequest object
let xhr = new XMLHttpRequest();
xhr.onerror = function() {
console.error("Request failed");
};
In this example, a request is sent to the server asynchronously, and the response is handled once it’s
received.
javascript
Copy code
fetch('/article')
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json(); // Parse the response as JSON
})
.then(data => {
console.log(data); // Use the response data
})
.catch(error => {
console.error('There was an error!', error);
});
4
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]
javascript
Copy code
xhr.open('GET', 'url', true);
POST: Sends data to the server (usually for updating or creating).
javascript
Copy code
xhr.open('POST', 'url', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));
Advantages of AJAX
Better User Experience: AJAX provides smoother and faster web applications since only parts of the page
are reloaded, reducing the load on the server and bandwidth usage.
Asynchronous Data Loading: The user can interact with the webpage while the data is being fetched in the
background, making the app more responsive.
Reduced Server Load: By only fetching specific parts of a web page, AJAX reduces the load on the server.
No Page Reloads: Users don’t need to reload the entire page for small updates, which saves time.
Disadvantages of AJAX
SEO Issues: Since AJAX does not reload the page, search engines might have difficulty indexing the
content that’s dynamically loaded via AJAX.
JavaScript Dependency: AJAX relies heavily on JavaScript. If JavaScript is disabled in the browser, AJAX
features won’t work.
Browser Compatibility: Although modern browsers fully support AJAX, older browsers may not fully
support all features (though this is less of an issue today).
AJAX with JSON Example
Using JSON instead of XML for data exchange is more common nowadays.
javascript
Copy code
fetch('/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username: "JohnDoe", password: "12345" })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
Conclusion
AJAX is a powerful technique that enables asynchronous communication between a browser and a server,
resulting in faster, more responsive web applications. By dynamically updating parts of the web page,
AJAX enhances the user experience without the need for full-page reloads. Modern web development has
embraced AJAX through methods like fetch() and APIs like JSON for efficient data exchange.
Internet Addressing, InetAddress, Factory Methods, Instance Methods, TCP/IP Client Sockets, URL, URL
Connection, TCP/IP Server Sockets, Datagram.
5
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]
Internet Addressing
Internet addressing refers to the way devices on a network are uniquely identified and located. The most
common form of addressing is IP addresses. An IP address is a unique identifier assigned to each device
connected to a network, ensuring that data sent over the Internet reaches the correct destination.
There are two versions of IP addresses:
IPv4: 32-bit addresses, e.g., 192.168.0.1.
IPv6: 128-bit addresses, designed to accommodate the growing number of devices on the Internet,
e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334.
Java Networking: InetAddress Class
In Java, the InetAddress class represents an IP address. It provides methods to work with both IPv4 and
IPv6 addresses.
Factory Methods
getByName(String host): Returns the InetAddress of the host provided as a name or an IP address
string.
java
Copy code
InetAddress address = InetAddress.getByName("www.google.com");
System.out.println(address);
getLocalHost(): Returns the InetAddress object representing the local host (i.e., the current
machine).
java
Copy code
InetAddress localAddress = InetAddress.getLocalHost();
System.out.println(localAddress);
getAllByName(String host): Returns an array of InetAddress objects for the specified host (useful
when a host has multiple IP addresses, such as Google).
java
Copy code
InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
for (InetAddress addr : addresses) {
System.out.println(addr);
}
Instance Methods
getHostName(): Returns the hostname associated with the IP address.
java
Copy code
String host = address.getHostName();
getHostAddress(): Returns the IP address as a string.
java
Copy code
String ip = address.getHostAddress();
isReachable(int timeout): Tests if the address is reachable within a given timeout (in milliseconds).
java
Copy code
boolean reachable = address.isReachable(5000);
TCP/IP Networking
TCP/IP (Transmission Control Protocol/Internet Protocol) is the fundamental communication protocol for
the Internet. It defines how data should be packaged, addressed, transmitted, and received across
networks.
TCP: Provides reliable, connection-oriented communication, ensuring that data is transmitted
accurately and in the correct order.
6
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
URL and URLConnection
URL (Uniform Resource Locator) is a Java class representing a URL (web address). It is used to
identify resources on the Internet.
Example:
java
Copy code
URL url = new URL("http://www.example.com");
URLConnection is an abstract class representing a communication link between the application and
a URL. Through this connection, we can read from or write to the resource pointed to by the URL.
Example of opening a connection:
java
Copy code
URL url = new URL("http://www.example.com");
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
Server Sockets (ServerSocket)
7
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]
A server socket waits for requests from clients and establishes a connection upon receiving a request. It is
used to create a TCP/IP server.
Example of a simple TCP server:
java
Copy code
import java.io.*;
import java.net.*;
while (true) {
Socket clientSocket = serverSocket.accept(); // Wait for a client to connect
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
8
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]
socket.close();
}
}
Conclusion
Java provides a robust API for network programming, allowing developers to create both TCP/IP and
UDP clients and servers. Classes like InetAddress, Socket, ServerSocket, URL, URLConnection, and
DatagramSocket offer powerful ways to manage networking tasks such as communication between servers
and clients, fetching resources from URLs, and handling both reliable (TCP) and unreliable (UDP) data
transmission.