Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Saving and retrieving objects with Java

Saving and retrieving objects with Java

Tutorial Details:

RMI and object serialization
RMI and object serialization
By: By Rinaldo Di Giorgio
Saving and retrieving objects with Java
ecently JavaSoft has made available an alpha release of the serialization and RMI classes . This new API is extremely useful and important to master. It allows developers to save and retrieve Java classes to any I/O stream. Objects can be saved and retrieved at later points in time. The supplied classes do not provide any namespace management for the saved objects. This article will cover:
saving and retrieve objects to a local disk.
saving objects over a network to a server on a known port.
some performance metrics for you to think about.
learn how to write Java programs that are both applications and applets -- where an application means it contains a main() .
We'll conclude with some more discussion of the specifics of the implementation. This column teaches concepts and the applets don't really do anything useful other than serve as an instructional vehicle.
The following examples demonstrate the use of the serialization technology that has been included in JDK 1.0.2. An add-on package is required to run the following applets. The applets below were run with the JDK appletviewer on Windows 95 and Solaris. I was unable to find a browser manufacturer that clearly stated what version of the JDK they were running and decided that it was best to wait for Microsoft and Netscape to get up to speed with the up and coming JDK 1.1 release. If you have problems installing the serialization classes needed on your system try looking at this FAQ . The javaSoft team has also been nice enough to collect all the mail messages for those of you wishing to find quick answers to questions.
Setting up the environment on your local machine
Get and install the RMI classes following the directions provided with them. Make sure you set your CLASSPATH environment variable to include the lib/rmi.zip file in the RMI directory.
Download and unzip the example files provided. Install these files into a directory and add it to CLASSPATH . Use the appletviewer or the Java virtual machine to run the applets below.
Reading and writing Java objects to a local disk
The following two applets save an object and retrieve some classes. Because they are writing to the disk you will have to run these programs locally on your system. The ShowSavingAClass applet saves a String and a Date class to a disk file called SavedObject in the current directory. ShowReadingAClass reads the saved file SavedObject and recreates the classes contained in it. If your browser has RMI support then you can run the following two applets:
Write the objects . Here's the source .
Read the objects . Here's the source .
Writing Java objects to a server
The programs require that a server program be running on the server so that an applet or Java application can communicate over the port to save objects.
The program WriteObjectsFromSocket reads an object from a client and saves it on the server in a fixed area. Here's the source . To create a file on a server that contains an instance of the object run the following:
java WriteObjectsFromSocket servername portnumber ,
The corresponding client piece is ShowSavingAClassNet . Here's the source . Don't forget to modify the HTML parameter tags if you host this applet on your local system.
The source code for ShowSavingAClassNet.java contains an example of designing applets that use the same source code for both so that users can start them with or without the applet viewer.
Performance considerations
The ShowWritedouble applet writes 10,000 double objects to a disk file. You can easily modify this to write other data types and get a feeling for how long it takes to save an object. This applet can easily be modified to retrieve the objects also.
Summary
When designing classes that may be serialized be aware of the fact that all methods and data will be saved so try to optimize space usage and practice encapsulation. There are some important security restrictions described in the documentation of the RMI classes. The special handling of private maintains Java's strict security model. Those of you wishing more information can take a look at reflection . The following questions arose during the development of these instructional applets:
The setting of the acl.read or acl.write variables had no effect on the ability of an applet to read or write files on the local file system on my system. I was only able to get an applet to read or write files on the local file system when I included the applet's class files in the CLASSPATH .
It would be nice if the performance metric included serialization speed as a number.
This page formated for crawlers and browsers that don't support scripts and tables.
Home
EZone


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Saving and retrieving objects with Java

View Tutorial:
Saving and retrieving objects with Java

Related Tutorials:

Saving and retrieving objects with Java
Saving and retrieving objects with Java
 
Build an object database - JavaWorld January 2000
Build an object database - JavaWorld January 2000
 
Process XML with JavaBeans, Part 3 - JavaWorld January 2000
Process XML with JavaBeans, Part 3 - JavaWorld January 2000
 
Eliminate tedious programming: Recover data with XML and Reflection - JavaWorld November 2000
Eliminate tedious programming: Recover data with XML and Reflection - JavaWorld November 2000
 
Object mobility in the Jini environment - JavaWorld January 2001
Object mobility in the Jini environment - JavaWorld January 2001
 
The magic of Merlin - JavaWorld March 2001
The magic of Merlin - JavaWorld March 2001
 
J2EE or J2SE? JNDI works with both
J2EE or J2SE? JNDI works with both
 
Chart a new course with JFreeChart
Chart a new course with JFreeChart
 
Transparently cache XSL transformations with JAXP
Transparently cache XSL transformations with JAXP
 
A simple sample, but interesting enough
A simple sample, but interesting enough
 
Impressive !
Impressive !
 
Fixing the Java Memory Model, Part 1
JSR 133, which has been active for nearly three years, has recently issued its public recommendation on what to do about the Java Memory Model (JMM).
 
SQL Database Access with DBTags
SQL Database Access with DBTags In a J2SE or J2EE application, Java Database Connectivity (JDBC) can be used to create a connection with a SQL database, create database tables, retrieve result sets, and update the database. To use a database from a Jav
 
XML DOM-lite parser and writer
Summary This article provides a simple method for creating valid XML and a simple DOM-like (Document Object Model) parser. This method is useful for small applications that need simple XML functionality without the size and complexity of the full range d
 
HeapAnalyzer
What is HeapAnalyzer? HeapAnalyzer allows the finding of a possible JavaTM heap leak area through its heuristic search engine and analysis of the Java heap dump in Java applications. Java heap areas define objects, arrays, and classes.
 
An Introduction to Java Object Persistence with EJB
The 'impedance mismatch' between relational databases' tabular orientation and object-oriented Java's hierarchical one is a perennial problem for which the Java world has several good solution offerings. This article, the first in a three-part series, wil
 
BioInformatics Tools
BioInformatics Tools BioInformatics Tools The Bioinformatics tools are the software programs for the saving, retrieving and analysis of Biological data and extracting the information from them. Factors that must be taken into consideration when
 
Introduction to the JSP Java Server Pages
Introduction to the JSP Java Server Pages Welcome to JSP Section Introduction To JSP Java Server Pages or JSP for short is Sun's solution for developing dynamic web sites. JSP provide excellent server side scripting support for creating database
 
Connecting to MySQL database and retrieving and displaying data in JSP page
Connecting to MySQL database and retrieving and displaying data in JSP page Connecting to MySQL database and retrieving and displaying data in JSP page This tutorial shows you how to connect to MySQL database and retrieve the data from the
 
ORA
ORA is a Framework written in Java (plattform independent).
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.