Skip to content

Instantly share code, notes, and snippets.

@hemangsk
Created June 26, 2016 09:49
Show Gist options
  • Save hemangsk/3229c597c36b45e669a08ec313686e1e to your computer and use it in GitHub Desktop.
Save hemangsk/3229c597c36b45e669a08ec313686e1e to your computer and use it in GitHub Desktop.
Head First Java notes.

TABLE OF CONTENTS

  1. Java Basics
  2. Classes, Objects, References, Arguments, Return Values, ArrayLists, Interfaces, Inheritance, and Polymorphism
  3. Object References on the Stack
  4. Constructors and the Garbage Collector
  5. Static and Final
  6. Exception Handling
  7. Events
  8. Graphics
  9. Layout Managers
  10. Object Serialization - Saving Objects
  11. Serialization and File I/O
  12. Networking and Threads
  13. Package, Jar, and Deployment
  14. Remote Deployment with RMI
  15. Servlets and JSP

1. JAVA BASICS

  • 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 break to 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 Object can 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 Object can’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 type Object (meaning, they can be referenced only by an Object reference 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 null when 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 void return type means the method doesn't return anything.
  • If a method declares a non-void return type, it must return a value compatible with the declared return type.
  • ArrayList is a class in the Java API.
  • To put something into an ArrayList, use add().
  • To remove something from an ArrayList, use remove().
  • To find out where something is (and if it is) in an ArrayList, use indexOf().
  • To find out if an ArrayList is empty, use IsEmpty().
  • To get the size (number of elements) in an ArrayList, use the size() method.
  • To get the length (number of elements) in a regular old array, remember, you use the length variable.
  • 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 type Button (or subclasses of Button as you'll learn in the next couple of chapters)
  • Although an ArrayList holds objects and not primitives, the compiler will automatically wrap (and unwrap when you take it out) a primitive into an Object, and place that object in the ArrayList instead 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 ArrayList is really java.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-A test to verify that your inheritance hierarchy is valid. If X extends Y, then X IS-A Y must make sense.
  • The IS-A relationship works in only one direction. A Hippo is an Animal, 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, and class C IS-A class B, and class 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 Object can 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 Object can’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 type Object (meaning, they can be referenced only by an Object reference 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();

3. OBJECT REFERENCES ON THE STACK

  • 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.

4. CONSTRUCTORS AND THE GARBAGE COLLECTOR

  • 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 new on 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.

5. STATIC AND FINAL

  • A static method should be called using the class name rather than an object reference variable: Math.random() vs myFoo.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 static variables) 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 import declaration.
  • 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?

6. EXCEPTION HANDLING

  • 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 has Exception somewhere up its inheritance tree).
  • The compiler does NOT pay attention to exceptions that are of type RuntimeException. A RuntimeException does not have to be declared or wrapped in a try/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 RuntimeExceptions are 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 Exception declaration.
  • 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.

7. EVENTS

  • 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 JFrame using: frame.getcontentPane().add(button);
  • Unlike most other components, the JFrame doesn't let you add to it directly, so you must add to the JFrame'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's ActionEvents, 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.

8. GRAPHICS

  • You can draw 2D graphics directly on to a widget.
  • You can draw a .gif or .jpeg directly onto a widget.
  • To draw your own graphics (including a .gif or .jpeg), make a subclass of JPanel and override the paintComponent() method.
  • The paintComponent() method is called by the GUI system. YOU NEVER CALL IT YOURSELF. The argument to paintComponent() is a Graphics object 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 Graphics object (the paintComponent paramenter) 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 Image using: g.drawImage(image, 3, 4, this);
  • The object referenced by the Graphics parameter to paintComponent() 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 Graphics2D methods, you must cast the parameter from a Graphics object to a Graphics2D object: Graphics2D g2d = (Graphics2D) g;

9. LAYOUT MANAGERS

  • 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 BorderLayout manager 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 use pack()).
  • 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.
  • FlowLayout places 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.
  • FlowLayout gives components their preferred size in both dimensions.
  • BoxLayout lets you align components stacked vertically, even if they could fit side-by-sida. Like FlowLayout, BoxLayout uses the preferred size of the component in both dimensions.
  • BorderLayout is the default layout manager for a frame; FlowLayout is the default for a panel.
  • If you want a panel to use something other than flow, you have to call setLayout() on the panel.

10. OBJECT SERIALIZATION - SAVING OBJECTS

  • You can save an objects state by serializing the object.
  • To serialize an object, you need an ObjectOutputStream (from the java.io package)
  • 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 FileOuputStream and chain into an ObjectOutputStream.
  • To serialize an object, call writeObject(theObject) on the ObjectOutputStream. You do not need to call methods on the FileOutputStream.
  • To be serialized, an object must implement the Serializable interface. If superclass of the class implements Serializable, the subclass will automatically be serializable even if it does not specifically declare implements 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 type Object, 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.

11. SERIALIZATION AND FILE I/O

  • To write a text file, start with a FileWriter connection stream.
  • Chain the FileWriter to a BufferedWriter for efficiency.
  • A File object represents a file at a particular path, but does not represent the actual contents of the file.
  • With a File object you can create, traverse, and delete directories.
  • Most streams that can use a String filename can use a File object as well, and a File object can be safer to use.
  • To read a text file, start with a FileReader connection stream.
  • Chain the FileReader to a BufferedReader for 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 a String up 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.

12. NETWORKING AND THREADS

  • Client and server applications communicate over a Socket connection.
  • A Socket represents a connection between two applications which may (or may not) be running on two different physical machines.
  • A client must know the IP address (or domain name) and TCP port number of the server application.
  • A TCP port is a 16-bit unsigned number assigned to a specific server application. TCP port 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 the Socket.
  • InputStreamReader is 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-level BufferedReader and the low-level Socket input stream.
  • To write text data to the server, create a PrintWriter chained directly to the Socket's output stream. Call the print() or println() methods to send Strings to the server.
  • Servers use a ServerSocket that waits for client requests on a particular port number.
  • When a ServerSocket gets a request it 'accepts' the request by making a Socket connection 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 Thread with a capital 'T' is the java.lang.Thread class. A Thread object represents a thread of execution.
  • A Thread needs a job to do. A Thread's job is an instance of something that implements the Runnable interface.
  • The Runnable interface 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 Runnable to pass to the Thread's constructor.
  • A thread is in the NEW state when you have instantiated a Thread object but have not yet called start().
  • When you start a thread (by calling the Thread object's start() method, a new stack is created, with the Runnable's run() 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 the sleep method. Thread.sleep(200) puts a thread to sleep for 200 milliseconds.
  • The sleep() method throws a checked exception (InterruptedException), so all calls to sleep() must be wrapped in a try/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-timed sleep() 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.

13. PACKAGE, JAR, AND DEPLOYMENT

  • 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, the Foo class 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 -d compiler 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. JAR is based on the pkzip format.
  • You can make an executable JAR file by putting a manifest into the JAR that states which class has the main() 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-class line, or your manifest file may not work.
  • To create a JAR file, 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 JAR file.
  • 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 JAR and a .jnlp file. A .jnlp file is a simple XML document that describes your JWS application. It includes tags for specifying the name and location of the JAR, and the name of the class with the main() method. When a browser gets a .jnlp file from the server (because the user clicked on a link to the .jnlp file), the browser starts up the JWS helper app. The JWS helper app reads the .jnlp file and requests the executable JAR from the Web server. When the JWS gets the JAR, it invokes the main() method (specified in the .jnlp file).

14. REMOTE DEPLOYMENT WITH RMI

  • 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.Remote interface, and all methods must declare RemoteException.
  • Your remote service implements your remote interface.
  • Your remote service should extend UnicastRemoteObject (technically there are other ways to create a remote object, but extending UnicastRemoteObject is 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 RMI registry.
  • To register a remote service, use the static Naming.rebind("Service Name", serviceInstance);
  • The RMI registry 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 RMI can throw a RemoteException (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.

15. SERVLETS AND JSP

  • 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.jar file. The servlet classes are not part of the Java standard libraries, so you need to download the servlets.jar from java.sun.com or 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 ISP that supports servlets, the ISP will tell you which directory to place your servlets in.
  • A typical servlet extends HttpServlet and overrides one or more servlet methods, such as doGet() or doPost().
  • 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 PrintWriter output stream from the response parameter of the doGet() method.
  • The servlet 'writes' out an HTML page, complete with tags.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment