Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

Various aspects of java programming, including its benefits, interfaces, reflection, serialization, and multi-threading capabilities. It also discusses java's use in the world wide web and its integration with the javamail api for sending emails.

Typology: Study notes

Pre 2010

Uploaded on 02/12/2009

koofers-user-ut9
koofers-user-ut9 🇺🇸

10 documents

1 / 19

Toggle sidebar

Related documents


Partial preview of the text

Download Java Programming for the World Wide Web and more Study notes Software Engineering in PDF only on Docsity! 1 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 2 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 5 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() 6 2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 11 Class Class API • Package java.lang; • Public class Class { • Private Class(); • Public String getName(); • Public Class getSuperclass(); • Public Class[] getInterfaces(); • Public class[] getClasses(); • Pubic class[] getDeclaredClasses(); • Public fileds[] getFields(); • Public methods[] getMethods(); 2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 12 Creating Objects Point pt = new Point(10,10); Class c = object.class; Object o = new c(); • Using No-Argument Constructors Class classDefinition = Class.forName(className); object = classDefinition.newInstance(); • Using Constructors that Have Arguments 1. Create a Class object for the object you want to create. 2. Create a Constructor object by invoking getConstructor on the Class object. The getConstructor method has one parameter: an array of Class objects that correspond to the constructor's parameters. 3. Create the object by invoking newInstance on the Constructor object. The newInstance method has one parameter: an Object array whose elements are the argument values being passed to the constructor. 7 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 10 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) 11 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 12 2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 23 Thread vs. Process • Program Counter • Stack • Register set • Child threads • State • Address space • Global variables • Open files • Child processes • Timers • Signals • Semaphores • Accounting information 2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 24 Create Thread in Java • Extends from java.lang.Thread public myThread extends Thread { public void run() { … … } Thread t = new myThread(“my thread”); t.start(); 15 2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 29 Synchronization of multiple threads • Blocking methods – a blocking method pauses the calling thread until some action occurs. – For example system.in.read(); – Use wait() and notify() to make blocking methods. – Wait() will give up the object’s monitor until the notify() or notifyall() method is called. MyThreadTest.java 2006-1-30 © Dr. Jeff Offutt & Dr. Ye Wu 30 Java Mail • JavaMail API – Pros: Robust, scalable. Does almost anything you could ever want to do. – Cons: Too complex for what you want to do. • Third party email classes – Pros: Open source, easy to use. – Cons: Do not provide as many features as JavaMail. • Sun SMTP class – Pros: Easy to use. – Cons: Unsupported, not portable, lack of features. 16 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 = new SmtpClient ("gmu.edu"); – send.from (“wuye@ise.gmu.edu"); – send.to (“wuye@ise.gmu.edu") ; • 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 <<interface>> java.mail.Message java.mail.internet.MimeMessage java.mail.internet.MimePart <<interface>> java.mail.internet.MimeBodypart java.mail.internet.Bodypart java.mail.internet.MimeMultipart java.mail.MimeMultipart 0..* 1 Session 17 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(“wuye@ise.gmu.edu”)); – Message.setRecipient(InternetAddress(“wuye@ise.gmu.edu”)); – Message.setSubject(“Java Mail”); • Single part – Message.setContent(“Java Mail Test”);
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved