Serialization - Web Design and Development - Lecture Handouts, Lecture notes of Web Design and Development

Serialization, Motivation, Revisiting AddressBook, Serialization in Java, Serializable Interface, Automatic Writing, Object Serialization and Network, Reading Objects over Network. Virtual University is one of best in Pakistan for distance education in science.

Typology: Lecture notes

2011/2012

Uploaded on 11/10/2012

taariq
taariq 🇵🇰

4.4

(16)

61 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Web Design & Development CS-506
- 249 -
HANDOUTS
WEB DESIGN AND DEVELOPMENT
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Serialization - Web Design and Development - Lecture Handouts and more Lecture notes Web Design and Development in PDF only on Docsity!

HANDOUTS

WEB DESIGN AND DEVELOPMENT

Lecture 22

Serialization

What?

ƒ You want to send an object to a stream.

ƒ

Motivation

ƒ A lot of code involves boring conversion from a file to memory

  • As you might recall that AddressBook program reads data from file and then

parses it

ƒ This is a common problem

Revisiting AddressBook

We read record from a text file named persons.txt. The person record was present in

the file in the following format.

Ali,defence,9201211Usman,gulberg, Salman,LUMS,

persons.txt

The code that was used to construct Person objects after reading information from the file

is given below. Here only the part of code is shown, for complete listing, see

AddressBook code in your earlier handout.

……………………… FileReader fr = new FileReader("persons.txt");BufferedReader br = new BufferedReader(fr); String line = br.readLine(); while ( line != null ) { tokens = line.split(","); name = tokens[0];add = tokens[1]; ph = tokens[2]; PersonInfo p = new PersonInfo(name, add, ph);

Automatic Reading

ƒ System knows how to read the data from Stream and re-create object in memory

ƒ The recreated object is of type “Object” therefore Down-casting is required to convert

it into actual type.

Serialization: How it works?

ƒ To write an object of PersonInfo, ObejctOutputStream and its method

writeObject( ) will be used

PersonInfo p = new PersonInfo( ); ObejctOutputStream out; // out.writeObject(p); writing PersonInfo’s object p

ƒ To read that object back, ObejctInputStream and its method readObject()

will be used

ObejctInputStream in; // reading PersonInfo’s object. Remember type casting //PersonInfo is required obj = (PersonInfo)in.readObject( );

Example Code 22.1: Reading / Writing PersonInfo objects

We want to send PersonInfo object to stream. You have already seen this class number of

times before. Here it will also implement serializable interface.

PersonInfo.java

import import (^) javax.swing.; java.io. class PersonInfo implements Serializable { String name; StringString address;phoneNum;

//parameterized constructor public PresonInfo(String n, String a, String p) { nameaddress = n; = a; phoneNm = p; } //methodpublic void for printPersonInfo(displaying person ) record{ on GUI JOptionPane.showMessageDialog(null , “name: ” + name + “address:” +address + “phone no:” + phoneNum); } } // end class

ReadEx.java

The following class will read serialized object of PersonInfo from file i.e “ali.data”

import java.io*; public class ReadEx{ public static void main(String args[ ]){ try { // attaching FileInput stream with “ali.dat” FileInputStream fin = new FileInputStream("ali.dat"); // attaching FileInput stream over ObjectInput stream ObjectInputStream in = new ObjectInputStream(fis); //de-serialization// reading object from ‘ali.dat’ PersonInfo pRead = (PersoInfo)in.ReadObject( ); //// callingobject containsprintPersonInfo same set method of values to confirm before that // serializatoion pRead.printPersonInfo(); //in.close(); closing streams fis.close(); } catch (Exception ex){ System.out.println(ex) } } // end class

Compile & Execute

After compilation, first run the WriteEx.java file and visit the “ali.dat” file. Then run

ReadEx.java from different command or same command prompt.

Object Serialization & Network

ƒ You can read / write to a network using sockets

ƒ All you need to do is attach your stream with socket rather than file

ƒ The class version should be same on both sides (client & network) of the network

Reading Objects over Network

The following class ServerReadNetEx.java will read an object of PersonInfo sent by

client.

import java.net.; importimport java.io.;javax.swing.; public class ServerReadNetEx{ public static void main(String args[]){ try { // create a server socket ServerSocket ss = new ServerSocket(2222); System.out.println("Server started..."); / (^) socketLoop back and towait the for accept a new method connection of the request. server So */ server^ will^ continuously^ listen^ for^ requests while(true) { // wait for incoming connection Socket s = ss.accept(); System.out.println("connection request recieved"); // Get I/O streams InputStream is = s.getInputStream(); // attaching ObjectOutput stream over Input stream ObjectInputStream ois = new ObjectInputStream(is); // read PersonInfo object from network PersonInfo p = (PersonInfo)ois.read( ); p.printPersonInfo(); //s.close(); closing communication socket } // end while

}catch(Exception ex){ System.out.println(ex); } } } // end class

Compile & Execute

After compiling both files, run ServerReadNetEx.java first, from the command

prompt window. Open another command prompt window and run

ClientWriteNetEx.java from it.

The ClientWriteNetEx.java will send an Object of PersonInfo to

ServerReadNetEx.java that displays that object values in dialog box after reading

it from network.