Introduction to Java - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Introduction to Java

Description:

Both applications and applets require JVM (often via JRE or Web browser plug-in) ... Applications are invoked from static main() method by Java interpreter ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 36
Provided by: ETS38
Category:
Tags: introduction | java | jre

less

Transcript and Presenter's Notes

Title: Introduction to Java


1
Introduction to Java
  • April 26, 2007
  • David Goldschmidt, Ph.D.
  • goldschd_at_strose.edu

2
Applications vs. Applets (i)
  • Because JFrame and JApplet are both subclasses of
    Container, all user interface components, layout
    managers, and event-handling features are the
    same
  • Applets require HTML for deployment
  • Both applications and applets require JVM (often
    via JRE or Web browser plug-in)

3
Applications vs. Applets (ii)
  • Applications are invoked from static main()
    method by Java interpreter
  • Applets are run by Web browser
  • Applications require a frame (e.g. JFrame)
  • Applets require a Web browser to provide
    graphical environment

4
Applications vs. Applets (iii)
  • Applets have security restrictions
  • An applet is not allowed to read from or write to
    the file system of the Web browsers computer
  • An applet is not allowed to run any programs on
    the Web browsers computer
  • An applet is not allowed to establish connections
    between the users computer and another computer
  • Except with the server on which the applet is
    stored

5
Applications vs. Applets (iv)
  • Converting an applet into an application is
    always possible (and usually mechanical)
  • Converting an application into an applet is
    sometimes possible, depending on whether security
    restrictions on applets are not violated

6
Tic Tac Toe (i)
7
Tic Tac Toe (ii)
8
Overcoming Applet Restrictions
  • Security restrictions disallow applets from
    accessing local or network files
  • How can applets load image, audio, video, and
    other local or network files?
  • Use the java.net.URL class to identify local
    files or files available via the Internet

9
Meta-Objects (i)
  • When a class is loaded, the JVM creates a
    meta-object for the class
  • java.lang.Class metaObject this.getClass()
  • Meta-object instances provide information about
    the class, including
  • Data fields
  • Constructors
  • Methods

10
Meta-Objects (ii)
  • Obtain URL of a file in the class directory
  • String filename ca.gif
  • Class metaObject this.getClass()
  • URL url metaObject.getResource(filename)
  • If the class directory was C\Java\rpi-week12\
    then the URL would be
  • C\Java\rpi-week12\ca.gif

11
Deploying Java Applets (i)
  • A Java Archive ( JAR) file groups all project
    files into a single compressed file for
    deployment
  • Includes Java class files, images, audio files,
    etc.
  • JAR files are based on the ZIP file format
  • Can usually open JAR files using WinZip
  • JAR files enables efficient deployment of both
    Java applets and applications

12
Deploying Java Applets (ii)
  • Use jar command from the Java SDK to create a
    Java archive file
  • jar -cvf TicTacToe.jar TicTacToe.class
    TicTacToeCell.class
  • Command-line arguments very similar to tar
  • Use -c to create a new JAR file
  • Use -v to specify verbose mode
  • Use -f to specify the name of the JAR file

Files to be added to the JAR file
Name of the JAR file
13
Deploying Java Applets (iii)
  • Use the following HTML to deploy the JAR file
  • ltapplet
  • codeTicTacToe.class
  • archiveTicTacToe.jar
  • width350
  • height200gt
  • lt/appletgt

14
JDBC
  • JDBC is a platform-independent Java API for
    executing SQL statements
  • Using JDBC, you can
  • Connect to databases
  • Send SQL statements
  • Receive results (i.e. rows of data)

15
JDBC Architecture
16
JDBC Drivers (i)
  • Before connecting to a database, a driver class
    must first be loaded into the JVM
  • A driver is simply a class in Java
  • Access sun.jdbc.odbc.JdbcOdbcDriver
  • MySQL com.mysql.jdbc.Driver
  • Oracle oracle.jdbc.driver.OracleDriver
  • Oracle driver located in a JAR file of the Oracle
    distribution

17
JDBC Drivers (ii)
  • To load a driver, use static forName() method of
    the java.lang.Class class
  • Class.forName(sun.jdbc.odbc.JdbcOdbcDriver)
  • For Oracle
  • Class.forName("oracle.jdbc.driver.OracleDriver")

18
JDBC Interfaces
19
JDBC Connection Strings
  • Connect to a database using its connect string
    (i.e. its connection URL)
  • Access jdbcodbcdataSource
  • MySQL jdbcmysql//hostname/dbname
  • Oracle jdbcoraclethin_at_hostnameportSID
  • Use a Connection object
  • Connection connection
  • DriverManager.getConnection(dbConnectURL)

20
Sample Access Database
21
Using JDBC to Process Data
22
Client/Server Architecture
  • Web-based and other applications are often
    designed using a client/server architecture
  • An application server is always running, waiting
    for connections from clients
  • A client connects to a server as
    necessary on behalf of its
    user
  • A socket serves as
    the communications
    channel between

    client and server

Server
socket
socket
socket
Client
Client
Client
23
Creating a Server in Java (i)
  • When the server first starts up, it creates a
    server socket (java.net.ServerSocket) on a given
    port
  • ServerSocket serverSocket new
    ServerSocket(port)
  • Port numbers typically range from 0 to 65535
  • First 1024 port numbers are reserved for
    privileged services (e.g. HTTP uses port 80, FTP
    uses port 21)
  • ServerSocket serverSocket new
    ServerSocket(8000)
  • Using a port in use throws a BindException

24
Creating a Server in Java (ii)
  • Once a server socket is created, the accept()
    method is called to activate the server socket as
    a listener ready to accept new incoming client
    connections
  • Socket incomingSocket serverSocket.accept()
  • Java supports both TCP and UDP transmission
    protocols
  • For UDP, use java.net.DatagramSocket and
    java.net.DatagramPacket classes

25
Creating a Client in Java
  • A client connects to a listening server via the
    Socket() constructor
  • Socket clientSocket new Socket(serverName,
    port)
  • Socket s1 new Socket(java.rpi.edu, 8123)
  • Socket s2 new Socket(128.3.127.5, 8000)
  • Socket s3 new Socket(127.0.0.1, 9000)
  • Socket s4 new Socket(localhost, 9001)
  • Constructor could throw UnknownHostException

26
Communicating via Sockets (i)
  • Once established, a socket contains both an input
    and an output stream
  • Methods getInputStream() and getOutputStream()
    return InputStream and OutputStream objects (from
    the java.io library)
  • Interesting and useful subclasses of InputStream
    and OutputStream include
  • DataInputStream and DataOutputStream
  • ObjectInputStream and ObjectOutputStream

27
Communicating via Sockets (ii)
  • Use DataInputStream and DataOutputStream to
    transfer primitive data types between client and
    server
  • InputStream input socket.getInputStream()
  • DataInputStream in new DataInputStream(input)
  • OutputStream output socket.getOutputStream()
  • DataOutputStream out new DataOutputStream(outp
    ut)

28
Communicating via Sockets (iii)
  • Use ObjectInputStream and ObjectOutputStream to
    transfer serializable objects between client and
    server
  • InputStream input socket.getInputStream()
  • ObjectInputStream in new ObjectInputStream(inp
    ut)
  • OutputStream output socket.getOutputStream()
  • ObjectOutputStream out new ObjectOutputStream(
    output)

29
Communicating via Sockets (iv)
  • For an object to be serializable, its class must
    implement the java.io.Serializable interface
  • The Serializable interface has no methods or
    fields and serves only to identify the semantics
    of being serializable
  • Enables the use of readObject() and writeObject()
    methods of ObjectInputStream and
    ObjectOutputStream

30
Serving Multiple Clients
  • So far, weve only looked at a server that blocks
    when it receives an incoming client connection
  • To serve multiple clients, a server delegates
    each incoming client connection to a dedicated
    thread
  • while (true)
  • Socket socket serverSocket.accept()
  • Thread thread new ThreadClass(socket)
  • thread.start()

31
Applets as Clients
  • Applets can only connect to the host from which
    they were loaded (i.e. the Web server)
  • Obtain the name of the Web server by calling the
    getHost() method of the URL object returned by
    the getCodeBase() method
  • URL host getCodeBase()
  • socket new Socket(host.getHost())

32
Retrieving Files from a Web Server (i)
  • Java provides direct programmatic access to files
    on a Web server via the java.net.URL class
  • No need to create a server application!
  • URL url new URL(http//webaddress)
  • Throws a MalformedURLException if the URL is
    invalid

33
Retrieving Files from a Web Server (ii)
  • Use the openStream() method to open an input
    stream to the given URL
  • URL url new URL(http//webaddress)
  • InputStream input url.openStream()
  • Read text from the input stream using the
    BufferedReader and other Java API classes

34
Retrieving Files from a Web Server (iii)
  • Use the javax.swing.JEditorPane class to display
    plain text, HTML, or RTF files
  • JEditorPane jep1 new JEditorPane()
  • JEditorPane jep2 new JEditorPane(URL)
  • JEditorPane jep3 new JEditorPane(urlString)
  • Once constructed, use the setPage() method to
    load another file
  • jep1.setPage(URL)

35
Retrieving Files from a Web Server (iv)
  • The javax.swing.JEditorPane class captures a
    javax.swing.event.HyperlinkEvent when a hyperlink
    in the editor pane is clicked
  • jep.addHyperlinkListener(new
    HyperlinkListener()
  • public void hyperlinkUpdate(HyperlinkEvent
    e)
  • try jep.setPage(e.getURL())
  • catch (IOException ex) ...
  • )
Write a Comment
User Comments (0)
About PowerShow.com