Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views25 pages

SADP 5th Module Notes

SADP 5th module notes

Uploaded by

vanishreey25
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)
14 views25 pages

SADP 5th Module Notes

SADP 5th module notes

Uploaded by

vanishreey25
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/ 25

Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

Module 5

Designing with Distributed Objects


Client/Server Systems

Distributed systems can be classified into peer-to-peer systems and client-server


systems. In the former, every computer system (or node) in the distributed system runs
the same set of algorithms; they are all equals, in some sense. The latter, the client/server
approach, is more popular in the commercial world. In client/server systems, there are
two types of nodes: clients and servers.

We look at the implementation of object-oriented systems that use the client/server


paradigm, which is the architecture itself

Basic Architecture of Client/Server Systems

We assume that although the client/server systems we build may have multiple clients,
they will have just one server. It is not difficult to extend the techniques to multiple
servers, so this is not a serious restriction. Figure 12.1 shows a system with one server
and three clients. Each client runs a program that provides a user interface, which may
or not be a GUI. The server hosts an object-oriented system. Like any other client/server
system, clients send requests to the server, these requests are processed by the object-
oriented system at the server, and the results are returned. The results are then shown to
end-users via the user interface at the clients

There is a basic difficulty in accessing objects running in a different Java Virtual


Machine (JVM). Let us consider two JVMs hosting objects as in Fig. 12.2. A single
JVM has an address space part of which is allocated to objects living in it. For
example, objects object 1 and object 2 are created in JVM 1 and are allocated at
addresses A1 and A2 respectively. Similarly, objects object 3 and object 4 live in JVM
2 and are respectively allocated addresses A3 and A4. Code within Object 2 can access
fields and methods in object 1 using address A1 (subject, of course, to access
specifiers). However, addresses A3 and A4 that give the addresses of objects object 3
and object 4 in JVM 2 are meaningless within JVM 1. To see this, suppose A1 and A3
are equal. Then, accessing fields using address given by A3 from code within JVM 1

Software Architecture & Design Pattern (21CS741) Page | 126


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

will end up accessing memory locations within object 1

This difficulty can be handled in one of two ways:

1. By using object-oriented support software: The software solves the problem by the
use of proxies that receive method calls on ‗remote‘ objects, ship these calls, and then
collect and return the results to the object that invoked the call. The client could have a
custom-built piece of software that interacts with the server software. This approach is
the basis of Java Remote Method Invocation.

2. By avoiding direct use of remote objects by using the Hyper Text Transfer Protocol
(HTTP). The system sends requests and collects responses via encoded text messages.
The object(s) to be used to accomplish the task, the parameters, etc., are all transmitted
via these messages. This approach has the client employ an Internet browser, which is,
of course, a piece of general purpose software for accessing documents on the world-
wide web. In this case, the client software is ignorant of the application structure and
communicates to the server via text messages that include HTML code and data. This
is the technique used for hosting a system on the Web.

Java Remote Method Invocation


The goal of Java RMI is to support the building of Client/Server systems where the
server hosts an object-oriented system that the client can access programmatically. The
objects at the server maintained for access by the client are termed remote objects. A
client accesses a remote object by getting what is called a remote reference to the
remote object. After that the client may invoke methods of the object.

The basic idea behind RMI is to employ the proxy design pattern. This pattern is used when it
is inefficient or inconvenient (even impossible, perhaps) to use the actual object. (Refer to
Fig. 12.3 for a description of the proxy pattern.)

Software Architecture & Design Pattern (21CS741) Page | 127


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

The proxy pattern creates a proxy object at each client site that accesses the remote object.
The proxy object implements all of the remote object‘s operations that the remote object
wants to be available to the client. The set up is shown in Fig. 12.4. When the client calls a
remote method, the corresponding method of the proxy object is invoked. The proxy object
then assembles a message that contains the remote object‘s identity, method name, and
parameters. This assembly is called marshalling. In this process, the method call must be
represented with enough information so that the remote site knows the object to be used, the
method to be invoked, and the parameters to be supplied. When the message is received by it,
the server performs demarshalling, whereby the process is reversed. The actual call on the
remote method of the remote object is made, and any return value is returned to the client via
a message shipped from the server to the proxy object

