DT Notes Unit II
DT Notes Unit II
J2EE
1.1 Introduction
Java 2 Enterprise Edition, or J2EE, is a package of specifications aligned to enable the
development of multi-tier enterprise applications. The specifications outline the various
components needed within a J2EE enterprise system, the technologies for accessing and providing
services, and even the roles played during the development, deployment, and runtime lifecycle.
The combination of these specifications introduced faster and more streamlined development
processes, to the software industry, that have been mapped onto common software methodologies
such as RUP, XP, and others.
J2EE has fast become the de facto standard for developing and deploying enterprise systems. It
represents Sun’s attempt to take their Java mantra of “Write Once, Run Anywhere” to the next
level and make it “Write Once, Deploy Anywhere.” While using it is not as easy as dropping
new code fragments into existing code, J2EE has made significant strides in easing the burden on
the developers and deployers of a system.
J2EE Architecture
J2EE, introduced in 1998, defines a multi-tier architecture for enterprise information
systems (EIS). By defining the way in which multi-tier applications should be developed, J2EE
reduces the costs, in both time and money, of developing large-scale enterprise systems. Figure
1.5 illustrates the J2EE architecture.
The J2EE platform specifies the logical application components within a system and defines the
roles played in the development process.
PAGE NO. 1
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
Application components
Four application components are defined within the J2EE platform. They are as follows.
✦ Application clients (Standalone Java clients)
✦ Applets (Java code which executes within a browser)
✦ Web components (JSPs, Servlets)
✦ Server components (EJBs, J2EE API implementations)
A product does not need to support all types of components the norm is to provide an
implementation to support a particular component type. However, all components are similar in
that they run within a container. The container is responsible for providing the runtime
environment, the mechanism for identifying and understanding the file formats used for
deployment, and the standard services for application components to use.
The four application components are discussed in the following sections.
Application clients
Clients are generally stand-alone applications written in Java. They run within a virtual
machine and can use the J2EE standard services to access components located within another
tier. The J2EE standard services are usually provided on the client via an installation of J2SE, or
along with the distribution of the application itself.
Applets
Applets are similar to application clients, but execute within a Web browser. Initially
applets garnered extensive attention, as they were seen as a means of making Web pages more
dynamic. Most Web browsers have an embedded Java Virtual Machine (JVM) however, the Java
plugin can be used to force the browser to use a particular version of JVM.
Web components
Although the term can be misleading, Web components do not execute on the client side.
Web components are server-side components, generally used to provide the presentation layer to
be returned to a client. Two types of Web components exist: Java Server Pages (JSPs) and Java
servlets. Very basically, JSPs are similar to regular HTML pages but contain embedded Java
code while Java servlets are Java classes that use Java’s I/O application programming interfaces
(APIs) to output HTML to the client. Both JSPs and servlets can be used to output other format
PAGE NO. 2
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
types.
Server components
Server components come in the form of Enterprise JavaBeans (EJBs). EJBs execute within
a container that manages the runtime behavior of the EJB. EJBs are usually where the business
logic for an enterprise system resides.
Roles
The roles specified within the J2EE are those played during the development and
deployment cycles of an enterprise application. While the roles are distinct, in reality multiple roles
tend to be filled by the same organization.
The following roles are discussed in this section.
✦ J2EE product provider
✦ Application component provider
✦ Application assembler
✦ Deployer
✦ System administrator
✦ Tool provider
✦ System component provider
The J2EE product provider
A J2EE product provider is a company that provides a product that implements a part of
the J2EE specification. For example, one company may provide a product that implements the
J2EE container for EJBs, and another may provide a product that provides an implementation for
a JMS server.
The application component provider
An application component provider is a developer who creates a component that isintended
to reside within one of the J2EE containers. The application component providerdevelops application
components adhering to the J2EE API specifications with the intention that the component will be
deployed within a J2EE Server. This enables a developer to select a different J2EE product provider
without modifying the component. Application componentproviders develop a range of components,
including EJBs, HTML pages, and other Web components.
PAGE NO. 3
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
PAGE NO. 4
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
PAGE NO. 5
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
The .NET Framework is a managed environment. The common language runtime monitors
the execution of .NET applications and provides essential services. It manages memory, handles
exceptions, ensures that applications are well-behaved, and much more. Language interoperability
is one goal of .NET. .NET languages share a common runtime (the common language runtime, a
common class library), the Framework Class Library (FCL), a common component model, and
common types. In .NET, the programming language is a lifestyle choice. Except for subtle
differences, C#, VB.NET, or JScript.NET offer a similar experience.
Common Language Runtime (CLR)
The CLR is the heart of .NET framework. It is .NET equivalent of Java Virtual Machine
(JVM). It is the runtime that converts a MSIL (Micro Soft Intermediate Language) code into the
host machine language code, which is then executed appropriately.
The CLR provides a number of services that include
• Loading and execution of codes
• Memory isolation for application
• Verification of type safety
• Compilation of IL into native executable code
• Providing metadata
• Automatic garbage collection
• Enforcement of Security
• Interoperability with other systems
• Managing exceptions and errors
• Provide support for debugging and profiling
Common Type System (CTS)
The language interoperability, and .NET Class Framework, are not possible without all the
language sharing the same data types. What this means is that an “int” should mean the same in
VB, VC++, C# and all other .NET compliant languages. Same idea follows for all the other data
types. This is achieved through introduction of Common Type System (CTS).
CTS, much like Java, define every data type as a Class. Every .NET compliant language must stick
to this definition. Since CTS defines every data type as a class this means that only Object-
Oriented (or Object-Based) languages can achieve .NET compliance.
PAGE NO. 6
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
PAGE NO. 7
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
Managed Code
The role of CLR doesn‘t end once we have compiled our code to MSIL and a JIT compiler
has compiled this to native code. Code written using the .NET framework, is managed code when
it is executed. This stage is usually referred to as being at runtime. This means thatthe CLR
looks after our applications, by managing memory, handling security, allowing cross language
debugging and so on. By contrast, applications that do not run under the control of the CLR are
said to be unmanaged and certain languages such as C++ can be used to write such applications,
that for example, to access low level functions of the operating systems. Howeverin C# we can
only write code that runs in a managed environment.
Unified classes
The term .NET framework refers to the group of technologies that form the development
foundation for the Microsoft .NET platform. The key technologies in this group are the run time
and the class libraries. The run time is responsible for managing your code and providing
services to it while it executes, playing a role similar to that of the Visual Basic 6.0 run time. The
.NET programming languages including Visual Basic .NET, Microsoft Visual C# and C++
managed extensions and many other programming languages from various vendors utilize .NET
services and features through a common set of unified classes.
The .NET unified classes provide foundation of which you build your applications, regardless of
the language you use. Whether you simply concating a string, or building a windows Services or
a multiple-tier web-based applications, you will be using these unified classes.
The unified classes provide a consistent method of accessing the platforms functionality. Once you
learn to use the class library, you will find that all tasks follow the same uniform architecture, you
no longer need to learn and master different API architecture to write your applications.
By building your applications on a unified, integrated framework, you maximize your return on
the time you spend learning this framework, and you end up with more robust applications that are
easy to deploy and maintain.
PAGE NO. 8
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
To compile applications for .NET, compilers take source code as input and produce MSIL as
output.
• MSIL itself is a complete language that you can write applications in. However, as with
assembly language, you would probably never do so except in unusual circumstances. Because
MSIL is its own language, each compiler team makes its own decision about how much of the
MSIL it will support. However, if you're a compiler writer and you want to create a language that
does interoperate with other languages, you should restrict yourself to features specified by the
CLS. See the figure 1.6.1
• You write source code in C# and compile it using the C# compiler (csc.exe) into an
EXE.
• The C# compiler outputs the MSIL code and a manifest into a read-only part of the
EXE that has a standard PE (Win32-portable executable) header. When the compiler
creates the output, it also imports a function named _ CorExeMain from the .NET
runtime.
PAGE NO. 9
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
• When the application is executed, the operating system loads the PE, as well as
any dependent dynamic-link libraries (DLLs), such as the one that exports the _
CorExeMain function (mscoree.dll), just as it does with any valid PE.
Framework Base Classes
The .NET Framework has an extensive set of class libraries. This includes classes for
• Data Access: High Performance data access classes for connecting to SQL
Server or anyother OLEDB provider
• XML Supports: Next generation XML support that goes far beyond the
functionality ofMSXML
• Directory Services: Support for accessing Active Directory/LDPA using ADSI
• Regular Expression: Support for above and beyond that found in Perl 5
• Queuing Supports: Provides a clean object-oriented set of classes for
working withMSMQ
These class libraries use the CLR base class libraries for common functionality.
Base Class Libraries
The Base class library in the .NET Framework is huge. It covers areas such as
• Collection : The System.Collections namespaes provides numerous collection
classes
• Thread Support: The System.Threading namespace provides support for creating
fast, efficient, multi-threaded application
• Code Generation: The System.CodeDOM namespace provides classes for
generating source files in numerous languages. ASP.NET uses these classes when
converting ASP.NET pages into classes, which are subsequently compiled
• IO: The System.IO provides extensive support for working with files and all other
streamtypes
• Reflection: The System.Reflection namespace provides support for load
assemblies,examining the type with in assemblies, creating instances of types, etc
• Security: The System.Security namespace provides support for services such as
authentication, authorization, permission sets, policies, and cryptography. These
base services are used by application development technologies like ASP.NET to
build their security infrastructure
PAGE NO. 10
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
Model-View-Controller (MVC) Architecture
The MVC paradigm provides a pattern for separating the presentation logic
(view),business logic (control), and data objects (model). J2EE’s architecture maps onto
the MVC nicely. Typically, entity beans are used to provide the model logic, while a mix
of entity beans and session beans are used to provide the control logic, and Web
components are used to implement both control and presentation logic. In practice,
however, the separation of the three types of logic is not as distinct, and additional patterns
are often needed to support the development cycle. Figure 1.7 shows how the three
different logical functional blocks work together.
The model
The M in MVC refers to the data object model. For example, in an airline ticketing
service you may have the concept of a booking, which in the real world is represented by
a paperticket. The model deals with issues such as how the booking is represented within
the software system, where it is persisted, and how it is accessed. For example, the
booking may be held within a relational database within a table named Bookings with
the fields PassengerName, DepartureCity, DestinationCity, TravelDate, and
DepartureTime. This data may be accessedvia JDBC using Entity Beans.
The view
The view is responsible for presentation issues. It handles how the client will see
the application, and so HTML issues are usually deal with here. However, other markup
PAGE NO. 11
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
languages such as Wireless Markup Language (WML) and Extensible Markup Language
(XML) are increasingly being used to support more varied types of clients. The Booking
example may be displayed in various ways. For example, on a wireless device only the
most relevant information might be displayed due to the limited screen size. In fact, the
term view may be misleading, implying that it is meant for visual display only, the view
may also be used to present the model via an audio interface if desired. The method in
which the model is presented is abstracted from the underlying data.
The control
The control part of the paradigm deals with the business logic of the application.
It handles how and when a client interacting with the view is able to access the model.
The control layer usually interacts with authorization and authentication services, other
J2EE services, and external systems to enforce the business rules to be applied to the
application. In our Booking example, the control would determine whether the view can
actually display the model. This maybe based on whether the user is logged in, if he or
she has appropriate authorization and so on. It would also hold the business logic of what
to do if the user attempts to view a booking that no longer exists for example, should an
error be presented to the user? Should the user be prompted with a screen asking for
additional information? These are rules that change within the business but they do not
necessarily force a change on the view or model. To support the MVC, the J2EE
architecture also provides a varied set of APIs to help facilitate the separation
between themodel, view, and control functional blocks within an application.
Java Beans
JavaBeans is one of the reusable class code elements often applied on java-based
software application programming. Here, Beans can be defined as a way of encompassing
multiple objects beneath a single object. The JavaBeans class components can be labelled
as methods, events, properties and persistence. This type of class is advantageous over
others due to its notable reusability characteristics, enables object communication,
platform-independent, shorter time consumption, requires lesser manual efforts, etc.
PAGE NO. 12
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
Understanding JavaBeans
If you are still confused, then consider the example of a worker assembling a car, and
the radio is one of the components. Instead of manufacturing the radio from scratch, a worker
can directly assemble the radio to the car. This eliminates the need for manufacturing a radio
for assembly.
Components of JavaBeans
Components of Java Beams are nothing but the class that contains the definition of
beans. These classes have specific design convections. It consists of events, methods,
persistence, properties. Non-GUI based and GUI based components are the two types of
components. The classes are discussed below:
• Properties: This attribute consists of a label, font, color, size, display size, etc. It
determines behavior, appearance, and the state of Beam.
• Methods: Methods in JavaBeans are similar to the normal Java methods. It does have
any specific naming convention.
• Events: Events in JavaBeans is similar to Swing event handling.
• Persistence: It helps in storing the state of JavaBean.
PAGE NO. 13
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
delivers real-time price information about the stock market is also one of the examples of
this.
Beans can also be used to give interactive capabilities to web-based applications. Beans can
run on any operating system. It can run inside numerous application environments such as word
processors, browser and many other applications. A UI component such as trees, graphical
buttons, and trees gives a lively touch to applications and eliminates the need to write custom
components. One can develop the JavaBeans with Beans Development Kit (BDK) from Sun.
Advantages of JavaBeans
1. Run anywhere; write once- Java Beans eliminates the need for writing software again and
again.
2. One can work with JavaBeans on Numerous local platforms, which make it valuable in the
global market.
3. JavaBeans can capture the events that are sent by other objects. It enables object
communication.
4. An application developer is able to control Methods, events, and the properties of any Java
beans.
5. Zero hassle at run time with auxiliary software; one can configure beans during design time.
6. Reusable: One can make a persistence configuration setting in JavaBeans. Persistent storage
and restoration later can save the configuration setting of the bean.
PAGE NO. 14
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
Career Scope in this Technology
In today’s interconnected world, programming language plays a pivotal role in the business
world. Java is considered as one of the reliable programming languages in the world. It is a
very popular language among developers. The scope and carriers in developing JavaBeans
expected to rise in the coming years. Professionals with Java skills have a lot of opportunities
in these sectors. Today Java is the leading programming language in the back-end market and
application development. The future looks promising for all java professionals across the globe.
Millions of Java Developers prefer Java for its usability, practicality, compatibility, and ease
of use.
There is plenty of Java opportunities present across the globe. Numerous reputed companies
like Microsoft, Amazon web services, IBM, Oracle, and many others are always looking for
Java professionals.
JavaBean Professionals can make a flourishing carrier in Information Technology. Everyone
has different reasons for picking up different technology, but having knowledge of JavaBeans
surely benefits one’s skill set and offers a lot of growth opportunities and money.
Introduction
• EJB stands for enterprise java beans.
• EJB java beans is server side component.
• EJB is an essential part of J2EE platform
• J2EE application containers contains the components that can be used by client for
executing business logic
• EJB mainly contains the business logic and business data.
• EJB components always lie in some container which is called EJB container EJB
component is an EJB class which is written by developer that implement business logic.
• It is a specification provided by Sun Microsystem to develop secured , robust and scalable
distributed application.
• To run EJB application, you need application server (EJB container) such as, Jboss,
Glassfish.
• EJB application is deployed on the server, so is called server side component also
PAGE NO. 15
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
• EJB container performs: -life cycle management -Security -Transaction management -
object pooling
EJB Architecture Java
Java beans incorporate a set of objects into one accessible object that can be accessed
easily from any application. This single accessible object is maintainable, customizable,
and reusable. The setter/getter method and the single public constructor are used to
govern that single accessible object. We can update and read the value of any variable
of any object by using the setter and getter, respectively.
The EJB stands for Enterprise Java beans that is a server-based architecture that
follows the specifications and requirements of the enterprise environment. EJB is
conceptually based on the Java RMI(Remote Method Invocation) specification. In EJB,
the beans are run in a container having four-tier architecture. This architecture consists
of four layers, i.e., Client layer, Web layer, Application layer, and Data layer.
Architecture
The EJB architecture has two main layers, i.e., Application Server and EJB Container,
based on which the EJB architecture exist. The graphical representation of the EJB
architecture is given below.
In the above diagram, the logical representation of how EJBs are invoked and deployed
by using RMI(Remote Method Invocation) is defined. The containers of the EJB cannot
be self-deployed. In order to deploy the containers, it requires the Application server.
Application Server
In the EJB architecture, the Application server is the outermost layer that holds or
contains the Container to be deployed. The application layer plays an important role
in executing the application developed using the beans. It provides the necessary
environment to execute those applications. Some most popular application servers are
PAGE NO. 16
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
Web-logic, Tomcat, JBoss, Web-sphere, Wildfly, and Glass-finish. The main tasks of the
application server are:
1. Manage Interfaces
2. Execution of the processes
3. Connecting to the database
4. Manage other resources.
Container
In EJB architecture, the Container is the second outermost layer. It is a very important
layer for enterprise beans that are contained in it. For the enterprise bean, the
Container provides various supporting services, which are as follows:
o It provides support for transactional services such as registering the objects, assign
remote interfaces, purge the instances.
o It provides support for monitoring the object's activities and coordinating distributed
components.
o It provides support for security services.
o It provides support for the pooling of resources.
o It provides support for managing the Life-cycle of beans and their concurrency.
o It provides support to concentrate on business logic.
Beans
Java beans of the enterprise are installed in the Container in the same way as a Plain
old java object (POJO) is installed and registered to the Container. For developing
secured, large scale and robust business applications, beans provide business logic.
Types of EJB
There are three types of Enterprise Java Beans or EJB available, which are as follows:
Stateless EJB
In order to implement the stateless business logic, the stateless EJBs are primarily
used. Storing a user's physical address into an inventory system's database is an
example of the stateless EJB. In addition, a stateless bean would be perfect for this type
PAGE NO. 17
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
of business logic because it is not necessary to have more than two forms on the user
interface at all stages of the transaction.
StatelessBeanExample.java
1. package com.javatpoint.ejbarchitecture;
2. import javax.ejb.Stateless;
3.
4. @Stateless
5. public class StatelessBeanExample {
6. }
Stateful EJB
The Stateful EJB is just opposite to the Stateless EJB. The Stateful EJBs are used
when we have to maintain the state of the application on the backend during the user
session. The shopping cart of an online shopping application is an example of
the Stateful EJB. In order to achieve such application, we will use the following steps:
StatelessBeanExample.java
1. package com.javatpoint.ejbarchitecture;
2. import javax.ejb.Stateful;
3.
4. @Stateful
5. public class StatelessBeanExample {
6. }
Message-driven EJB
Another special type of EJB that is used for sending and receiving messages from
message brokers implements the JMS specification. The systems based on the broker
are loosely coupled. The components that communicate through the broker have the
PAGE NO. 18
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
advantage of not waiting for one request to finish before submitting another request
because the broker by nature is asynchronous.
MessageDrivenBeanExample.java
package com.javatpoint.ejbarchitecture;
import javax.ejb.MessageDriven;
import javax.jms.MessageListener;
import javax.jms.Message;
import javax.ejb.ActivationConfigProperty;
@MessageDriven(mappedName = "jms/Queue", activationConfig = {
@ActivationConfigProperty(propertyName = "reciverType", propertyValue = "ja
vax.jms.Queue"),
@ActivationConfigProperty(propertyName = "reciver", propertyValue =
"java:/queue/testqueue")
})
public class MessageDrivenBeanExample implements MessageListener {
public MessageDrivenBeanExample() {
}
public void onMessage(Message message) {
System.out.println("Message received successfully .");
}
}
Difference b/w EJB and JB
These are the following differences between EJB and JB:
S.No. EJB JB
1. EJB is not visible because it runs as a JB is visible.
remote.
2. EJB is executed on the server-side. EJB can execute on both the client-side
and the server-side.
3. EJB works with an external builder tool For interpreting the bean's
or its IDE. functionality, EJB uses its external
interface.
4. EJB uses the technology of components By using generic components created
but cannot build or extend it over by Java beans, it is able to build applet
beans. and application.
5. It has no property editor, customizer, It has property editors, customizers,
PAGE NO. 19
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
and bean information classes. It has and bean information classes.
only information about what is
provided by the deployment
descriptor.
6. It supports transactions. It doesn't support transactions.
7. Three types of EJB possible. No types.
The RMI (Remote Method Invocation) is an API that provides a mechanism to create
distributed application in java. The RMI allows an object to invoke methods on an
When the caller invokes method on the stub object, it does the following tasks:
1. It initiates a connection with remote Virtual Machine (JVM),
2. It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),
3. It waits for the result
4. It reads (unmarshals) the return value or exception, and
PAGE NO. 20
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
5. It finally, returns the value to the caller.
skeleton
The skeleton is an object, acts as a gateway for the server side object. All the incoming
requests are routed through it. When the skeleton receives the incoming request, it
does the following tasks:
skeletons.
Understanding requirements for the distributed applications
If any application performs these tasks, it can be distributed application.
.
1. The application need to locate the remote method
2. It need to provide the communication with the remote objects, and
3. The application need to load the class definitions for the objects.
The RMI application have all these features, so it is called the distributed application.
Java RMI Example
The is given the 6 steps to write the RMI program.
1. Create the remote interface
2. Provide the implementation of the remote interface
PAGE NO. 21
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
3. Compile the implementation class and create the stub and skeleton objects using the
rmic tool
4. Start the registry service by rmiregistry tool
5. Create and start the remote application
6. Create and start the client application
RMI Example
In this example, we have followed all the 6 steps to create and run the rmi application.
The client application need only two files, remote interface and client application. In
the rmi application, both client and server interacts with the remote interface. The
client application invokes methods on the proxy object, RMI sends the request to the
remote JVM. The return value is sent back to the proxy object and then to the client
application.
a remote interface that extends the Remote interface. There is only one method named
add() and it declares RemoteException.
PAGE NO. 22
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
1. import java.rmi.*;
2. public interface Adder extends Remote{
1. import java.rmi.*;
2. import java.rmi.server.*;
{
4. AdderRemote()throws RemoteException{
5. super();
6. }
invokes the RMI compiler and creates stub and skeleton objects.
1. rmic AdderRemote
4) Start the registry service by the rmiregistry tool
Now start the registry service by using the rmiregistry tool. If you don't specify the port
number, it uses a default port number. In this example, we are using the port number
5000.
1. rmiregistry 5000
PAGE NO. 23
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
5) Create and run the server application
Now rmi services need to be hosted in a server process. The Naming class provides
methods to get and store the remote object. The Naming class provides 5 methods.
In this example, we are binding the remote object by the name sonoo.
1. import java.rmi.*;
2. import java.rmi.registry.*;
5. try{
6. Adder stub=new AdderRemote();
7. Naming.rebind("rmi://localhost:5000/sonoo",stub);
8. }catch(Exception e){System.out.println(e);}
9. }
10. }
6) Create and run the client application
At the client we are getting the stub object by the lookup() method of the Naming
class and invoking the method on this object. In this example, we are running the
server and client applications, in the same machine so we are using localhost. If you
want to access the remote object from another machine, change the localhost to the
PAGE NO. 24
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
2. public class MyClient{
5. Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
6. System.out.println(stub.add(34,4));
7. }catch(Exception e){}
8. }
9. }
10. For running this rmi example,
11.
PAGE NO. 25
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
Output of this RMI example
PAGE NO. 26
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
Java Server Pages - JSP
JSP technology is used to create web application just like Servlet technology. It can be
A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to maintain
than Servlet because we can separate designing and development. It provides some
expression language and Custom tags in JSP, that makes JSP development easy.
2) Easy to maintain
JSP can be easily managed because we can easily separate our business logic with
presentation logic. In Servlet technology, we mix our business logic with the
presentation logic.
3) Fast Development: No need to recompile and redeploy
If JSP page is modified, we don't need to recompile and redeploy the project. The
Servlet code needs to be updated and recompiled if we have to change the look and
PAGE NO. 27
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
o Instantiation (Object of the Generated Servlet is created).
o Initialization ( the container invokes jspInit() method).
o Request processing ( the container invokes _jspService() method).
o Destroy ( the container invokes jspDestroy() method).
Note: jspInit(), _jspService() and jspDestroy() are the life cycle methods of JSP.
As depicted in the above diagram, JSP page is translated into Servlet by the help of
JSP translator. The JSP translator is a part of the web server which is responsible for
translating the JSP page into Servlet. After that, Servlet page is compiled by the
compiler and gets converted into the class file. Moreover, all the processes that happen
in Servlet are performed on JSP later like initialization, committing response to the
PAGE NO. 28
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
Creating a simple JSP Page
To create the first JSP page, write some HTML code as given below, and save it by .jsp
extension. We have saved this file as index.jsp. Put it in a folder and paste the folder in
the web-apps directory in apache tomcat to run the JSP page.
index.jsp
Let's see the simple example of JSP where we are using the scriptlet tag to put Java
code in the JSP page. We will learn scriptlet tag later.
1. <html>
2. <body>
5. </html>
It will print 10 on the browser.
How to run a simple JSP Page?
Follow the following steps to execute this JSP page:
o Start the server
o Put the JSP file in a folder and deploy on the server
o Visit the browser by the URL http://localhost:portno/contextRoot/jspfile, for example,
http://localhost:8888/myapplication/index.jsp
However, if you are using Bean class, Servlet or TLD file, the directory structure is
required.
PAGE NO. 29
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
PAGE NO. 30
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
• XML can be used to exchange the information between organizations
and systems.
• XML can be used for offloading and reloading of databases.
• XML can be used to store and arrange the data, which can customize
your data handling needs.
• XML can easily be merged with style sheets to create almost any
desired output.
• Virtually, any type of data can be expressed as an XML document.
What is Markup?
XML is a markup language that defines set of rules for encoding documents in a
format that is both human-readable and machine-readable. So what exactly is a
markup language? Markup is information added to a document that enhances its
meaning in certain ways, in that it identifies the parts and how they relate to each
other. More specifically, a markup language is a set of symbols that can be placed in
the text of a document to demarcate and label the parts of that document.
Following example shows how XML markup looks, when embedded in a piece of text
<message>
<text>Hello, world!</text>
</message>
XML - Syntax
In this chapter, we will discuss the simple syntax rules to write an XML document.
Following is a complete XML document −
You can notice there are two kinds of information in the above example −
• Markup, like <contact-info>
• The text, or the character data, Tutorials Point and (040) 123-4567.
The following diagram depicts the syntax rules to write different types of markup
and text in an XML document.
PAGE NO. 31
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
XML Declaration
The XML document can optionally have an XML declaration. It is written as follows
PAGE NO. 32
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
<element>....</element>
<element/>
XML References
References usually allow you to add or include additional text or markup in an XML
document. References always begin with the symbol "&" which is a reserved
character and end with the symbol ";". XML has two types of references −
• Entity References − An entity reference contains a name between the
start and the end delimiters. For example & where amp is name.
The name refers to a predefined string of text and/or markup.
• Character References − These contain references, such as A,
contains a hash mark (“#”) followed by a number. The number always
refers to the Unicode code of a character. In this case, 65 refers to
alphabet "A".
XML Text
The names of XML-elements and XML-attributes are case-sensitive, which means
the name of start and end elements need to be written in the same case. To avoid
character encoding problems, all XML files should be saved as Unicode UTF-8 or
UTF-16 files.
Whitespace characters like blanks, tabs and line-breaks between XML-elements and
between the XML-attributes will be ignored.
Some characters are reserved by the XML syntax itself. Hence, they cannot be used
directly. To use them, some replacement-entities are used, which are listed below −
Not Allowed Character Replacement Entity Character Description
XML - DTDs
The XML Document Type Declaration, commonly known as DTD, is a way to describe
XML language precisely. DTDs check vocabulary and validity of the structure of XML
documents against grammatical rules of appropriate XML language.
PAGE NO. 33
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
An XML DTD can be either specified inside the document, or it can be kept in a
separate document and then liked separately.
Syntax
Basic syntax of a DTD is as follows −
Syntax
<!DOCTYPE root-element [element-declarations]>
where root-element is the name of root element and element-declarations is where
you declare the elements.
Example
Following is a simple example of internal DTD −
<address>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</address>
PAGE NO. 34
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
External DTD
In external DTD elements are declared outside the XML file. They are accessed by
specifying the system attributes which may be either the legal .dtd file or a valid URL.
To refer it as external DTD, standalone attribute in the XML declaration must be set
as no. This means, declaration includes information from the external source.
Syntax
Following is the syntax for external DTD −
<!DOCTYPE root-element SYSTEM "file-name">
where file-name is the file with .dtd extension.
Example
The following example shows external DTD usage −
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>
<!DOCTYPE address SYSTEM "address.dtd">
<address>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</address>
The content of the DTD file address.dtd is as shown −
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
XML - Schemas
XML Schema is commonly known as XML Schema Definition (XSD). It is used to
describe and validate the structure and the content of XML data. XML schema defines
the elements, attributes and data types. Schema element supports Namespaces. It is
similar to a database schema that describes the data in a database.
Syntax
You need to declare a schema in your XML document as follows −
Example
The following example shows how to use schema −
<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
<xs:element name = "contact">
<xs:complexType>
<xs:sequence>
<xs:element name = "name" type = "xs:string" />
<xs:element name = "company" type = "xs:string" />
<xs:element name = "phone" type = "xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
PAGE NO. 35
M. Gnanasekaran, MCA., M.Phil., SET., TVKGAC-TVR DT – Unit_II Notes
The basic idea behind XML Schemas is that they describe the legitimate format that
an XML document can take.
Elements
As we saw in the XML - Elements chapter, elements are the building blocks of XML
document. An element can be defined within an XSD as follows −
Simple Type
Simple type element is used only in the context of the text. Some of the predefined
simple types are: xs:integer, xs:boolean, xs:string, xs:date. For example −
In the above example, Address element consists of child elements. This is a container
for other <xs:element> definitions, that allows to build a simple hierarchy of
elements in the XML document.
PAGE NO. 36