basic java part_ii

166
© 2004 VeriSign, Inc. Java Beans Marutha IT Architect, ISL, BPTSE [email protected]

Upload: khaled-al-ghazaly

Post on 31-Oct-2014

894 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Basic java part_ii

© 2004 VeriSign, Inc.

Java Beans

MaruthaIT Architect, ISL, BPTSE

[email protected]

Page 2: Basic java part_ii

2

Java Beans+ Component architecture, a reusable software component written in Java programming language.

+ Components (JavaBeans) are reusable software programs that you can develop and assemble easily to create sophisticated applications.

+ Used for using & building components in Java.

+ Doesn’t alter the existing Java language.

Page 3: Basic java part_ii

3

Features of Java Bean

Independent Reusability Secure Bean Bridge Interface Etc.

Page 4: Basic java part_ii

4

Basic Bean Concepts Properties Introspection Customization Events Persistence Methods

Page 5: Basic java part_ii

5

Java Beans : Properties

• Simple Properties

• Indexed Properties

• Bound Properties

• Constrained Properties

Page 6: Basic java part_ii

6

Java Beans : Introspection

• Introspection allows a builder tool to analyze how Beans work. They adhere to specific rules called design patterns for naming Bean features.

Page 7: Basic java part_ii

7

Java Beans : Customization

• Customization allows a user to alter the appearance and behavior of a Bean.

• Beans support customization by using property editors.

Page 8: Basic java part_ii

8

Java Beans : Events

+ Events are used by Beans to communicate with other Beans.

+ Beans fire events.

+ The Bean receiving event is called a listener Bean.

Page 9: Basic java part_ii

9

Java Beans : Persistence

+ Beans use Java object serialization, to save and restore state that may have changed as a result of customization.

Page 10: Basic java part_ii

10

Java Beans : Methods

+ Identical to Java classes.

Page 11: Basic java part_ii

© 2004 VeriSign, Inc.

Applet

Page 12: Basic java part_ii

12

Introduction

+ Java Application vs Java Applets+ Running Java Applet

+ Using Browser+ Using Appletviewe

+ Java includes classes.

Page 13: Basic java part_ii

13

Applet Class

+ All applets are subclass of the Applet class.+ Applet Class is present in the ‘java.applet’ package. + Applet must import java.applet and java.awt.+ Applets contain a single default class.

Page 14: Basic java part_ii

14

Creating, Running and executing an Applet

import java.applet.Applet;import java.awt.Graphics; public class FirstApplet extends Applet {

public void paint(Graphics g){

g.drawString(“Welcome to the World of Applets!!”, 10, 50);}

}

Page 15: Basic java part_ii

15

Viewing In Applet

+ Write a HTML file, and view it in browser

+ Include a commented applet tag in the java program and view it in appletviewer.

Page 16: Basic java part_ii

16

Activities In An Applet

Creation

Initialization

Starting

Stop

Destroy

Page 17: Basic java part_ii

17

Paint() Method

+ Paint is how an applet displays something on the screen. This can be anything from a text, a line, background color or an image.

+ public void paint(Graphics g)

{

g.drawString( String message, int x, int y);

}

Page 18: Basic java part_ii

18

Repaint() Method

+ Whenever your applet needs to update the information displayed in its window, it simply calls repaint().

+ The repaint() method is defined by the AWT.

+ It causes the AWT run-time system to execute a call to your applet’s update() method, which, in its default implementation, calls paint()

Page 19: Basic java part_ii

19

HTML Applet Tag

<APPLET[CODEBASE = codebaseURL]CODE = appletFile[ALT = alternateText][NAME = appletInstanceName]WIDTH = pixels HEIGHT = pixels[ALIGN = alignment][VSPACE = pixels][HSPACE = pixels]>[< PARAM NAME = attributeName VALUE = AtrributeValue>]..</APPLET>

Page 20: Basic java part_ii

20

Passing Parameters to Applets

+ The Applet tag in HTML allows you to pass parameters to your applet. To retrieve a parameter, use the getParameter() method.

Page 21: Basic java part_ii

21

Important Methods

+ getDocumentBase( )+ Returns the path of the document file as url.

+ getCodeBase( )+ Returns the path of .class file as url

Page 22: Basic java part_ii

22

Applet Context

+ Methods Of Applet Context+ Void showDocument(URL url)

+ Void showDocument(URL url String where) + AudioClip getAudioClip(URL url)

+ AudioClp getAudioClip(URL url, String name) + Image getImage(URL url)

+ Image getImage(URL url, String name)

Page 23: Basic java part_ii

23

Applet Context (Contd…)

+ Enumeration getApplets().

+  Applet getapplet(String name)

Page 24: Basic java part_ii

24

Security Restrictions applied on Applets

+ Cannot read or write files on the user system.+ Cannot communicate with an Internet site but only with the one that

served the web page including the applet. + Cannot run any programs on the reader’s system. + Cannot load any programs stored on the user’s system.

Page 25: Basic java part_ii

25

File Name Filters

+ FilenameFilter+ public boolean accept(File dir, String name)

+ Sample Program

Page 26: Basic java part_ii

26

+ RandomAccessFile Class+ This class provides the capability to perform I/O to specific locations

within a file. + The name randomaccess is due to the fact that data can be read or

written to random locations within a file instead of a continuous stream of information.

+ An argument ‘r’ or ‘rw’ is passed to the RandomAccessFile indicating read-only and read-write file access.

Page 27: Basic java part_ii

© 2004 VeriSign, Inc.

Java Thread

Page 28: Basic java part_ii

28

Thread

+ Benefits + User and Kernel Threads+ Multithreading Models+ Solaris 2 Threads+ Java Threads

Page 29: Basic java part_ii

29

Page 30: Basic java part_ii

30

Page 31: Basic java part_ii

31

Page 32: Basic java part_ii

32

Page 33: Basic java part_ii

33

MultiThread Models

+ Many-to-One

Many User-Level Threads Mapped to Single Kernel Thread.

Used on Systems That Do Not Support Kernel Threads.

Page 34: Basic java part_ii

34

Page 35: Basic java part_ii

35

Page 36: Basic java part_ii

36

Page 37: Basic java part_ii

37

Page 38: Basic java part_ii

38

Page 39: Basic java part_ii

39

Page 40: Basic java part_ii

40

Java Thread

+ Java Threads May be Created by:+ – Extending Thread class+ – Implementing the Runnable interface

Page 41: Basic java part_ii

41

Extending Thread Class

class Worker1 extends Thread

{

public void run() {

System.out.println(“I am a Worker Thread”);

}

}

Page 42: Basic java part_ii

42

Creating the Thread

public class First

{

public static void main(String args[]) {

Worker runner = new Worker1();

runner.start();

System.out.println(“I am the main thread”);

}

}

Page 43: Basic java part_ii

43

Runnable Interface

public interface Runnable

{

public abstract void run();

}

Page 44: Basic java part_ii

44

Implementing Runnable

class Worker2 implements Runnable

{

public void run() {

System.out.println(“I am a Worker Thread”);

}

}

Page 45: Basic java part_ii

45

Creating the Threadpublic class Second

{

public static void main(String args[]) {

Runnable runner = new Worker2();

Thread thrd = new Thread(runner);

thrd.start();

System.out.println(“I am the main thread”);

}

}

Page 46: Basic java part_ii

46

Java Thread Management

+ suspend() – suspends execution of the currently running thread.+ • sleep() – puts the currently running thread to sleep for a+ specified amount of time.+ • resume() – resumes execution of a suspended thread.+ • stop() – stops execution of a thread.

Page 47: Basic java part_ii

47

Page 48: Basic java part_ii

48

Life Cycle of a Thread

new Threadnew Thread

runnablerunnable

blockedblocked

deaddead

start( )

stop( )

sleep( )

suspend( )

resume( )

notify( )

wait( )

Page 49: Basic java part_ii

49

What is Multithreading?

+ A thread is a smallest unit of executable code that performs a particular task.

+ Java supports multithreading. + An application can contain multiple threads. + Each thread is specified a particular task which is executed

concurrently with the other threads. + The capability of working with multiple thread is called

