Docsity

Java Programming for the World Wide Web, Study notes of Software Engineering

Partial preview of the text

Download Java Programming for the World Wide Web and more Study notes Software Engineering in PDF only on Docsity!

Java and World Wide Web

Ye Wu http://www.ise.gmu.edu/~wuye/ SWE 642 Software Engineering for the World Wide Web sources: HTML 4 for the World Wide Web, Castro, Peachtree Press Core Web Programming, Hall, PTR

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 2

How to learn Java?

• Syntax

• Semantic (OO features)

• Java Core API

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 3

Benefits of OO implementation

  • Notion of an abstract data type Well-encapsulated aggregate of data and behavior
  • Extensibility provided by inheritance -- Code

Reuse

  • Inheritance
  • Overloading and overriding
  • Dynamic binding

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 4

Interface

A java interface is a type of object reference

Interface allow you to reference to objects without having to know the object’s class

Other Java OO features

  • Inner class, anonymous class
  • Exception handling
  • Access control
  • … … VariableTest2.java

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 7

Vector

  • Vector is a self-expanding array-like object
    • public void addElement(Object);
    • public void insertElement(Object);
    • public Boolean Contains(Object);
    • public Object firstElement();
    • public Object elementAt(int);
    • public Enumeration elements();
  • Stack extends Vector with couple of more methods.
    • public Object pop()
    • Public push(Object) (^) OrderInfo.java

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 8

Property

  • Used to store String/String key-value pairs
  • Extends from Hashtable
  • System class has a static Properties which holds system properties - Java VM version info - Current user info
  • Properties props = System.getProperties()
  • Props.list(System.out)

TestProperty.java

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 9

Java Reflection

  • Obtain Public data members and methods
  • Obtain Inherited data members and methods
  • Get/Set data members
  • Invoke methods
  • Obtain Class name, and parent class names
  • Obtain Interfaces that are implemented

Reflection API – are capable of doing the above functionality

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 10

Retrieving Class Objects

  • Retrieve class objects from an instance
    • Class class = object.getClass();
  • Retrieve class objects if you know the class name at compile time - Class class = java.lang.String.class;
  • If the class name is unknown at compile time, but available at runtime - Class class = classForName(“com.sun.jdbc.odbc”)
  • ClassLoader.loadClass()

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 13

Using Constructors that Have Arguments

Rectangle rectangle; Class rectangleDefinition; Class[] intArgsClass = new Class[] {int.class, int.class}; Integer height = new Integer(12); Integer width = new Integer(34); Object[] intArgs = new Object[] {height, width}; Constructor intArgsConstructor; try { rectangleDefinition = Class.forName("java.awt.Rectangle"); intArgsConstructor = rectangleDefinition.getConstructor(intArgsClass); rectangle = (Rectangle) createObject(intArgsConstructor, intArgs); } catch (ClassNotFoundException e) { System.out.println(e); } catch (NoSuchMethodException e) { System.out.println(e); }

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 14

Data Members and Methods

  • In java.lang.reflect package, there are two corresponding class, Field and Method ;
  • Method class invocation
  • Public Object invoke(Object obj, Object[] args)
    • Obj – the object whose method is called
    • Types of params matches getParamTypes()
    • Exceptions may be thrown, but not declared
    • Reflective invocation is very slow. TestReflection.java

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 15

Serialization

Take any object that implements the

Serializable interface and turns it into a

sequence of bytes that can later be fully

restored into the original object(state of an

object)

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 16

Persistence Object Storage

ObjectOutputStread extends FilterOutputStream

Serialization

OutputStream

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 19

What CANNOT be serialized?

  • Thread, file, socket
  • Static data is not part of object’s state
  • Transient data – some of the data are not serializable or you don’t want to include them into the persistent data. / Security reason
  • For transient values, upon recreation, transient values take on default value, for ex. Int – 0, boolean, false

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 20

Deseralization

readObject/writeObject

Private void readObject(ObjectInputStream ois) Throws IOException, ClassNotFoundException { ois.defaultReadObject(); }

  • writeObject – write extra data into the stream(for security)

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 21

Serializable interfaces

package java.io public interface Serializable {}

  • A class that implements(or inherits implementation) is seriablizable. - Public class point implements Serializable {..} - Public class point3D extends Point Serialization*.java

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 22

Multi-Threading Capability in Java

  • Thread vs. Process
  • Create Thread in Java
  • Synchronization of multiple threads

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 25

Create Thread in Java (cont)

  • Create a Runnable object package java.lang; public interface Runnable { void run(); } public myRunnable implements Runnable { public void run() { Thread cur = Thread.currentThread(); cur.sleep(500); …. }

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 26

Public static void main (String [] argv) { Runnable r = new MyRunnable(dfadad); Thread t = new Thread(r); t.start(); … … }

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 27

Synchronization of multiple threads

  • Synchronization problems
  • Object monitor – a monitor is a synchronization device that can only be owned by one java thread at a time.

Public synchronized void add() {… …}

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 28

public void moveTo(int newx, int newy) { x = newx; y = newy; }

public boolean inUperleftQuadant(int x, int y) { if (x < 0 && y > 0) return true; return false; }

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 31

Sun SMTP class

  • Import mail utilities:
    • import sun.net.smtp.SmtpClient;
  • Setup mail header:
  • Send message:
    • out = send.startMessage ();
    • out.println ("… message header and body …");
    • out.flush ();
    • out.close ();
    • send.closeServer ();

Common job is to gather data from form and send through email

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 32

JavaMail

<> java.mail.Part

java.mail.Message

java.mail.internet.MimeMessage

java.mail.internet.MimePart<>

java.mail.internet.MimeBodypart

java.mail.internet.Bodypart

java.mail.internet.MimeMultipart

java.mail.MimeMultipart

0..*

1

Session

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 33

Advantages

  • Session
  • Message vs. MimeMessage
  • Message vs. Part
  • Multiparts
  • Scalability

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 34

Send email – javax.mail.Transport

  • Create session instance
    • Session session = Session.getInstance(prop,null)
  • Build messages
    • MimeMessage message = new MimeMessage(session);
    • Message.setFrom(new InternetAddress(“[email protected]”));
    • Message.setRecipient(InternetAddress(“[email protected]”));
    • Message.setSubject(“Java Mail”);
  • Single part
    • Message.setContent(“Java Mail Test”);

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 37

Installation

  • OSF
    • csh/tcsh:
      • setenv CLASSPATH.:/home/u3/wuye/mail.jar:/home/u3/wuye/activation.jar
    • ksh/sh:
      • export CLASSPATH=.:/home/u3/wuye/mail.jar:/home/u3/wuye/activation.jar
  • hermes
    • csh/tcsh:
      • Setenv CLASSPATH .:$CATALINA_HOME/common/lib/mail.jar:$CATALINA_HOME/common/l ib/activation.jar
    • ksh/sh:
      • exportCLASSPATH=.:$CATALINA_HOME/common/lib/mail.jar:$CATALINA_H OME/common/lib/activation.jar

2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 38

Installation

• OSF

  • alias javac /usr/opt/java142/bin/javac
  • alias java /usr/opt/java142/bin/java
  • hermes
  • alias java=/usr/java/bin/java
  • alias javac=/usr/java/bin/javac