0% found this document useful (0 votes)
9 views13 pages

RMI Basics: Stubs, Skeletons, and Clients

The document covers various topics related to Java programming, including RMI (Remote Method Invocation) concepts like stubs and skeletons, HTML form elements, servlet lifecycle, session tracking methods, and JavaBeans. It also discusses JavaMail protocols, servlet chaining, applet-servlet communication, JDBC for database connectivity, and Enterprise Java Beans (EJB). Each section provides code examples and applications, emphasizing the importance of these concepts in building dynamic and modular web applications.

Uploaded by

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

RMI Basics: Stubs, Skeletons, and Clients

The document covers various topics related to Java programming, including RMI (Remote Method Invocation) concepts like stubs and skeletons, HTML form elements, servlet lifecycle, session tracking methods, and JavaBeans. It also discusses JavaMail protocols, servlet chaining, applet-servlet communication, JDBC for database connectivity, and Enterprise Java Beans (EJB). Each section provides code examples and applications, emphasizing the importance of these concepts in building dynamic and modular web applications.

Uploaded by

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

1. Explain Stubs and Skeletons with Example.

📆 Asked in Dec 2022, Dec 2023, Jun 2022, Jun 2023


Stub:
• The stub is a client-side proxy that represents the remote object.
• It forwards method calls from the client to the server-side object using the
network.
Skeleton:
• The skeleton was the server-side component that received the method calls and
invoked the appropriate method.
• It was used in Java 1.1, and removed in Java 1.2 onwards (now dynamic proxies
are used).
✅ Mini Code Structure:
java

// Interface
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}

// Server
[Link]("rmi://localhost/Hello", new HelloImpl());

// Client
Hello stub = (Hello) [Link]("rmi://localhost/Hello");
[Link]([Link]());
Conclusion:
Stub and skeleton act like bridges in RMI communication. Today, only stubs are used
(skeleton is obsolete).
Application: Online chat apps, remote calculator, distributed games.
-------------------------------------------------------------------------

2. Discuss any Three HTML Form Elements.


📆 Asked in Dec 2022, Jun 2022, May 2024
HTML forms allow user input to be submitted to a web server.
✅ Three Common Form Elements:
1. Text Box
html

<input type="text" name="username" />


Used to take single-line input from the user.
2. Radio Button
html

<input type="radio" name="gender" value="male" />


Used to select one option among many.
3. Submit Button
html

<input type="submit" value="Submit" />


Used to submit form data to the server.
Conclusion:
Form elements are essential for user interaction on websites.
Application: Login, registration, feedback, search forms.
________________________________________

3. Explain Servlet Life Cycle.


📆 Asked in Jun 2022, Dec 2023
Servlet lifecycle includes 3 key stages managed by the servlet container.
✅ Stages:
1. init()
• Called only once when the servlet is first loaded.
• Used to initialize resources like DB connections.
2. service()
• Called for every request.
• Calls doGet() or doPost() based on request type.
3. destroy()
• Called once before the servlet is removed.
• Used for cleanup activities.
✅ Mini Code:
java

public void init() { }


public void service() { }
public void destroy() { }
Conclusion:
Understanding servlet lifecycle helps manage resource efficiency and request
handling.
Application: Any backend web application like login or shopping cart.
________________________________________

4. Discuss Session Tracking Methods in Servlet.


📆 Asked in Dec 2023, Jun 2023
Session tracking is used to identify returning users or maintain data across pages.
✅ Methods:
1. Cookies – small data stored on client browser
java

Cookie c = new Cookie("user", "Harisha");


[Link](c);
2. URL Rewriting – adds data to URL
java

<a href="[Link]?user=Harisha">Cart</a>
3. HttpSession – server-side session
java

HttpSession session = [Link]();


[Link]("username", "Harisha");
Conclusion:
Session tracking ensures personalized and secure web sessions.
Application: Shopping carts, login sessions, quiz attempts.
________________________________________

5. Short Notes on JAR File in JavaBeans


📆 Dec 2019, May 2024
Definition:
JAR stands for Java ARchive. It is a compressed file format used to bundle multiple
.class files, images, sound files, and other resources into a single archive file.
🔹 Why is it used in JavaBeans?
• To package and distribute reusable components.
• Allows easy deployment and sharing of beans in IDEs like NetBeans or Eclipse.
• Can include a Manifest file (optional) that tells which class contains the
main method or other metadata.
🔹 Example: Creating a JAR for a JavaBean
bash

jar cf [Link] [Link]


Where:
• c → create
• f → specify file name
• [Link] → compiled bean
🔹 Manifest Example (optional):
css

Main-Class: Student
✅ Conclusion:
JAR files are essential for organizing and deploying JavaBeans and other reusable
components in a clean, compressed format.
Application: Java libraries, JDBC drivers, JavaBeans in JSP/Servlet-based web apps.
________________________________________

6. Explain the Steps to Create a New Java Bean.


📆 Asked in Dec 2021, Jun 2022
✅ Steps:
1. Create a Public Class
o Class should implement Serializable.
2. Define Private Properties
o Properties should be private.
3. Add Getter and Setter Methods
java

public class Student implements Serializable {


private String name;
public void setName(String name) { [Link] = name; }
public String getName() { return name; }
}
4. Compile and Package into JAR (optional)
Conclusion:
JavaBeans follow simple conventions, making them ideal for reusable UI and backend
components.
Application: Form components, backend objects in JSP/Servlets.
________________________________________

7. JSP Scripting / Directive Elements


📆 Asked in Dec 2019, Dec 2023, Jun 2022
✅ Scripting Elements:
1. Scriptlet – Java code block

<% int x = 5; %>

2. Expression – Output of Java expression

<%= x * 2 %>

3. Declaration – Declares variables/methods

<%! int count = 0; %>

✅ Directive Elements:

1. Page – page-related settings

<%@ page language="java" %>


2. Include – includes another file

<%@ include file="[Link]" %>

3. Taglib – custom tag libraries

<%@ taglib uri="..." prefix="c" %>

Conclusion:
JSP scripting/directives add Java logic and configuration inside HTML pages.

Application: Dynamic pages, backend logic in web apps.


________________________________________

8. Write Short Notes on RMI Client.


📆 Asked in Jun 2023, May 2024
RMI Client connects to the remote object and calls methods defined in the remote
interface.
✅ Steps:
1. Look up remote object via RMI Registry.
2. Typecast to the remote interface.
3. Call the remote method.
✅ Mini Code:
java

Hello stub = (Hello) [Link]("rmi://localhost/Hello");


[Link]([Link]());
Conclusion:
RMI clients allow calling methods of remote objects as if they are local.
Application: Remote login, file sharing, remote monitoring.
________________________________________
9. Applet to Servlet Communication
📆 Asked in Dec 2022, May 2024
Applets can communicate with servlets using HTTP protocol and URLConnection.
✅ Steps:
1. Applet sends data using URLConnection.
2. Servlet reads using InputStream or parameters.
✅ Applet Code:
java

URL url = new URL("[Link]


URLConnection con = [Link]();
[Link](true);
✅ Servlet Code:
java

BufferedReader br = [Link]();
String input = [Link]();
Conclusion:
Applet-Servlet communication is useful for GUI + backend logic in Java web apps.
Application: Online forms, quizzes, real-time data updates.
________________________________________

10 marks:-

✅ 1. RMI Registry with Example (Simple Code + Full Explanation)


📅 Asked in May 2021, Jun 2023, May 2024
________________________________________
🔷 What is RMI?
**Remote Method Invocation (RMI)** allows Java programs to invoke methods on an
object located in another JVM (usually on a different
machine).________________________________________
🔷 What is RMI Registry?
• The RMI Registry is a naming service provided by Java to register and locate
remote objects.
• It allows clients to look up remote services using a name, like a phone
directory.
________________________________________
🔷 Simple RMI Flow:
1. Create Remote Interface
2. Implement it in a class
3. Register the object in the RMI Registry
4. Client looks up the object and calls methods
________________________________________
✅ Step-by-Step Example
________________________________________
🔹 1. Remote Interface
java

import [Link].*;

public interface Hello extends Remote {


String sayHello() throws RemoteException;
}
________________________________________
🔹 2. Implementation Class
java

import [Link];

public class HelloImpl extends UnicastRemoteObject implements Hello {


HelloImpl() throws RemoteException { super(); }

public String sayHello() {


return "Hello from Server";
}
}
________________________________________
🔹 3. Server Program
java

import [Link].*;

public class Server {


public static void main(String args[]) throws Exception {
HelloImpl obj = new HelloImpl();
[Link]("rmi://localhost:1099/HelloService", obj);
[Link]("Server is ready...");
}
}
________________________________________
🔹 4. Client Program
java

import [Link].*;
public class Client {
public static void main(String args[]) throws Exception {
Hello stub = (Hello) [Link]("rmi://localhost:1099/HelloService");
[Link]([Link]());
}
}
________________________________________
🔹 5. Start the RMI Registry (on Command Prompt):
bash

start rmiregistry
Run it from the folder where .class files are present.
________________________________________
✅ Conclusion:
• The RMI Registry is essential for locating remote objects.
• It acts like a directory server that binds names to remote objects.
• It helps in building distributed applications using Java.
________________________________________
🎯 Application Examples:
• Online quiz system (server sends questions)
• Remote file access system
• Server-based chat/messenger

✅ 2. Explain JavaMail Protocols / Components


📅 Dec 2019, Dec 2021, Dec 2023
________________________________________
🔷 What is JavaMail?
JavaMail is a Java API used to send, receive, and read emails using standard email
protocols like SMTP, POP3, and IMAP.
________________________________________
🔷 JavaMail Protocols
Protocol Description
SMTP Used to send emails (Simple Mail Transfer Protocol)
POP3 Downloads emails from the server (Post Office Protocol v3)
IMAP Reads and manages mail on the server without deleting (Internet Mail Access
Protocol)
________________________________________
🔷 JavaMail Components
1. Session – Stores config and authentication info
2. Message – Represents the email
3. Transport – Sends the message
4. Store – Retrieves messages from mail server
________________________________________
✅ Simple Code to Send Email
java

Properties props = new Properties();


[Link]("[Link]", "[Link]");
[Link]("[Link]", "587");

Session session = [Link](props);


MimeMessage message = new MimeMessage(session);

[Link](new InternetAddress("from@[Link]"));
[Link]([Link], new
InternetAddress("to@[Link]"));
[Link]("Test Mail");
[Link]("This is a test email from Java");

[Link](message);
________________________________________
✅ Conclusion:
JavaMail allows Java applications to send and manage emails easily using standard
protocols.
📌 Applications:
• Sending OTP
• Contact form emailers
• Registration confirmation
________________________________________

✅ 3. Servlet Chaining with Example


📅 Dec 2022, Dec 2023
________________________________________
🔷 What is Servlet Chaining?
Servlet chaining allows multiple servlets to work together in a sequence, where one
servlet forwards the request/response to another servlet.
________________________________________
🔷 Two Methods:
• [Link]() → Passes control
• [Link]() → Includes content
________________________________________
✅ Simple Code
🔸 Servlet 1
java

RequestDispatcher rd = [Link]("SecondServlet");
[Link](request, response);

🔸 Servlet 2
java

PrintWriter out = [Link]();


[Link]("Welcome to Second Servlet");
________________________________________
✅ Conclusion:
Servlet chaining promotes modular and reusable servlet design, where each servlet
does a specific job.
📌 Applications:
• Authentication + dashboard loading
• Pre-processing and logging
• Multi-step form submissions
______________________________________

__
✅ 4. Applet to Servlet Communication
📅 Dec 2021, Dec 2022
________________________________________
🔷 What is it?
Communication between a Java Applet (GUI client) and a Servlet (backend server) is
done via HTTP using URLConnection.
________________________________________
✅ Applet Code
java

URL url = new URL("[Link]


URLConnection con = [Link]();
[Link](true);

OutputStreamWriter out = new OutputStreamWriter([Link]());


[Link]("name=Harisha");
[Link]();
________________________________________
✅ Servlet Code
java

BufferedReader br = [Link]();
String input = [Link]();
PrintWriter out = [Link]();
[Link]("Received: " + input);
________________________________________

A connection is opened from the applet to the servlet using the URL.

setDoOutput(true) enables sending data to the servlet.

Data (name=Harisha) is written from the applet to the servlet.

The servlet reads the incoming data using [Link]().

The servlet responds back with a message using [Link]().

✅ Conclusion:
Applet–Servlet communication allows GUI-based clients to interact with backend
logic dynamically.
📌 Applications:
• Online quizzes
• Live form submissions
• GUI → server interaction systems
________________________________________

✅ 5. Explain Database Connectivity in Servlet


📅 Dec 2019, Jun 2023
________________________________________
🔷 What is JDBC?
JDBC (Java Database Connectivity) is a Java API used to connect and perform
operations on databases.
________________________________________
✅ Steps in JDBC with Servlet:
1. Load the driver
2. Establish DB connection
3. Create and execute query
4. Display result
5. Close the connection
________________________________________
✅ Simple Code in Servlet
java

[Link]("[Link]");
Connection con = [Link]("jdbc:mysql://localhost/mydb", "root",
"1234");
Statement st = [Link]();
ResultSet rs = [Link]("SELECT * FROM student");

while([Link]()) {
[Link]("Name: " + [Link]("name"));
}
________________________________________
✅ Conclusion:
Using JDBC in servlets allows dynamic web apps to interact with databases in real
time.
📌 Applications:
• Login authentication
• Displaying records
• Saving feedback, forms
________________________________________

✅ 6. JavaBeans Development Kit (BDK) / Properties


📅 Dec 2023, Jun 2023
________________________________________
🔷 What is JavaBean?
JavaBean is a reusable software component that follows certain design rules like
having private properties and getter/setter methods.
________________________________________
🔷 Properties of a Bean
• Must be public class
• Should have no-arg constructor
• Should be Serializable
• Uses get/set methods
________________________________________
✅ Sample Bean Code
java

public class Student implements Serializable {


private String name;

public void setName(String n) { [Link] = n; }


public String getName() { return name; }
}
________________________________________
🔷 What is BDK?
BDK (Bean Development Kit) is a tool from Sun Microsystems used to:
• Test and configure beans
• Visually drag-drop and connect beans
• Package them into JAR
________________________________________
✅ Conclusion:
JavaBeans + BDK help in building modular and reusable components that work in JSP,
IDEs, and even GUIs.
📌 Applications:
• Visual GUI builders
• JSP backend components
• Business logic reusability
________________________________________

✅ 7. Types of EJB / Session Beans with Example


📅 Dec 2020, Dec 2022, May 2024
________________________________________
🔷 What is EJB?
Enterprise Java Beans (EJB) are server-side components used in large-scale
enterprise apps.
________________________________________
🔷 Types of EJB

1. Session Bean
o Business logic

o Types:
 Stateless → No memory of client (e.g., calculator)
 Stateful → Remembers client session (e.g., cart)

2. Message-Driven Bean
o Listens to JMS messages
o Used in asynchronous systems
________________________________________
✅ Simple Stateless Bean Code
java

@Stateless
public class CalculatorBean {
public int add(int a, int b) {
return a + b;
}
}
________________________________________
✅ Conclusion:
EJB helps build secure, scalable, transactional systems that are easy to manage.
📌 Applications:
• ATM systems
• Shopping carts
• Booking and payment systems
________________________________________

✅ 8. Push Data from RMI Server


📅 Dec 2022, Jun 2022
________________________________________
🔷 Can RMI push data?
Yes! RMI usually pulls, but using callbacks, the server can push data to the
client.
________________________________________
🔷 How?
1. Client implements a remote interface
2. Server gets client’s stub and calls method on it
3. This way, server pushes data
________________________________________
✅ Conceptual Code

Client's Remote Interface:

public interface Notify extends Remote {


void notifyClient(String msg) throws RemoteException;
}
Server calls:

[Link]("You have a new message!");


________________________________________
✅ Conclusion:
Though RMI is typically pull-based, push notifications are possible using callback
mechanism.
📌 Applications:
• Live chat
• Remote alerts
• Stock market ticker apps
________________________________________

2 marks:-
## ✅ **Repeated 2-Marks Questions – Answers**

### 1. **What is Java Bean?**

A **Java Bean** is a reusable software component that follows specific conventions:

* Public no-argument constructor


* Getter and setter methods for accessing properties
* Serializable
* Example:

```java
public class Student implements Serializable {
private String name;
public String getName() { return name; }
public void setName(String name) { [Link] = name; }
}
```

---

### 2. **What are the types of EJB?**

* **Session Beans** (stateless/stateful)


* **Entity Beans** (deprecated, replaced by JPA)
* **Message-Driven Beans (MDB)**

> Modern EJB focuses mainly on session and message-driven beans.

---

### 3. **What is Introspection in Java Bean?**

Introspection is the process of analyzing a Bean to:

* Discover its properties, events, and methods at runtime


* Done using **[Link]** class

---

### 4. **What is JAR file?**


JAR (Java ARchive) is a package file format that bundles:

* Multiple class files, metadata, and resources


* Into a single compressed file
* Example: `[Link]`

---

### 5. **What are the components of RMI?**

* **Remote Interface**
* **Stub and Skeleton** (skeleton deprecated after Java 1.2)
* **RMI Registry**
* **Remote Object Implementation**
* **Client**

---

### 6. **What is RMI Registry?**

A **name server** that allows clients to look up remote objects via logical names.

* Run using: `rmiregistry`


* Objects must be registered using `[Link]()`

---

### 7. **What is JDBC?**

**Java Database Connectivity** is an API for accessing relational databases.

* Interfaces: `Connection`, `Statement`, `ResultSet`


* Example: `[Link](...)`

---

### 8. **Write the advantages of Java Beans.**

* Platform independent
* Reusable and portable
* Supports property/event management
* Easy to manipulate using builder tools

---

### 9. **What is the usage of RMI over Inter-ORB Protocol (IIOP)?**

Allows Java RMI applications to interact with CORBA systems.

* Enables **interoperability** between Java and other CORBA-compliant languages


* Uses `[Link]`

---

### 10. **What is a bound property?**

A bound property notifies listeners when its value changes.

* JavaBeans use `PropertyChangeSupport` for this.


```java
[Link]("propertyName", oldVal, newVal);
```

---

### 11. **State any two applications of RMI technology.**

* Distributed banking applications


* Remote file access and editing
* Chat servers or distributed game engines

---

Common questions

Powered by AI

Servlets use several session tracking methods to maintain data consistency across multiple web pages. These methods include cookies, URL rewriting, and HttpSession. Cookies store small amounts of data on the client's browser to identify returning users. URL rewriting appends session data to the URL string, allowing the session state to be maintained as the user navigates the site. HttpSession provides a server-side store for session data, maintaining continuity across page requests without exposing sensitive data .

The JavaMail API includes key components such as Session, Message, Transport, and Store, which together facilitate email operations. The Session component manages configuration and authentication details required for email sessions. The Message component represents the actual email to be sent or received. Transport handles sending the email using protocols like SMTP, while Store manages email retrieval from servers using protocols like POP3 or IMAP. These components abstract the complexities of email handling, enabling developers to seamlessly integrate emailing capabilities into Java applications .

The servlet lifecycle ensures efficient resource management and request handling through its three key stages: init(), service(), and destroy(). The init() method is called once when the servlet is initialized, allowing for the setup of necessary resources like database connections. The service() method is called for each client request, ensuring that requests are handled efficiently by invoking doGet() or doPost() based on the request type. Finally, the destroy() method is called once before the servlet is removed, enabling cleanup operations such as closing open connections, thus preventing resource leaks and ensuring optimal use of resources throughout the servlet's lifecycle .

Servlet chaining is utilized to promote modular design in Java web applications by allowing multiple servlets to process a request in a sequence. Using mechanisms such as RequestDispatcher.forward() and RequestDispatcher.include(), servlets can pass requests and responses between one another. This modular approach encourages the development of small, specialized servlets, each performing a distinct function, which can be chained together to achieve complex processing tasks. Consequently, servlet chaining supports code reuse, separation of concerns, and easier maintenance .

Using push notifications via RMI is advantageous in scenarios requiring real-time updates or alerts, such as chat applications, remote monitoring systems, and stock market tickers. This is achieved in Java through a callback mechanism where the client implements a remote interface, allowing the server to receive the client’s stub and invoke methods on the client. By using this approach, the server can push data to the client, effectively enabling push notifications even though RMI is traditionally pull-based .

HTML form elements enhance user interaction by facilitating user input and enabling data submission to the web server. They allow users to interact directly with applications through a web interface, such as executing searches or providing feedback. Common examples include text boxes for single-line input, radio buttons for choosing from a predefined set of options, and submit buttons for posting form data to the server .

In RMI communication, stubs function as client-side proxies which represent the remote objects. They are responsible for forwarding method calls from the client to the server-side object over the network . Skeletons used to serve as the server-side component that received method calls and invoked the appropriate server-side methods. However, skeletons were removed in Java 1.2 in favor of dynamic proxies, rendering the skeleton component obsolete .

Creating a new Java Bean involves several key steps: defining a public class that implements Serializable, defining private properties to encapsulate data, and providing public getter and setter methods for accessing these properties. This process is significant for application development as it promotes encapsulation, reusability, and maintainability. JavaBeans are particularly useful for building UI components and backend logic, allowing developers to create versatile and easily customizable elements within enterprise applications .

JSP scripting and directive elements enhance the dynamic capabilities of web applications by allowing the integration of Java logic directly within HTML pages. Scripting elements such as scriptlets, expressions, and declarations allow developers to write Java code, output Java expressions, and declare variables or methods, respectively. Directive elements like page, include, and taglib manage page-level settings, the inclusion of files, and use of custom tag libraries. Together, these elements enable the creation of highly interactive and configurable web pages, seamlessly combining presentation with backend logic .

JAR files are critical in the deployment of JavaBeans because they provide a compressed file format for bundling multiple components such as .class files, images, and other resources into a single archive. This consolidation simplifies distribution and management, making it easier to share and deploy reusable JavaBeans across different platforms. Additionally, JAR files can include a manifest file specifying metadata, further enhancing their utility in packaging JavaBeans for use in IDEs like NetBeans or Eclipse .

You might also like