Multithreading.

Page 50: Basic java part_ii

50

What is Multithreading? (Contd…)

+ Multithreading allows you to write efficient programs that makes the maximum use of CPU, by keeping the idle time to a minimum.

+ It is a specialized form of multitasking.+ Multitasking is running multiple programs simultaneously, with each

program having at least one thread in it. + These programs are executed by a single processor.

Page 51: Basic java part_ii

51

Thread Scheduling and Setting the Priorities (Contd…)

+ It is the programmer, or the Java Virtual Machine or the operating system that makes sure that the CPU is shared between the threads.

+ This is called scheduling of threads.+ Every thread in Java has its own priority. + This priority is in the range:

+ Thread.MIN_PRIORITY &+ Thread.MAX_PRIORITY

Page 52: Basic java part_ii

52

Thread Scheduling and Setting the Priorities (Contd…)

+ By default, a thread has a priority of Thread.NORM_PRIORITY, a constant of 5.

+ Every new thread that is created, inherits the priority of the thread that creates it.

+ The priority of the thread can be adjusted with the method called setPriority( ).

+ This takes an integer as an argument. + This value has to be within the range of 1 to 10, else, the method throws

an exception called as IllegalArgumentException.

Page 53: Basic java part_ii

53

Daemon Threads

+ A Java program is terminated only after all threads die. + There are two types of threads in a Java program:

+ User threads+ Daemon threads

+ The threads that are created by the Java Virtual Machine on your behalf are called daemon threads.

+ Java provides garbage collector thread that reclaims dynamically allocated memory.

Page 54: Basic java part_ii

54

Daemon Threads (Contd…)

+ This thread runs as a low priority thread + The garbage collection is marked as a daemon thread.+ Thread class has two methods that deal with daemon threads.

+ public void setDaemon(boolean on)+ public boolean isDaemon( )

Page 55: Basic java part_ii

55

Thread Synchronization

+ It may so happen that more than one thread wants to access the same variable at the same time

+ One thread tries to read the data while the other tries to change the data. + What we need to do is, allow one thread to finish its task completely

(changing the value) and then allow the next thread to read the data. + This can be attained with the help of synchronized( ) method. + This method tells the system to put a lock around a particular method.

Page 56: Basic java part_ii

56

InterThread Communication

+ Necessary to avoid polling + Methods

+ wait()+ notify()+ notifyAll()

Page 57: Basic java part_ii

© 2004 VeriSign, Inc.

JDBC

Page 58: Basic java part_ii

58

JDBC

+ Overview of JDBC and its design goals+ Key API classes+ Some examples

Page 59: Basic java part_ii

59

What is JDBC?

+ JDBC is a Java™ API for executing SQL+ statements+ It’s deliberately a “low level” API+ But it’s intended as a base for higher level APIs+ And for application builder tools+ It’s influenced by existing database APIs+ Notably the XOPEN SQL CLI+ And Microsoft’s ODBC

Page 60: Basic java part_ii

60

Page 61: Basic java part_ii

61

Types of JDBC technology drivers

+ JDBC-ODBC Bridge+ Native API Partly+ Net Protocol Fully+ Native Protocol Fully

Page 62: Basic java part_ii

62

JDBC-ODBC bridge

+ JDBC API access via one or more ODBC drivers+ ODBC native code and in many cases native database client code

must be loaded on each client machine+ (e.g) Microsoft ODBC-Drivers

Page 63: Basic java part_ii

63

native-API partly

+ JDBC calls into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMS

+ this style of driver requires that some binary code be loaded on each client machine

+ Native Module of dependent form of H/W like .dll or .so.+ (e.g) OCI driver for local connection to Oracle

Page 64: Basic java part_ii

64

net-protocol fully

+ JDBC API calls into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server

+ able to connect to many different databases+ (e.g) III party Driver using protocol depends on the vendor

Page 65: Basic java part_ii

65

native-protocol fully

+ JDBC technology calls into the network protocol used by DBMSs directly

+ a direct call from the client machine to the DBMS server+ It is independent from H/W because this driver is %100 java.+ (e.g) thin driver for local/global connection to Oracle

Page 66: Basic java part_ii

66

Page 67: Basic java part_ii

67

Page 68: Basic java part_ii

68

Page 69: Basic java part_ii

69

Page 70: Basic java part_ii

70

Page 71: Basic java part_ii

71

Page 72: Basic java part_ii

72

Page 73: Basic java part_ii

73

Page 74: Basic java part_ii

74

Page 75: Basic java part_ii

75

Page 76: Basic java part_ii

76

Page 77: Basic java part_ii

77

Page 78: Basic java part_ii

78

Page 79: Basic java part_ii

79

Seven Basic Steps in Using JDBC

+ Load the Driver+ Define the Connection URL+ Establish the Connection+ Create a Statement Object+ Execute a Query+ Process the result+ Close the Connection

Page 80: Basic java part_ii

80

+ Three types of statement objects are available+ Statement : executing Simple Query+ PreparedStatement : for executing pre-compiled SQL Statement + CallableStatement : for executing data base stored Procedure

Page 81: Basic java part_ii

81

Page 82: Basic java part_ii

82

Page 83: Basic java part_ii

83

Page 84: Basic java part_ii

84

Page 85: Basic java part_ii

85

JDBC Transaction

Page 86: Basic java part_ii

86

JDBC Transaction

Page 87: Basic java part_ii

87

Learnings from this Session

+ Tell me few responsibility of DriverManager+ What are the three different Statements?+ What is ResultsetMetaData?+ What is DatabaseMetaData?+ Write a Simple prg to create your own SQL> prompt and we can run

any SELECT query.

Page 88: Basic java part_ii

© 2004 VeriSign, Inc.

Networking in Java

Page 89: Basic java part_ii

89

Types Of Network Programming

Two general types are :+ Connection-oriented programming + Connectionless Programming

Page 90: Basic java part_ii

90

Connection-oriented Networking

The client and server have a communication link that is open and active from the time the application is executed until it is closed. Using Internet jargon, the Transmission control protocol os a connection oriented protocol. It is reliable connection - packets are guaranteed to arrive in the order they are sent.

                                                                                       

Page 91: Basic java part_ii

91

Connection-less Networking

The this type each instance that packets are sent, they are transmitted individually. No link to the receiver is maintained after the packets arrive. The Internet equivalent is the User Datagram Protocol (UDP). Connectionless communication is faster but not reliable. Datagrams are used to implement a connectionless protocol, such as UDP.                                                     

Page 92: Basic java part_ii

92

Common Port Numbers

Port Number Service

21 FTP

23 Telnet

25 SMTP

80 Http (Web)

110 POP

143 IMAP

443 HTTS (SSL)

Page 93: Basic java part_ii

93

Page 94: Basic java part_ii

94

Page 95: Basic java part_ii

95

URI, URL, URLConnection and HttpURLConnection

+ URI -Universal Resource Identifier -For parsing+ URL -Universal Resource Locator -For fetching+ URLConnection -Finer control of the transfer+ HttpURLConnection -Subclass specific to HTTP

Page 96: Basic java part_ii

96

URL

+ URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.

+ http://www.abc.com/

Protocol Identifier Resource Name

Page 97: Basic java part_ii

97

URL(contd…)

+ Contrustors Of URL are:+ URL(String urlspecifier)

+ Constructor that allows to break up the URL into its component parts are:

+ URL(String protocolName, String hostname, int port, String path)+ URL(String protocolName, String hostname, String path)

Page 98: Basic java part_ii

98

URL(contd…)

+ java.net.url consists of the following methods:+ openConnection( )

– This method establishes a connection and returns a stream.

+ getContent( )– This method makes a connection and reads addressed

contents.+ getFile( )

– This method returns the filename part of the URL.

Page 99: Basic java part_ii

99

URL(contd…)

+ getHost( )– This method returns only the host name part from the URL.

+ getPort( )– This method returns the port number part from the URL.

+ getProtocol( )– This method returns the name of the protocol of the URL.

Page 100: Basic java part_ii

100

URL(contd…)

+ URLConnection Class+ If we have an active HTTP connection to the web, the URLConnection

class encapsulates it. + It is an abstract class. + Its objects can be created from a single URL. + The URLConnection constructor is protected, hence to create a

URLConnection, we have to first open the connection using the openConnection( ) method.

Page 101: Basic java part_ii

101

URL(contd…)

+ This class supports several methods so as to modify the defaults, query and modify the current settings for a URLConnection object.

+ The methods are:

+ connect( )

– To open a communication link.

+ getDate( )

– To get the value of the date.

Page 102: Basic java part_ii

102

URL(contd…)

+ getExpiration( )

– To get the date of expiration of the URL.

+ getInputStream( )

– To get the InputStream that can be used to read the resource.

+ getLastModified( )

– To get the date last modified.

+ getURL( )

– To get the URL used to establish a connection.

Page 103: Basic java part_ii

103

Inside java.net

+ java.net package consists of the following classes:

+ Socket

+ InetAddress

+ ServerSocket

+ DatagramSocket

+ DatagramPacket

Page 104: Basic java part_ii

104

Inside java.net (Contd…)

+ InetAddress Class+ Eases finding of addresses of the Internet. + Summarizes Internet IP addresses.

+ The static getByName method returns an InetAddress object of a host.

+ getLocalHost method to get the address of your localhost.

Page 105: Basic java part_ii

105

Inside java.net (Contd…)

+ Socket Class+ Java programs connect to the network using a socket. + The socket class helps in establishing client connections. + It is this socket that helps establishing connections and developing

applications between the client and server.

Page 106: Basic java part_ii

106

Inside java.net (Contd…)

+ Socket class has eight constructors that create sockets. + They in turn connect to the destination host and port. + The port number of the machine to which the socket is connected is

obtained by the getPort( ) method. + The local port number is obtained using the getLocalPort( ) method.

Page 107: Basic java part_ii

107

Inside java.net (Contd…)

+ The local IP address is obtained using the getLocalAddress( ). + The input and output streams are accessed using the

getInputStream( ) and getOutputStream( ) methods. + The socket is closed using the close( ) method.+ The string representation of the object is written using the toString( )

method.

Page 108: Basic java part_ii

108

Inside java.net (Contd…)

+ The method to switch from the default Java socket implementation to a custom socket implementation is defined using the setSocketImplFactory( ) method.

Page 109: Basic java part_ii

109

Inside java.net (Contd…)

+ ServerSocket Class+ The TCP server socket is implemented using the ServerSocket class. + There are three constructors specifying the port to which the server socket is to

listen for incoming connection requests. + The address of the host to which the socket is connected is returned using

getInetAddress( ) method. + The local port on which the server socket listens for incoming connection is

returned using the getLocalPort( ) method. + The socket’s address and port number is returned as a string using the toString( )

method.

Page 110: Basic java part_ii

110

Inside java.net (Contd…)

+ DatagramSocket Class+ This class is used to implement the client and server sockets using the User

Datagram Protocol (UDP) protocol. + This class provides three constructors.

+ The default constructor creates a datagram socket. This is used by client applications. In this case no port number is specified.

+ The second constructor creates a datagram socket using a specified port. This is generally with the server applications.

+ The third constructor allows an Internet Address to be specified in addition to the port.

Page 111: Basic java part_ii

111

Inside java.net (Contd…)

+ The datagrams are sent and received using the send( ) and receive( ) methods respectively.

+ The local port and Internet address of the socket is returned using the getLocalPort( ) method and getLocalAddress( ) method respectively.

Page 112: Basic java part_ii

112

Inside java.net (Contd…)

+ DatagramPacket Class

+ This class encapsulates the datagrams that are sent and received using objects

of class DatagramSocket.

+ There are two different constructors, one for datagrams that are received from

the datagram socket and the other for creating datagrams that are sent over the

datagram socket.

Page 113: Basic java part_ii

113

Inside java.net (Contd…)

+ There are about eight access methods provided such as:+ getAddress( ) and getPort( ) for reading the destination IP address and port

of the datagram.

+ getLength( ) and getData( ) for getting the number of bytes of data in the datagram and reading the data into a byte array buffer.

+ setAddress( ), setPort( ), setLength( ) and setData( ) to allow the datagram’s IP address, port, length and data values to be set.

Page 114: Basic java part_ii

© 2004 VeriSign, Inc.

JNDI

Page 115: Basic java part_ii

115

Overview of the Architecture

+ The JNDI architecture consists of the JNDI API and the JNDI SPI. + The JNDI API allows Java applications to access a variety of

naming and directory services.+ The JNDI SPI is designed to be used by arbitrary service providers

including directory service providers.

Page 116: Basic java part_ii

116

JNDI Architecture

Page 117: Basic java part_ii

117

JNDI Architecture

Page 118: Basic java part_ii

118

Composite NameSpace

Page 119: Basic java part_ii

119

Lookup Process

Page 120: Basic java part_ii

120

Lookup Process

Page 121: Basic java part_ii

121

Mapping Directory To Schema

Page 122: Basic java part_ii

© 2004 VeriSign, Inc.

RMI

Page 123: Basic java part_ii

123

What is Distributed System?

Page 124: Basic java part_ii

124

Page 125: Basic java part_ii

125

Page 126: Basic java part_ii

126

Page 127: Basic java part_ii

127

Page 128: Basic java part_ii

128

Page 129: Basic java part_ii

129

Page 130: Basic java part_ii

130

Page 131: Basic java part_ii

131

Page 132: Basic java part_ii

132

Page 133: Basic java part_ii

133

Page 134: Basic java part_ii

134

Page 135: Basic java part_ii

135

Page 136: Basic java part_ii

136

Page 137: Basic java part_ii

137

Page 138: Basic java part_ii

138

Page 139: Basic java part_ii

139

Page 140: Basic java part_ii

140

Page 141: Basic java part_ii

141

Page 142: Basic java part_ii

142

Page 143: Basic java part_ii

143

Page 144: Basic java part_ii

144

RMI Registry

Page 145: Basic java part_ii

145

RMI Communication

Page 146: Basic java part_ii

146

Page 147: Basic java part_ii

147

Page 148: Basic java part_ii

148

Page 149: Basic java part_ii

149

Page 150: Basic java part_ii

150

Page 151: Basic java part_ii

151

Page 152: Basic java part_ii

152

Page 153: Basic java part_ii

153

Page 154: Basic java part_ii

154

UnicastRemoteObject

+ The server typically extends class+ java.rmi.server.UnicastRemoteObject+ The constructor of this class throws a RemoteException+ Therefore, so should the constructor of any specialization.

Page 155: Basic java part_ii

155

UnicastRemoteObject

+ Most arguments and results+ are converted to a sequence of bytes;+ the bytes are sent over the net+ therefore the class should implement the java.io.Serializable interface+ a clone of the argument/result is constructed on the other side.+ The effect is pass by object value, rather than by object reference.

Page 156: Basic java part_ii

156

Page 157: Basic java part_ii

157

Page 158: Basic java part_ii

158

Page 159: Basic java part_ii

159

Page 160: Basic java part_ii

160

Page 161: Basic java part_ii

161

Page 162: Basic java part_ii

162

Page 163: Basic java part_ii

163

RMI Issues

+ Concurrency -If there are multiple clients, the server may field multiple calls at the same time. So use synchronization as appropriate.

+ Argument passing - Arguments are usually passed by value, not reference.+ Proxy and Skeleton generation- Proxy and Skeleton classes are

automatically derrived from the server class+ Lookup - Objects are usually found via a registry

Page 164: Basic java part_ii

164

RMI Issues

+ The proxy and the server share an interface. This interface must extend java.rmi.remote.

+ Every method in the interface should be declared to throw java.rmi.RemoteException

+ RemoteExceptions are thrown when network problems are encountered,

+ or when server objects no longer exist.

Page 165: Basic java part_ii

165

Learning from this session

+ Tell me the Default port of RMI+ Remote+ UniCastRemoteObject+ RmiRegistry+ Stub & Skeleton

Page 166: Basic java part_ii

© 2004 VeriSign, Inc.

Thank You