Software Architecture & Design Pattern (21CS741) Page | 128


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

Setting up a remote object system is accomplished by the following steps:

1. Define the functionality that must be made available to clients. This is accomplished by
creating remote interfaces.

2. Implement the remote interfaces via remote classes.

3. Create a server that serves the remote objects.

4. Set up the client.

Remote Interfaces

The first step in implementing a remote object system is to define the system functionality
that will be exported to clients, which implies the creation of a Java interface. In the case of
RMI, the functionality exported of a remote object is defined via what is called a remote
interface. A remote interface is a Java interface that extends the interface java.rmi.Remote,
which contains no methods and hence simply serves as a marker. Clients are restricted to
accessing methods defined in the remote interface. We call such method calls remote method
invocations

A remote interface must extend java.rmi.Remote and every method in it must declare to
throw java.rmi.RemoteException. These concepts are shown in the following example.

Implementing a Remote Interface

The remote interfaces are defined; the next step is to implement them via remote classes.
Parameters to and return values from a remote method may be of primitive type, of remote
type, or of a local type.

All arguments to a remote object and all return values from a remote object must be
serializable. Thus, in addition to the requirement that remote classes implement remote
interfaces, we require that they also implement the java.io.Serializable interface.

Parameters of non-remote types are passed by copy; they are serialized using the object
serialization mechanism, so they too must implement the Serializable interface.

Remote objects must somehow be capable of being transmitted over networks. A convenient
way to accomplish this is to extend the class java.rmi.server.UnicastRemoteObject.

Thus, the implementation of BookInterface is as below

Software Architecture & Design Pattern (21CS741) Page | 129


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

Since it is a remote class, Book must be compiled using the RMI compiler by invoking the
command rmic as below.

Remote objects are thus passed by reference. This is depicted in Fig. 12.5, where we have a
single remote object that is being accessed from two clients. Both clients maintain a reference
to a stub object that points to the remote object that has a field named a. Suppose now that
Client 1 invokes the method setA with parameter 5.
As we have seen earlier, the call goes through the stub to the remote object and gets executed
changing the field a to 5. The scheme has the consequence that any changes made to the state
of the object by remote method invocations are reflected in the original remote object. If the
second client now invokes the method getA, the updated value 5 is returned to it.

Software Architecture & Design Pattern (21CS741) Page | 130


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

Creating the Server


Before a remote object can be accessed, it must be instantiated and stored in an object
registry, so that clients can obtain its reference. Such a registry is provided in the form of the
class java.rmi.Naming. The method bind is used to register an object and has the following
signature:

The first argument takes the form //host:port/name and is the URL of the object to be
registered; host refers to the machine (remote or local) where the registry is located, port is
the port number on which the registry accepts calls, and name is a simple string for
distinguishing the object from the other objects in the registry. Both host and port may be
omitted in which case they default to the local host and the port number of 1099, respectively.
The process of creating and binding the name is given below.

The complete code for activating and storing the Book object is shown below

The Client
A client may get a reference to the remote object it wants to access in one of two ways:
1. It can obtain a reference from the Naming class using the method lookup.
2. It can get a reference as a return value from another method call.

Software Architecture & Design Pattern (21CS741) Page | 131


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

In the following we assume that an object of type SomeInterface has been entered into the
local registry under the name SomeName.

After the above step, the client can invoke remote methods on the object. In the following
code, the getters of the BookInterface object are called and displayed.

Setting up the System


To run the system, create two directories, say server and client, and copy the files
BookInterface.java, Book.java, and BookServer.java into server and the file BookUser.java
into client. Then compile the three Java files in server and then invoke the command

While in the server directory. This command creates the stub file Book_ Stub.class. Copy the
client program into client and compile it.
Run RMI registry and the server program using the following commands (on Windows).

The first command starts the registry and the second causes the Book instance to be created
and registered with the name MyBook.
Finally, run the client as below from the client directory.

Software Architecture & Design Pattern (21CS741) Page | 132


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

Implementing an Object-Oriented System on the Web


The world-wide web is the most popular medium for hosting distributed applications.
Increasingly, people are using the web to book airline tickets, purchase a host of consumer
goods, make hotel reservations, and so on. The browser acts as a general purpose client that
can interact with any application that talks to it using the Hyper Text Transfer Protocol
(HTTP).
One major characteristic of a web-based application system is that the client (the browser),
being a general-purpose program, typically does no application-related computation at all. All
business logic and data processing take place at the server. Typically, the browser receives
web pages from the server in HTML and displays the contents according to the format, a
number of tags and values for the tags, specified in it. In this sense, the browser simply acts
as a ‗dumb‘ program displaying whatever it gets from the application and transmitting user
data from the client site to the server
The HTML program shipped from a server to a client often needs to be customised: the code
has to suit the context. For example, when we make a reservation on a flight, we expect the
system to display the details of the flight on which we made the reservation. This requires
that HTML code for the screen be dynamically constructed.
This is done by code at the server. For server-side processing, there are competing
technologies such as Java Server Pages and Java Servlets, Active Server Pages (ASP), and
PHP. In this book we study Java Servlets.
HTML and Java Servlets
We have stated earlier, any system that ultimately displays web pages via a browser has to
create HTML code. HTML code displays text, graphics such as images, links that users can
click to move to other web pages, and forms for the user to enter data. We will now describe
the essential code for doing these.
An HTML program can be thought of as containing a header, a body, and a trailer. The
header contains code like the following:

The first four lines are usually written as given for any HTML file. We do not elaborate on
these, but observe words such as html and head that are enclosed between angled brackets (<
and >). They are called tags. HTML tags usually occur in pairs: start tag that begins an entry
and end tag that signals the entry‘s end. For example, the tag begins the header and is ended
by. The text between the start and end tags is the element content.
In the fifth line we see the tag title, which defines the string that is displayed in the title bar.
The idea is that the string A Web Page will be displayed in the title bar of the browser when
this page is displayed.

Software Architecture & Design Pattern (21CS741) Page | 133


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

As a sample body, let us consider the following

The body contains code that determines what gets displayed in the browser’s window.
Some tags may have attributes, which provide additional information. For example, see the
line

The body contains code to display the string An Application in the font Lucida bright,
bolded, italicised, and in blue color.

The last line of the file is

Obviously, it ends the HTML file


Entering and Processing Data
Web pages that allow the user to enter information that the system processes. For example, a
search engine provides a field in which we type in some search terms. When an
accompanying button is clicked, the system transfers control to the search engine that
displays results of the search.

This is accomplished by using what is called a form tag in HTML. The complete code that
allows us to enter some piece of text in the web page is given below.

Software Architecture & Design Pattern (21CS741) Page | 134


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

Let us get a general understanding of the above piece of code. Consider the code that begins
with the line

The tag form begins the specification of a set of elements that allow the user to enter
information. The action attribute specifies that the information entered by the user is to be
processed by a Java class called ProcessInput.class, which resides in the package apackage.

There are two primary ways in which form data is encoded by the browser: one is GET and
the other is POST. GET means that form data is to be encoded into a URL while POST
makes data appear within the message itself. See Fig. 12.6 for the considerations in deciding
which of these methods should be used.

Software Architecture & Design Pattern (21CS741) Page | 135


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

The first line states that the data is HTML and the second line begins the HTML code. The
complete code for the servlet is given below.

Software Architecture & Design Pattern (21CS741) Page | 136


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

The architecture for serving web pages is depicted in Fig. 12.7. Assume that an HTML page
is displayed on the client‘s browser. The page includes, among other things, a form that
allows the user to enter some data. The client makes some entries in the form‘s fields and
submits them, say, by clicking a button. The data in the form is then transmitted to the server
and given to a Java servlet, which processes the data and generates HTML code that is then
transmitted to the client‘s browser, which displays the page.

Deploying the Library System on the World-Wide Web

Developing User Requirements


As in any system, the first task is to determine the system requirements. We will, as has been
the case throughout the book, restrict the functionality so that the system‘s size is
manageable.
1. The user must be able to type in a URL in the browser and connect to the library system.

2. Users are classified into two categories: superusers and ordinary members. Superusers are
essentially designated library employees, and ordinary members are the general public who
borrow library books. The major difference between the two groups of users is that
superusers can execute any command when logged in from a terminal in the library, whereas
ordinary members cannot access some ‗privileged commands‘. In particular, the division is as
follows:
(a) Only superusers can issue the following commands: add a member, add a book,
return a book, remove a book, process holds, save data to disk, and retrieve data from
disk.
(b) Ordinary members and superusers may invoke the following commands: issue and
renew books, place and remove holds, and print transactions.
(c) Every user eventually issues the exit command to terminate his/her session.

3. Some commands can be issued from the library only. These include all of the commands
that only the superuser has access to and the command to issue books.

4. A superuser cannot issue any commands from outside of the library. They can log in, but
the only command choice will be to exit the system.

5. Superusers have special user ids and corresponding password. For regular members, their
library member id will be their user id and their phone number will be the password.

Interface requirements It turns out that due to the nature of the graphical user interface, an
arbitrarily large number of sequences of interactions are possible between the user and the
interface

Software Architecture & Design Pattern (21CS741) Page | 137


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

Logging in and the Initial Menu


In Fig. 12.8, we show the process of logging in to the system. When the user types in the
URL to access the library system, the log in screen that asks for the user id and password is
displayed on the browser. If a valid combination is typed in, an appropriate menu is
displayed. What is in the menu depends on whether the user is an ordinary member or a
superuser and whether the terminal is in the library or is outside.
1. The Issue Book command is available only if the user logs in from a terminal in the library.
2. Commands to place a hold, remove a hold, print transactions, and renew books are
available to members of the library (not superusers) from anywhere.
3. Certain commands are available only to superusers who log in from a library terminal:
these are for returning or deleting books, adding members and books, processing holds, and
saving data to and retrieving data from disk.

When the user types in the URL for the library, the system presents a log-in screen for
entering the user id and password. If the user types in a bad user id/password combination,
the system presents the log in screen again with an error message.
On successful validation, the system displays a menu that contains clickable options. The
Command State in Fig. 12.8 denotes the general flow of a command. When a certain
command is chosen, we enter a state that represents the command. How the transitions take
place within a command obviously depends on what the command is. All screens allow an
option to cancel and go back to the main menu. If this option is chosen, the system goes on to
display the main menu awaiting the next command.
When the exit command is chosen, the system logs the user out and presents the log in screen
again.
Add Book
The flow is shown in Fig. 12.9. When the command to add a book is chosen, the system
constructs the initial screen to add a book, which should contain three fields for entering the
title, author, and id of the book, and then display it and enter the Add Book state. By clicking
on a button, it should be possible for the user to submit these values to system. The system
must then call the appropriate method in the Library class to create a Book object and enter it
into the catalog. The result of the operation is displayed in the Command Completed state.

Software Architecture & Design Pattern (21CS741) Page | 138


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

From the Command Completed state, the system must allow the user to add another book or
go back to the menu. In the Add Book state, the user has the option to cancel the operation
and go back to the main menu.
Add Member, Return Book, Remove Book
The requirements are similar to the ones for adding books. We need to accept some input
(member details or book id) from the user, access the Library object to invoke one of its
methods, and display the result. So we do not describe them here nor do we give the
corresponding state transition diagrams.
Save Data
When the data is to be written to disk, no further input is required from the user. The system
should carry out the task and print a message about the outcome. The state transition diagram
is given in Fig. 12.10.

Retrieve Data
The requirements are similar to those for saving data.
Issue Book
This is one of the more complicated commands. As shown in the state transition diagram in
Fig. 12.11, a book may be checked out in two different ways: First, a member is allowed to
check it out himself/herself. Second, he/she may give the book to a library staff member, who
checks out the book for the member. In the first case, the system already has the user‘s
member id, so that should not be asked again. In the second case, the library staff member
needs to input the member id to the system followed by the book id
After receiving a book id, the system must attempt to check out the book. Whether the
operation is successful or not, the system enters the Book Id Processed state.

Software Architecture & Design Pattern (21CS741) Page | 139


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

A second reason for the complexity arises from the fact that any number of books may be
checked out. Thus, after each book is checked out, the system must ask if more books need to
be issued or not. The system must either go to the Get Book Id state for one more book id or
to the Main Menu state.
As usual, it should be possible to cancel the operation at any time.

Place Hold, Remove Hold, Print Transactions


The requirements for these are similar to those for issuing a book, so we omit their
description.
Renew Books
The system must list the title and due date of all the books loaned to the member. For each
book, the system must also present a choice to the user to renew the book. After making the
choices, the member clicks a button to send any renew requests to the system. For every book
renewal request, the system must display the title, the due date (possibly changed because of
renewal), and a message that indicates whether the renewal request was honoured. After
viewing the results, the member uses a link on the page to navigate to the main menu. The
state transition diagram is given in Fig. 12.12.

Software Architecture & Design Pattern (21CS741) Page | 140


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

Design and Implementation


To deploy the system on the web, we need the following:
1. Classes associated with the library; you will recall that this includes classes such as
Library, Member, Book, Catalog, and so on.
2. Permanent data (created by the save command) that stores information about the members,
books, who borrowed what, holds, etc.
3. HTML files that support a GUI for displaying information on a browser and collecting data
entered by the user. For example, when a book is to be returned, a screen that asks for the
book id should pop up on the browser. This screen will have a prompt to enter the book id, a
space for typing in the same, and a button to submit the data to the system.
4. A set of files that interface between the GUI and the objects that actually do the processing
Servlets will be used to accomplish this task.
Structuring the files
HTML code for delivery to the browser can be generated in one of two ways:
1. Embed the HTML code in the servlets. This has the disadvantage of making the servlets
hard to read, but more dynamic code can be produced.
2. Read the HTML files from disk as a string and send the string to the browser. This is less
flexible because the code remains static
Examples of HTML file fragments
To show how this approach works in practice, consider the two commands, one for returning
and the other for removing books. In both, the user must be presented with a web page that
asks him/her to enter a book id. We have just one file that displays this page. However, the
servlet that needs to be invoked will change depending on the context. Therefore, we code the
servlet name as below.

A similar approach is taken for accepting member ids.


For every web page, the header should display a title that depends on the context. We
maintain just one file for the header. This file has a string TITLE that stands for the title of
the web page. Depending on which page is being displayed, TITLE is replaced by an
appropriate string, which gets displayed in the title bar.
When a command is completed, we need to display a web page. For most commands, the
data to be displayed is small enough that it can be thought of as a simple string. We,
therefore, employ just one file, commandCompleted.html, to carry out this task. This file is
adapted, however, in two different ways.
1. The result to be displayed will vary on the command as well as whether the operation was
successful. To take care of this, the file has a string called RESULT.

Software Architecture & Design Pattern (21CS741) Page | 141


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

This may be replaced by strings such as Book not found and Member added. Once the file is
read into a string, the RESULT string is replaced by the appropriate result of executing the
command. The following pseudocode gives the idea.

2. To reduce the number of mouse clicks, the user may be given the option to repeat the
command whose result is displayed by the commandCompleted.html file. For example, after
completing the Add Book command, we need to give an option to issue the command once
again so that the user can add another book. Since the code where control should go to
depends on the command that was just executed, some adaptation is in order. This is
facilitated by having the line

in the HTML file.


In the case of Add Member, we substitute REPLACE_COMMAND by Add Book, which
provides a link that the user can click, and REPLACE_JS by addmemberinitialization, which
locates the Java class that is given the control when the link is clicked.

Configuration
The server runs with the support of Apache Tomcat, which is a servlet container. A servlet
container is a program that supports servlet execution. The servlets themselves are registered
with the servlet container. URL requests made by a user are converted to specific servlet
requests by the servlet container. The servlet container is responsible for initialising the
servlets and delivering requests made by the client browser to the appropriate servlet.
The directory structure is as in Fig. 12.13. We store the HTML files in a directory named
Library, which is a subdirectory of webapps, which, in turn, is a subdirectory of the home
directory of Tomcat. The servlets are in the package library, which is stored in Library/WEB-
INF/classes. The implementation of the backend classes such as Member, Catalog, etc. is in
the package basicImplementation.

Software Architecture & Design Pattern (21CS741) Page | 142


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

Our implementation requires that the user create an environment variable named LIBRARY-
HOME that has as value the absolute path name of the directory that houses the HTML files.
The deployment descriptor elements are defined in a file called web.xml. While this file
permits a large number of tags, our use of them is limited to mapping the URLs to servlets.
To understand how this is done, first examine the following lines of XML code.

in the HTML file, the string login is mapped to the servlet name LoginServlet.
But the servlet name given by the tag is just a name that is mapped to the fully-qualified class
name of the servlet as below.

Structure of servlets in the web-based library system


A servlet receives data from a browser through a HttpServletRequest object. This involves
parameter names and their values, IP address of the user, and so on. For example, when the
form to add book is filled and the Add button is clicked, the servlet‘s doPost method is
invoked. As we have seen earlier, this method has two parameters: a request parameter of
type HttpServletRequest and a response parameter of type HttpServletResponse.
Each command is organised as a combination of one to three servlets. They need a number of
common utility functions during the course of processing. These methods and doPost and

Software Architecture & Design Pattern (21CS741) Page | 143


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

doGet are collected into a class named LibraryServlet. This class has the structure shown in
Fig. 12.14.

Most of the methods of LibraryServlet fall into one of five categories:


1. One group contains methods that store information about the user. This information
includes the user id, the type of terminal from which the user has logged in, etc. and are
stored in attributes associated with the session object. The methods are addAttribute,
setAttribute, getAttribute, and deleteAllAttributes.
2. Methods to validate users and help assess access rights. The validateSuper User method
checks whether the user is a superuser and validateOrdinary Member does the same job for
ordinary members. The method library Invocation returns true if and only if the user has
logged in from a terminal located within the library.
3. The getFile method reads an HTML file and returns its contents as a String object.
4. The fourth group of methods are used for handling users who may have invoked a
command without actually logging in. The method notLoggedIn returns true if and only if the
user has not currently logged in. The method noLoginError Message returns HTML code that
displays an error message when a person who has not logged in attempts to execute a
command.
5. The final group of commands deal with processing the request and responding to it. The
doGet message calls doPost, which does some minimal processing needed for all commands
and then calls the abstract runmethod, which individual servlets override.

Software Architecture & Design Pattern (21CS741) Page | 144


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

Execution flow Processing a request sometimes involves simply generating an HTML page,
which is quite straightforward. This is best understood by following a sample command. We
choose as example, the command to remove a book. A somewhat simplified sequence of
what takes place in the course of the execution of this command is shown in Fig. 12.15.

Discussion and Further Reading


RMI provides a level of abstraction much higher than the traditional communication
mechanism in networks, viz. sockets. A socket is an endpoint of a communication channel to
or from which data is transmitted in the network. Sockets are analogous to phones and a
socket allocated on a machine is uniquely associated with a process running on it. The type of
socket associated with a process depends on the transport layer in use (TCP or UDP, for
example). A socket can have an associated port number using which processes may send
messages to it. Socket programming is possible in many modern programming languages
including C and Java
The Common Object Request Broker Architecture (CORBA), standardised by the Object
Management Group (OMG), is another approach to distributed object-based computing. It
allows a distributed, heterogeneous collection of objects to interoperate, and automates many
common network programming tasks such as object registration, location, and activation,
error-handling, parameter marshalling and demarshalling, security control and concurrency
control.
Like RMI, the services that a CORBA object provides are defined by its interface. Again, as
in RMI, object references are really of interface types. The Object Request Broker (ORB) is
responsible for delivering requests from a client to a remote object and to return the results.
The Java Servlet technology is just one of the tools available for creating web-based systems.
PHP is a scripting language that usually runs on the server side. It can have HTML code
embedded into it and outputs web pages. ASP.NET is another competing scripting
technology from Microsoft for building web-based applications. JSP is similar to PHP and

Software Architecture & Design Pattern (21CS741) Page | 145


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

ASP, the difference being that we intersperse Java code with HTML code to create dynamic
web pages. Other technologies such as Ruby on Rails (RoR) are also available.

A Note on Input and Output


Inputting numeric values through the keyboard has been a problem in Java. We need to read a
string and extract a number from it. One way of inputting data is through a graphical user
interface (GUI). A class called JOptionPane has a method named showInputDialog, which
can be used for accepting a String. The String can then be parsed to retrieve the proper value.
String response;
response = JOptionPane.showInputDialog("Enter a number");
int num = Integer.parseInt(response);

The code opens up a dialog box for entering a string. After inputting the data, the user can
click ―O.K.‖ The string is stored in response, which is parsed by the code

Integer.parseInt(response);

It returns the integer value stored in the string. (If the string does not have an integer in it, it
would cause an ―exception.‖)

Messages can also be displayed in a window using the method showMessageDialog in


JOptionPane. The format is

JOptionPane.showMessageDialog(null, message-as-a-string);

Selection Statements

Java supports if else statements and switch statements. Both allow nesting. The syntax of the
if else statement is

The else part is optional.

Here is a program that accepts the age of a person and prints out whether the person is
eligible to vote.

Software Architecture & Design Pattern (21CS741) Page | 146


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

The next example selects people younger than 20 and all females over 30.

The switch statement allows us to handle the situation when there are numerous cases. Here
is an example.

Software Architecture & Design Pattern (21CS741) Page | 147


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

Loops

Java, like C and C++, allows three types of loops: for, while, and do.

while The while loop has a simple syntax.

The statement is executed as long as the condition is true. Before each iteration, the condition
is checked. If it is true, the loop is executed once and the condition is checked once again and
the process repeats until the condition is false.

Here are some examples of the use of while loop.

for The for loop has the following syntax

The code works as follows:

1. Evaluate expression1.
2. Evaluate condition.
3. If the evaluation in (2) returns true, enter the loop and execute the statement, which can be
a block. Otherwise, exit the loop.
4. Evaluate expression2.
5. Go to (2) above.

do The do loop executes at least once. At the end of the first and succeeding iterations, a
condition is checked. If the condition is true, the next iteration is performed. The syntax is

The following example makes the user enter ―Yes‖, ―No‖, or ―cancel‖ (caseinsensitive).

Software Architecture & Design Pattern (21CS741) Page | 148


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

Arrays

Java supports the creation of arrays of any number of dimensions. The process of creating an
array can be thought of as consisting of two steps:

1. Declare a variable that refers to the array. This is not the array itself, but eventually
contains the address of the array, which has to be dynamically allocated.

2. Allocate the array itself and make the variable declared in (1) above to point to this array.

The following code creates a variable that can serve as a reference to an array of integers

An array of five integers is created during execution by the following code.

The new operator returns the address of the array; this is termed the reference in Java. We
make a hold the reference to the array by writing

The first cell of the array is indexed by 0. If the array has n elements, the last cell is indexed
n − 1.

Array cells are referred by the notation a[index].

The following code stores 1 in a[0], 2 in a[1], etc. and then prints these values.

The following program reads in a sequence of numbers and prints them in reverse. The
number of numbers is the first number read in. An array large enough to hold the sequence is
then allocated.

Software Architecture & Design Pattern (21CS741) Page | 149


Cauvery Institute of Technology, Mandya Department of Computer Science & Engineering

Multi-dimensional Arrays

Let us look at an example of creating multi-dimensional arrays, which will suggest how to
allocate arrays of higher dimension.

Software Architecture & Design Pattern (21CS741) Page | 150

You might also like