0% found this document useful (0 votes)
11 views

Adv java

Uploaded by

rajambekar2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Adv java

Uploaded by

rajambekar2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ADV JAVA… 2.

State and explain socket programming along with the


1. Define the OSI Models with protocols. three internet sockets. (Explain host, client, server, TCP and
other things.)
The OSI (Open Systems Interconnection) model is a
conceptual framework used to understand network interactions Socket Programming allows for communication between two
in seven layers: machines over a network. A socket is one endpoint of a two-way
1. Physical Layer: Deals with the physical connection communication link.
between devices. Protocols: Ethernet, USB, DSL. Three Types of Internet Sockets:
2. Data Link Layer: Provides node-to-node data transfer.
Protocols: PPP (Point-to-Point Protocol), Frame Relay, 1. Stream Sockets (TCP): Provides a reliable, connection-
Ethernet. oriented communication channel. It guarantees that data
3. Network Layer: Handles routing of data packets. arrives in order and without errors.
Protocols: IP (Internet Protocol), ICMP (Internet Control 2. Datagram Sockets (UDP): Provides an unreliable,
Message Protocol). connectionless communication channel. Suitable for
4. Transport Layer: Ensures complete data transfer. applications where speed is more critical than reliability
Protocols: TCP (Transmission Control Protocol), UDP (e.g., streaming).
(User Datagram Protocol). 3. Raw Sockets: Allow direct access to lower-layer protocols.
5. Session Layer: Manages sessions between applications. They are mainly used for custom protocols and are
Protocols: NetBIOS, RPC (Remote Procedure Call). generally reserved for network administrators.
6. Presentation Layer: Translates data for the application 4.
layer. Protocols: SSL (Secure Sockets Layer), TLS Host, Client, Server:
 Host: Any machine connected to a network.
(Transport Layer Security).
 Client: A machine that requests resources from a server.
7. Application Layer: Closest to the end user, supports
 Server: A machine that provides resources or services to
application and end-user processes. Protocols: HTTP,
FTP, SMTP, DNS. clients.
 Ports: Identifiers for different services on a host. Each
socket is associated with a port number.

Example of a TCP Socket: Here’s a simplified workflow for a


TCP socket connection:

1. Client creates a socket and connects to the server using


the server's IP address and port number.
2. Server listens on a specific port for incoming connections.
3. Once a connection is established, both client and server
can send and receive data using input and output streams.
3. What is IP? Difference between IPv4 and IPv6.
4. Explain multicasting sockets along with 4 methods
IP (Internet Protocol) is the fundamental protocol that governs
communication on the internet. It is responsible for addressing Multicasting is the transmission of a message or information to a
devices and routing data packets across networks. In Java, group of destination computers simultaneously in a single
understanding IP is crucial for developing network-based transmission. Multicast sockets are used for sending data to
applications. multiple clients over a network efficiently. Java provides
Multicast Socket class for multicasting operations.
Key Concepts:
IPv4 IPv6  Multicast Group: A group of hosts that can send or receive

32 bits (4 bytes) long, 128 bits (16 bytes) long, allowing multicast messages.
resulting in approximately for 340 undecillion addresses  Multicast IP Address: A specific range of IP addresses

4.3 billion unique (2^128), which solves the (224.0.0.0 to 239.255.255.255) used for multicasting.
addresses (2^32). address exhaustion problem of  Multicast Socket: A type of Datagram socket that supports

IPv4. sending and receiving messages from multicast groups.


Written in decimal as four Written in hexadecimal,
octets separated by dots separated by colons (e.g., Four Key Methods for Multicasting:
(e.g., 192.168.1.1). 2001:0db8:85a3:0000:0000:8a2 1. joinGroup(InetAddress group): Allows the socket to join
e:0370:7334). Consecutive a multicast group, so it can receive messages sent to the
sections of zeros can be group’s multicast IP address.
abbreviated.
Unicast, broadcast, Unicast, multicast, anycast 2. leaveGroup(InetAddress group): Used when the socket
multicast. (allows data to be sent to the no longer wants to receive messages from a multicast
nearest instance of a service). group.
The header is complex, The header is simplified with 8
with 12 fields, requiring fields, improving processing 3. send(DatagramPacket p, byte ttl): Used to send data to
more processing time. efficiency and speed. the multicast group. The ttl (time-to-live) controls the scope
Security is optional and IPsec is mandatory, providing of the multicast, limiting the number of hops (i.e., network
typically implemented built-in security features. layers) it can go through.
through additional protocols
like IPsec. 4. setTimeToLive(int ttl): Sets the TTL for multicast packets
sent from the socket. The TTL controls how many network
segments (hops) the packet is allowed to traverse.
5. What is a servlet? Describe the life cycle of servlets with an
Short Note:
example.
A Servlet is a Java class that extends the capabilities of servers that Server side include: Server-Side Includes (SSI) are directives
host applications accessed via a request-response programming model. used within HTML pages that enable a web server to dynamically
They are mainly used to process or store data submitted by users, insert content before delivering the page to the client. This
generate dynamic web pages, or manage session states. mechanism is commonly used for maintaining consistent content
across multiple web pages (such as headers, footers, or
Servlet Life Cycle:
navigation menus). The advantage of SSIs is that any changes
1. Loading and Instantiation: When the servlet is first requested, made to the included content are reflected across all pages that
the server loads the servlet class and creates an instance. use the same include directive, reducing redundancy and
2. Initialization (init method): After instantiation, the init() method is improving maintainability.
called. This is where the servlet initializes itself (e.g., database
connection setup). Servlet chaining: Servlet chaining involves passing the request
from one servlet to another, with multiple servlets handling
3. Service (service method): After initialization, the servlet starts
different parts of the request in a sequence. This concept is
handling requests through the service() method. This method is
called for each incoming request, determining whether it's a GET,
useful when multiple processes or transformations need to be
POST, etc., and dispatches it to the appropriate handler. applied to the same data before generating the final output.
In Java Servlets, chaining is achieved through the Request
4. Destruction (destroy method): When the servlet is no longer Dispatcher interface, where one servlet forwards the request to
needed, or the server shuts down, the destroy() method is called to another servlet. It’s useful when one servlet handles
clean up resources.
authentication, and another handles business logic.
Example:
public class MyServlet public void
extends HttpServlet { doGet(HttpServletRequest
@Override request, HttpServletResponse
public void init() response) throws IOException
throws {
ServletException { response.setContentType("tex
t/html");
System.out.println("S PrintWriter out =
ervlet is initialized"); response.getWriter();
} out.println("<h1>Hello,
@Override World!</h1>");}
@Override
public void destroy() {
System.out.println("Servlet is
destroyed");}}
7. Which are the session tracking techniques used in java
Servlets CGI programming language?
It is thread based i.e. for every
It is process-based i.e. for
Session tracking is essential for maintaining state across
new request new thread isevery new request new
created. process is created. multiple client requests in web applications, as HTTP is
The codes are written in JAVA
The codes are written any inherently stateless. Java provides several techniques to track
programming language. programming language. user sessions:
It can use any of the web-
It can use the web-server that 1. Cookies: Cookies are small pieces of data that the server
server. supports it. sends to the client browser, which stores them and sends them
Data sharing is possible.Data sharing is not possible. back with each subsequent request. Java Servlets can use
It links directly to the server.
It does not link the web server cookies to store session IDs or other session-related
directly to the server.
information on the client side. Cookies are widely used because
It can read and set HTTP It can neither read nor set
they persist even after the browser is closed (if not set to
servers. HTTP servers.
It is portable. It is not portable. expire).
 Advantages: Simple to implement and supported by all
browsers.
 Disadvantages: Some users disable cookies, and they
ServletConfig ServletContext pose privacy concerns.
ServletConfig is servlet ServletContext is for whole
specific application 2. URL Rewriting: In URL rewriting, the session information
Parameters of servletConfig Parameters of servletContext (such as session ID) is appended to the URL as a query string.
are present as name-value are present as name-value This technique is useful when cookies are disabled on the client
pair in <init-param> inside pair in <context-param> which side. Every request from the client includes the session ID as
<servlet>. is outside of <servlet> and part of the URL.
inside <web-app>
 Advantages: Works even if cookies are disabled.
ServletConfig object is ServletContext object is
obtained by getServletConfig() obtained by  Disadvantages: Less secure since session information is
method. getServletContext() method. visible in the URL, and URLs can be bookmarked with
Each servlet has got its own ServletContext object is only session information, causing potential session hijacking.
ServletConfig object. one and used by different
servlets of the application. 3. Hidden Form Fields: In this method, session information is
Use ServletConfig when only Use ServletContext when stored in hidden fields within HTML forms. The session data is
one servlet needs information whole application needs sent to the server whenever the form is submitted. This
shared by it. information shared by it technique is useful for tracking user sessions across form
submissions but works only for forms.
 Advantages: Works well when the application relies
heavily on forms.
 Disadvantages: Session data is transmitted only when
forms are submitted. It doesn't work for non-form
interactions like clicking links.
4. Http Session (Session Tracking API):The most robust and
commonly used method is the Http Session interface, provided
by the Java Servlet API. The server automatically manages the
session and assigns a unique session ID. The session data is
stored server-side, with only the session ID transmitted
between the client and server (usually via cookies or URL
rewriting).
 Advantages: Automatically handles session
management, flexible, and secure since the data is stored
server-side.
 Disadvantages: More resource-intensive as the server
must maintain session data for multiple clients.

You might also like