Title: Introduction to Java
1Introduction to Java
- April 26, 2007
- David Goldschmidt, Ph.D.
- goldschd_at_strose.edu
2Applications 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)
3Applications 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
4Applications 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
5Applications 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
6Tic Tac Toe (i)
7Tic Tac Toe (ii)
8Overcoming 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
9Meta-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
10Meta-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
11Deploying 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
12Deploying 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
13Deploying Java Applets (iii)
- Use the following HTML to deploy the JAR file
- ltapplet
- codeTicTacToe.class
- archiveTicTacToe.jar
- width350
- height200gt
- lt/appletgt
14JDBC
- 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)
15JDBC Architecture
16JDBC 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
17JDBC 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")
18JDBC Interfaces
19JDBC 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)
20Sample Access Database
21Using JDBC to Process Data
22Client/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
23Creating 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
24Creating 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
25Creating 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
26Communicating 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
27Communicating 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)
28Communicating 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)
29Communicating 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
30Serving 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()
-
31Applets 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())
32Retrieving 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
33Retrieving 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
34Retrieving 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)
35Retrieving 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) ...
-
- )