- Java Basics
- Classes, Objects, References, Arguments, Return Values, ArrayLists, Interfaces, Inheritance, and Polymorphism
- Object References on the Stack
- Constructors and the Garbage Collector
- Static and Final
- Exception Handling
- Events
- Graphics
- Layout Managers
- Object Serialization - Saving Objects
- Serialization and File I/O
- Networking and Threads
- Package, Jar, and Deployment
- Remote Deployment with RMI
- Servlets and JSP
- Statements end in a semicolon:
; - Code blocks are defined by a pair of curly braces:
{} - Declare an int variable with a name and a type:
int x - The assignment operator is one equals sign:
= - The equals operator uses two equals signs:
== - A while loop runs everything within its block (defined by curly braces) as long as the conditional test is true.
- If the conditional test is fa1se, the while loop code block won't run, and execution will move down to the code immediately after the loop block.
- Put a boolean test inside parentheses:
while (x == 4) {} - Your Java program should start with a high level design.
- Typically you'll write three things when you create a new class: prepcode, testcode, real (Java) code.
- Prepcode should describe what to do, not how to do it. Implementation comes later.
- Use the prepcode to help design the test code.
- Write test code before you implement the methods. How many hits did you get last month?
- Choose for loops over while loops when you know how many times you want to repeat the loop code.
- Use the pre/post increment operator to add 1 to avariable:
x++; - Use the pre/post decrement to subtract 1 from a variable:
x--; - Use
Integer.parseInt()to get the int value of a String. Integer.parseInt()works only if the String represents a digit ("0", "1", "2", etc.).- Use
breakto leave a loop early (i.e. even if the boolean test condition is still true).
2. CLASSES, OBJECTS, REFERENCES, ARGUMENTS, RETURN VALUES, ARRAYLISTS, INTERFACES, INHERITANCE, AND POLYMORPHISM
- Object-oriented programming lets you extend a program without having to touch previously tested, working code.
- All Java code is defined in a class.
- A class describes how to make an object of that class type. A class is like a blueprint.
- An object can take care of itself; you don’t have to know or care how the object does it.
- An object knows things and does things.
- Things an object knows about itself are called instance variables. They represent the state of an object.
- Things an object does are called methods. They represent the behavior of an object.
- When you create a class, you may also want to create a separate test class which you’ll use to create objects of your new class type.
- A class can inherit instance variables and methods from a more abstract superclass.
- At runtime, a Java program is nothing more than objects ‘talking’ to other objects.
- When you don’t want a class to be instantiated (in other words, you don’t want anyone to make a new object of that class type) mark the class with the abstract keyword.
- An abstract class can have both abstract and non-abstract methods.
- If a class has even one abstract method, the class must be marked abstract.
- An abstract method has no body, and the declaration ends with a semicolon (no curly braces).
- All abstract methods must be implemented in the first concrete subclass in the inheritance tree.
- Every class in Java is either a direct or indirect subclass of class
Object(java.lang.Object). - Methods can be declared with Object arguments and/or return types.
- You can call methods on an object only if the methods are in the class (or interface) used as the reference variable type, regardless of the actual object type. So, a reference variable of type
Objectcan be used only to call methods defined in class Object, regardless of the type of the object to which the reference refers. - A reference variable of type
Objectcan’t be assigned to any other reference type without a cast. A cast can be used to assign a reference variable of one type to a reference variable of a subtype, but at runtime the cast will fail if the object on the heap is NOT of a type compatible with the cast. Example:Dog d = (Dog) x.getObject(aDog); - All objects come out of an
ArrayList<Object>as typeObject(meaning, they can be referenced only by anObjectreference variable, unless you use a cast). - Multiple inheritance is not allowed in Java, because of the problems associated with the “Deadly Diamond of Death”. That means you can extend only one class (i.e. you can have only one immediate superclass).
- An interface is like a 100% pure abstract class. It defines only abstract methods.
- Create an interface using the interface keyword instead of the word class.
- Implement an interface using the keyword implements:
Example:
Dog implements Pet - Your class can implement multiple interfaces.
- A class that implements an interface must implement all the methods of the interface, since all interface methods are implicitly public and abstract.
- To invoke the superclass version of a method from a subclass that’s overridden the method, use the super keyword.
Example:
super.runReport(); - Variables come in two flavors: primitive and reference.
- Variables must always be declared with a name and a type.
- A primitive variable value is the bits representing the value (5, 'a', true, 3.1416, etc.).
- A reference variable value is the bits representing away to get to an object on the heap.
- A reference variable is like a remote control.
- Using the dot operator (
.) on a reference variable is like pressing a button on the remote control to access a method or instance variable. - A reference variable has a value of
nullwhen it is not referencing any object. - An array is always an object, even if the array is declared to hold primitives.
- There is no such thing as a primitive array, only an array that holds primitives.
- Classes define what an object knows and what an object does.
- Things an object knows are its instance variables (state).
- Things an object does are its methods (behavior).
- Methods can use instance variables so that objects of the same type can behave differently.
- A method can have parameters, which means you can pass one or more values in to the method.
- The number and type of values you pass in must match the order and type of the parameters declared by the method.
- Values passed in and out of methods can be implicitly promoted to a larger type or explicitly cast to a smaller type.
- The value you pass as an argument to a method can be a literal value (2, 'c', etc.) or a variable of the declared parameter type (for example, X where X is an int variable). There are other things you can pass as arguments, but we're not there yet.
- A method must declare a return type. A
voidreturn type means the method doesn't return anything. - If a method declares a non-
voidreturn type, it must return a value compatible with the declared return type. ArrayListis a class in the Java API.- To put something into an
ArrayList, useadd(). - To remove something from an
ArrayList, useremove(). - To find out where something is (and if it is) in an
ArrayList, useindexOf(). - To find out if an
ArrayListis empty, useIsEmpty(). - To get the size (number of elements) in an
ArrayList, use thesize()method. - To get the length (number of elements) in a regular old array, remember, you use the
lengthvariable. - An Arraylist resizes dynamically to whatever size is needed. It grows when objects are added, and it shrinks when objects are removed.
- You declare the type of the array using a type parameter, which is a type name in angle brackets:
Example:
ArrayList<Button>means the ArrayList will be able to hold only objects of typeButton(or subclasses ofButtonas you'll learn in the next couple of chapters) - Although an
ArrayListholds objects and not primitives, the compiler will automatically wrap (and unwrap when you take it out) a primitive into anObject, and place that object in theArrayListinstead of the primitive. - Classes are grouped into packages.
- A class has a full name, which is a combination of the package name and the class name.
- Class
ArrayListis reallyjava.util.ArrayList. - To use a class in a package other than
java.lang, you must tell Java the full name of the class. - You use either an import statement at the top of your source code, or you can type the full name every place you use the class in your code.
- A subclass extends a superclass.
- A subclass inherits all public instance variables and methods of the superclass, but does not inherit the private instance variables and methods of the superdass.
- Inherited methods can be overridden; instance variables cannot be overridden (although they can be redefined in the subclass, but that's not the same thing, and there's almost never a need to do it).
- Use the
IS-Atest to verify that your inheritance hierarchy is valid. If X extends Y, thenX IS-A Ymust make sense. - The
IS-Arelationship works in only one direction. AHippois anAnimal, but not all Animals are Hippos. - When a method is overridden in a subclass, and that method is invoked on an instance of the subclass, the overridden version of the method is called (the lowest one wins).
- If class B extends A, and C extends B,
class B IS-A class A, andclass C IS-A class B, andclass C IS-A class A. - When you don’t want a class to be instantiated (in other words, you don’t want anyone to make a new object of that class type) mark the class with the abstract keyword.
- An abstract class can have both abstract and non-abstract methods.
- If a class has even one abstract method, the class must be marked abstract.
- An abstract method has no body, and the declaration ends with a semicolon (no curly braces).
- All abstract methods must be implemented in the first concrete subclass in the inheritance tree.
- Every class in Java is either a direct or indirect subclass of class
Object(java.lang.Object). - Methods can be declared with Object arguments and/or return types.
- You can call methods on an object only if the methods are in the class (or interface) used as the reference variable type, regardless of the actual object type. So, a reference variable of type
Objectcan be used only to call methods defined in classObject, regardless of the type of the object to which the reference refers. - A reference variable of type
Objectcan’t be assigned to any other reference type without a cast. A cast can be used to assign a reference variable of one type to a reference variable of a subtype, but at runtime the cast will fail if the object on the heap is NOT of a type compatible with the cast. Example:Dog d = (Dog) x.getObject(aDog); - All objects come out of an
ArrayList<Object>as typeObject(meaning, they can be referenced only by anObjectreference variable, unless you use a cast). - Multiple inheritance is not allowed in Java, because of the problems associated with the “Deadly Diamond of Death”. That means you can extend only one class (i.e. you can have only one immediate superclass).
- An interface is like a 100% pure abstract class. It defines only abstract methods.
- Create an interface using the interface keyword instead of the word class.
- Implement an interface using the keyword implements:
Example:
Dog implements Pet - Your class can implement multiple interfaces.
- A class that implements an interface must implement all the methods of the interface, since all interface methods are implicitly public and abstract.
- To invoke the superclass version of a method from a subclass that’s overridden the method, use the super keyword:
Example:
super.runReport();
- Java has two areas of memory we care about the Stack and the Heap.
- Instance variables are variables declared inside aclass but outside any method.
- Local variables are variables declared inside a method or method parameter.
- All local variables live on the stack, in the frame corresponding to the method where the variables are declared.
- Object reference variables work just like primitive variables -- if the reference is declared as a local variable, it goes on the stack.
- All objects live in the heap, regardless of whether the reference is a local or instance variable.
- Instance variables live within the object they belong to, on the Heap.
- If the instance variable is reference to an object, both the reference and the object it refers to are on the Heap.
- A constructor is the code that runs when you say
newon a class type. - A constructor must have the same name as the class, and must not have a return type.
- You can use a constructor to initialize the state (i.e. the instance variables) of the object being constructed.
- If you don't put a constructor in your class, the compiler will put in a default constructor.
- The default constructor is always a no-arg constructor.
- If you put a constructor--any constructor--in your class, the compiler will not build the default constructor.
- If you want a no-arg constructor, and you already put in a constructor with arguments, you'll have to build the no-arg constructor yourself.
- Always provide a no-arg constructor if you can, to make it easier for programmers to make a working object. Supply default values.
- Overloaded constructors means you have more than one constructor in your class.
- Overloaded constructors must have different argument lists.
- You cannot have two constructors with the same argument lists. An argument list includes the order and/or type of arguments.
- Instance variables are assigned a default value, even when you don't explicitly assign one. The default values are 0/0.0/false for primitives, and null for references.
- A static method should be called using the class name rather than an object reference variable:
Math.random()vsmyFoo.go() - A static method can be invoked without any instances of the method's class on the heap.
- A static method is good for a utility method that does not (and will never) depend on a particular instance variable value.
- A static method is not associated with a particular instance--only the class--so it cannot access any instance variable values off its class. It wouldn't know which instance's values to use.
- A static method cannot access a non-static method, since non-static methods are usually associated with instance variable state.
- If you have a class with only static methods, and you do not want the class to be instantiated, you can mark the constructor private.
- A static variable is a variable shared by all members of a given class. There is only one copy of a static variable in aclass, rather than one copy per each individual instance for instance variables.
- A static method can access a static variable.
- To make a constant in Java, mark a variable as both static and final.
- A final static variable must be assigned a value either at the time it is declared, or in a static initializer.
static
{DOG_CODE:: 420;} - The naming convention for constants (
final staticvariables) is to make the name an uppercase. - A final variable value cannot be changed once it has been assigned.
- Assigning a value to a final instance variable must be either at the time it is declared, or in the constructor.
- A final method cannot be overridden.
- A final class cannot be extended (subclassed).
- If you're only going to use a static member a few times, we think you should avoid static imports, to help keep the code more readable.
- If you're going to use a static member a lot, (like doing lots of Math calculations), then it's probably OK to use the
static import. - Notice that you can use wildcards (*), in your
static importdeclaration. - A big issue with static imports is that it's not too hard to create naming conflicts. For example, if you have two different classes with an
add()method, how will you and the compiler know which one to use?
- A method can throw an exception when something fails at runtime.
- An exception is always an object of type
Exception(which, as you remember from the polymorphism chapters means the object is from a class that hasExceptionsomewhere up its inheritance tree). - The compiler does NOT pay attention to exceptions that are of type
RuntimeException. ARuntimeExceptiondoes not have to be declared or wrapped in atry/catch(although you're free to do either or both of those things). - All exceptions the compiler cares about are called 'checked exceptions' which really means compiler-checked exceptions. Only
RuntimeExceptionsare excluded from compiler checking. All other exceptions must be acknowledqed in your code, according to the rules. - A method throws an exception with the keyword throw, followed by a new exception object:
throw new NoCaffeineException(); - Methods that might throw a checked exception must announce it with a throws
Exceptiondeclaration. - If your code calls a checked-exception-throwing method, it must reassure the complier that precautions have been taken.
- If you're prepared to handle the exception, wrap the call in a
try/catch, and put your exception handling/recovery code in the catch block. - If you're not prepared to handle the exception, you can still make the compiler happy by officially 'ducking' the exception.
- To make a GUI, start with a window, usually a
JFrame:JFrame frame = new JFrame(); - You can add widgets (buttons, text fields, etc.) to the
JFrameusing:frame.getcontentPane().add(button); - Unlike most other components, the
JFramedoesn't let you add to it directly, so you must add to theJFrame's content pane. - To make the window (
JFrame) display, you must give it a size and tell it be visible:frama.setSize(300,300);frame.sBtVisihle(true); - To know when the user clicks a button (or takes some other action on the user interface) you need to listen for a GUI event.
- To listen for an event, you must register your interest with an event source. An event source is the thing (button, checkbox, etc.) that 'fires' an event based on user interaction.
- The listener interface gives the event source away to call you back, because the interface defines the method(s) the event source will call when an event happens.
- To register for events with a source, call the source's registration method. Registration methods always take the form of:
add<EventType>Listener. To register for a button'sActionEvents, for example, call:button.addActionListener(this); - Implement the listener interface by implementing all of the interface's event-handling methods. Put your event-handling code in the listener call-back method. For
ActionEvents, the method is:public void actionPerformed(ActionEvent event){ button. setText ("you clicked!"); }
- The event object passed into the event-handler method carries information about the event, including the source of the event.
- You can draw 2D graphics directly on to a widget.
- You can draw a
.gifor.jpegdirectly onto a widget. - To draw your own graphics (including a
.gifor.jpeg), make a subclass ofJPaneland override thepaintComponent()method. - The
paintComponent()method is called by the GUI system. YOU NEVER CALL IT YOURSELF. The argument topaintComponent()is aGraphicsobject that gives you a surface to draw on, which will end up on the screen. You cannot construct that object yourself. - Typical methods to call on a
Graphicsobject (thepaintComponentparamenter) are:graphics.setColor(Color.blue);g.fillRect(20,SO,lOO,120); - To draw a
.jpg, construct an image using:Image image = new ImageIcon("catzilla.jpg").getImage(); - and draw the
Imageusing:g.drawImage(image, 3, 4, this); - The object referenced by the
Graphicsparameter topaintComponent()is actually an instance of the Graphics2D class. The Graphics2D class has a variety of methods including:fill3DRect(),draw3DRect(),rotate(),scale(),shear(),transform() - To invoke the
Graphics2Dmethods, you must cast the parameter from aGraphicsobject to aGraphics2Dobject:Graphics2D g2d = (Graphics2D) g;
- Layout managers control the size and location of components nested within other components.
- When you add a component to another component (sometimes referred to as a background component but that's not a technical distinction), the added component is controlled by the layout manager of the background component.
- A layout manager asks components for their preferred size, before making a decision about the layout. Depending on the layout manager's policies, it might respect all, some, or none of the component's wishes.
- The
BorderLayoutmanager lets you add a component to one of five regions. You must specify the region when you add the component, using the following syntax:add(BorderLayout. EAST, panel); - With
Bordertayout, components in the north and south get their preferred height but not width. Components in the east and west get their preferred width, but not height the component in the center gets whatever is left over (unless you usepack()). - The
pack()method is like shrink-wrap for the components--it uses the full preferred size of the center component, then determines the size of the frame using the center as astarting point, building the rest based on what's in the other regions. FlowLayoutplaces components left to right, top to bottom, in the order they were added, wrapping to a new line of components only when the components won't fit horizontally.FlowLayoutgives components their preferred size in both dimensions.BoxLayoutlets you align components stacked vertically, even if they could fit side-by-sida. LikeFlowLayout,BoxLayoutuses the preferred size of the component in both dimensions.BorderLayoutis the default layout manager for a frame;FlowLayoutis the default for a panel.- If you want a panel to use something other than flow, you have to call
setLayout()on the panel.
- You can save an objects state by serializing the object.
- To serialize an object, you need an
ObjectOutputStream(from thejava.iopackage) - Streams are either connection streams or chain streams
- Connection streams can represent a connection to a source or destination, typically a file, network socket connection, or the console.
- Chain streams cannot connect to a source or destination and must be chained to a connection (or other) stream.
- To serialize an object to a file, make a
FileOuputStreamand chain into anObjectOutputStream. - To serialize an object, call
writeObject(theObject)on theObjectOutputStream. You do not need to call methods on theFileOutputStream. - To be serialized, an object must implement the
Serializableinterface. If superclass of the class implementsSerializable, the subclass will automatically be serializable even if it does not specifically declareimplements Serializable. - When an object is serialized, its entire object graph is serialized. That means any objects referenced by the serialized object's instance variables are serialized, and any objects referenced by those objects...and so on.
- If any object in the graph is not serializable, an exception will be thrown at runtime, unless the instance variable referring to the object is skipped.
- Mark an instance variable with the transient keyword if you want serialization to skip that variable. The variable will be restored as
null(for object references) or default values (for primitives). - During deserialization, the class of all objects in the graph must be available to the JVM.
- You read objects in (using
readObject()) in the order in which they were originally written. - The return type of
readObject()is typeObject, so deserialized objects must be cast to their real type. - Static variables are not serialized! It doesn't make sense to save a static variable value as part of a specific objects state, since all objects of that type share only a single value--the one in the class.
- To write a text file, start with a
FileWriterconnection stream. - Chain the
FileWriterto aBufferedWriterfor efficiency. - A
Fileobject represents a file at a particular path, but does not represent the actual contents of the file. - With a
Fileobject you can create, traverse, and delete directories. - Most streams that can use a
Stringfilename can use aFileobject as well, and aFileobject can be safer to use. - To read a text file, start with a
FileReaderconnection stream. - Chain the
FileReaderto aBufferedReaderfor efficiency. - To parse a text file, you need to be sure the file is written with some way to recognize the different elements. A common approach is to use some kind of character to separate the individual pieces.
- Use the
String split()method to split aStringup into individual tokens. A String with one separator will have two tokens, one on each side of the separator. The separator doesn't count as a token.
- Client and server applications communicate over a
Socketconnection. - A
Socketrepresents a connection between two applications which may (or may not) be running on two different physical machines. - A client must know the
IPaddress (or domain name) andTCPport number of the server application. - A
TCPport is a16-bitunsigned number assigned to a specific server application.TCPport numbers allow different clients to connect to the same machine but communicate with different applications running on that machine. - The port numbers from 0 through 1023 are reserved for 'well-known services' including
HTTP,FTP,SMTP, etc. - A client connects to a server by making a Server socket:
Socket s = new Socket("127.0.0.1", 4200); - Once connected, a client can get input and output streams from the socket. These are low-level 'connection' streams:
sock.getInputStream(); - To read text data from the server, create a
BufferedReader, chained to an InputStreamReader, which is chained to the input stream from theSocket. InputStreamReaderis a 'bridge' stream that takes in bytes and converts them to text (character) data. Its used primarily to act as the middle chain between the high-levelBufferedReaderand the low-levelSocketinput stream.- To write text data to the server, create a
PrintWriterchained directly to theSocket's output stream. Call theprint()orprintln()methods to sendStrings to the server. - Servers use a
ServerSocketthat waits for client requests on a particular port number. - When a
ServerSocketgets a request it 'accepts' the request by making aSocketconnection with the client. - A thread with a lower-case 't' is a separate thread of execution in Java.
- Every thread in Java has its own call stack.
- A
Threadwith a capital 'T' is thejava.lang.Threadclass. AThreadobject represents a thread of execution. - A
Threadneeds a job to do. AThread's job is an instance of something that implements theRunnableinterface. - The
Runnableinterface has just a single method,run(). This is the method that goes on the bottom of the new call stack. In other words, it is the first method to run in the new thread. - To launch a new thread, you need a
Runnableto pass to theThread's constructor. - A thread is in the NEW state when you have instantiated a
Threadobject but have not yet calledstart(). - When you start a thread (by calling the
Threadobject'sstart()method, a new stack is created, with theRunnable'srun()method on the bottom of the stack. The thread is now in the RUNNABLE state, waiting to be chosen to run. - A thread is said to be RUNNING when the JVM's thread scheduler has selected it to be the currently running thread. On a single-processor machine, there can be only one currently-running thread.
- Sometimes a thread can be moved from the RUNNING state to a BLOCKED (temporarily non-runnable) state. A thread might be blocked because it's waiting for data from a stream, or because it has gone to sleep, or because it is waiting for an object's lock.
- Thread scheduling is not guaranteed to work in any particular way, so you cannot be certain that threads wlll take turns nicely. You can help influence turn-taking by putting your threads to sleep periodically.
- The static
Thread.sleep()method forces a thread to leave the running state for at least the duration passed to thesleepmethod.Thread.sleep(200)puts a thread to sleep for200 milliseconds. - The
sleep()method throws a checked exception (InterruptedException), so all calls tosleep()must be wrapped in atry/catch, or declared. - You can use
sleep()to help make sure all threads get a chance to run, although there's no guarantee that when a thread wakes up it'll go to the end of the runnable line. It might, for example, go right back to the front. In most cases, appropriately-timedsleep()calls are all you need to keep your threads switching nicely. - You can name a thread using the (yet another surprise)
setName()method. All threads get a default name, but giving them an explicit name can help you keep track of threads, especially if you're debugging with print statements. - You can have serious problems with threads if two or more threads have access to the same object on the heap.
- Two or more threads accessing the same object can lead to data corruption if one thread, for example, leaves the running state while still in the middle of manipulating an object's critical state.
- To make your objects thread-safe, decide which statements should be treated as one atomic process. In other words, decide which methods must run to completion before another thread enters the same method on the same object.
- Use the keyword synchronized to modify a method declaration, when you want to prevent two threads from entering that method.
- Every object has a single lock, with a single key for that lock. Most of the time we don't care about that lock; locks come into play only when an object has synchronized methods.
- When a thread attempts to enter a synchronized method, the thread must get the key for the object (the object whose method the thread is trying to run). If the key is not available (because another thread already has it), the thread goes into a kind of waiting lounge, until the key becomes available.
- Even if an object has more than one synchronized method, there is still only one key. Once any thread has entered a synchronized method on that object, no thread can enter any other synchronized method on the same object. This restriction lets you protect your data by synchronizing any method that manipulates the data.
- Organize your project so that your source code and class files are not in the same directory.
- A standard organization structure is to create a project directory, and then put a source directory and a classes directory inside the project directory.
- Organizing your classes into packages prevents naming collisions with other classes, if you prepend your reverse domain name on to the front of a class name. To put a class in a package, put a package statement at the top of the source code file, before any import statements:
package com.wickcedlysmart; - To be in a package, a class must be in a directory structure thet exactly matches the package structure. For a class,
com.wickedlysmart.Foo, theFooclass must be in a directory named wickedlysmart, which is in a directory named com. - To make your compiled class land in the correct package directory structure under the classes directory, use the
-dcompiler flag:% cd source %javac -d ../classes com/wickcedlysmart/Foo.java - To run your code, cd to the classes directory, and give the fully-quallfied name of your class:
%cd classes %java com.wickedlyamart.Foo - You can bundle your classes into
JAR(Java ARchive) files.JARis based on thepkzipformat. - You can make an executable
JARfile by putting a manifest into theJARthat states which class has themain()method. To create a manifest file, make a text file with an entry like the following (for example):Main-Class: com.wickedlysmart.Foo - Be sure you hit the return key after typing the
Main-classline, or your manifest file may not work. - To create a
JARfile, type:jar -cvfm manifest.txt MyJar.jar com - The entire package directory structure (and only the directories matching the package) must be immediately inside the
JARfile. - To run an executable JAR file, type:
java -jar MyJar.jar - Web Start technology lets you deploy a stand-alone client application from the Web. Java Web Start includes a 'helper app' that must be installed on the client (along with Java). A Java Web Start (JWS) app has two pieces: an executable
JARand a.jnlpfile. A .jnlpfile is a simpleXMLdocument that describes yourJWSapplication. It includes tags for specifying the name and location of theJAR, and the name of the class with themain()method. When a browser gets a.jnlpfile from the server (because the user clicked on a link to the.jnlpfile), the browser starts up theJWShelper app. TheJWShelper app reads the.jnlpfile and requests the executableJARfrom the Web server. When theJWSgets theJAR, it invokes themain()method (specified in the.jnlpfile).
- An object on one heap cannot get a normal Java reference to an object on a different heap (which means running on a different JVM).
- Java Remote Method Invocation (
RMI) makes it seem like you're calling a method on a remote object (i.e. an object in a different JVM), but you aren't - When a client calls a method on a remote object, the client is really calling a method on a proxy of the remote object. The proxy is called a 'stub'.
- A stub is a client helper object that takes care of the low-level networking details (sockets, streams, serialization, etc.) by packaging and sending method calls to the server.
- To build a remote service (in other words, an object that a remote client can ultimately call methods on), you must start with a remote interface.
- A remote interface must extend the
java.rmi.Remoteinterface, and all methods must declareRemoteException. - Your remote service implements your remote interface.
- Your remote service should extend
UnicastRemoteObject(technically there are other ways to create a remote object, but extendingUnicastRemoteObjectis the simplest). - Your remote service class must have a constructor, and the constructor must declare a RemoteException (because the superclass constructor declares one).
- Your remote service must be instantiated, and the object reg0istered with the
RMIregistry. - To register a remote service, use the static
Naming.rebind("Service Name", serviceInstance); - The
RMIregistry must be running on the same machine as the remote service, before you try to register a remote object with the RMI registry. - The client looks up your remote service using the static
Naming.lookup{"rmi://MyHostName/ServiceName"); - Almost everything related to
RMIcan throw aRemoteException(checked bythe compiler). This includes registering or looking up a service in the registry, and all remote method calls from the client to the stub.
- Servlets are Java classes that run entirely on (and/or within) an HTTP (web) server.
- Servlets are useful for running code on the server as a result of client interaction with a web page. For example, if a client submits information in a web page form, the servlet can process the information, add it to a database, and send back a customized confirmation response page.
- To compile a servlet, you need the servlet packages which are in the
servlets.jarfile. The servlet classes are not part of the Java standard libraries, so you need to download theservlets.jarfromjava.sun.comor get them from a servlet capable web server (note: the Servlet library is included with the Java 2 Enterprise Edition (J2EE)). - To run a servlet, you must have a web server capable of running servlets, such as the Tomcat server from
apache.org. - Your servlet must be placed in a location that's specific to your particular web server, so you'll need to find that out before you try to run your servlets. If you have a web site hosted by an
ISPthat supports servlets, theISPwill tell you which directory to place your servlets in. - A typical servlet extends
HttpServletand overrides one or more servlet methods, such asdoGet()ordoPost(). - The web server starts the servlet and calls the appropriate method (
doGet(), etc.) based on the client's request. - The servlet can send back a response by getting a
PrintWriteroutput stream from the response parameter of thedoGet()method. - The servlet 'writes' out an HTML page, complete with tags.