View RSS Feed

All Blog Entries

  1. How to use HTML to display an Applet in browsers

    by , 12-08-2011 at 12:19 PM
    Applet is a Java based application which is embedded into a web page. Its advantage is to provide rich user interface experience and interaction just like other desktop applications, while still keeping the powerful features of Java like cross-platform, security, multi-threadingÖ
    This article will guide you through some basic steps to build a simple applet, use HTML code to display the applet in browsers. In addition, you will learn how to passing parameters and loading external libraries ...
  2. Using Spring Web Flow Part 2

    by , 11-30-2011 at 06:00 AM
    In the previous article, we looked at the components of Spring Web Flow. In this article, I will tie all the pieces together for the application.

    As you know, Spring Web Flow provides a powerful controller to control the user navigation in case your application requires it. Below is the definition of a simple flow to carry out a booking process is shown graphically below:

    Why Spring Web Flow
    We know that defining and understanding page flow of a complex web application ...

    Updated 12-05-2011 at 02:45 PM by Spring Framework

    Categories
    Webflow , MVC , Spring 3 , Web Flow , Spring Flow
  3. Using Spring Web Flow Part 1

    by , 11-30-2011 at 05:52 AM
    In a previous article, I gave an introduction to Spring Web Flow. In this article, I will delve into the details of how to use Spring Web Flow.

    In Spring Web Flow, a flow is defined by three primary elements: states, transitions, and flow data. States are points in a flow where something happens. Consider if you want to plan a short holiday in Florence. You need to first choose your flights, then you choose your hotel, you might also choose a car, etc. Similarly, as a flow progresses, ...
  4. Viewing Options in Spring

    by , 11-30-2011 at 05:38 AM
    In your application, once the service have handled the request and returned the results desired, you will need to render the results in a useful manner to the user. This is the purpose of viewing technology such as JavaServer Pages (JSP). Although you can use JSPs in Spring there are a number of other viewing technologies that Spring support. In this tip, I will outline some of the other view technologies that are available.

    When Spring is looking to render some content, the DispatcherServlet ...
  5. Using Inheritance - Substitution vs. Extension

    by , 11-30-2011 at 03:51 AM
    We are going to look at inheritance hierarchy. If we take a classic approach, as shown below we would see the methods that have been defined in the base class overridden in the derived classes. Inheritance guarantees that any derived class will have the interface of the base class. If you follow this diagram below, derived classes will also have no more than the base-class interface.

    Name:  Inheritance-Example.jpg
Views: 1035
Size:  16.7 KB

    This can be thought of as pure substitution, because ...
  6. Design Principle: Choose Composition vs. Inheritance

    by , 11-30-2011 at 03:48 AM
    With composition or inheritance you can place sub-objects inside your new class. If you do this explicitly than you are using composition otherwise if you do this implicitly, you are using inheritance. I will outline the difference between the two below as well as point out one of the key design patterns to use in your development. Prefer composition vs inheritance.

    Composition is used when you want the features of an existing class to be included inside your new class, but not be ...
  7. Using Name Hiding for Overriding

    by , 11-30-2011 at 03:45 AM
    Sometimes you will have a java base class that has a method thatís been overloaded several times. It is important for the programmer to remember that redefining the method name in the derived class does not hide any of the base-class versions. Therefore you should keep in mind that overloading works irrespective of whether the method has been defined at this level or in a base class:

    Java Code:
    import static com.acme.examples.Print.*;
    
    public class Mother {
    	char foo(char
    ...
  8. Using Imports to Change Behaviour

    by , 11-30-2011 at 03:36 AM
    One thing that would be great to have is a feature that I know well from my previous experience coding in C. That is the possibility to conditional compile in order to modify the behavior of the application. A feature that is missing from Java is Cís conditional compilation, which allows you to change a switch and get different behavior without changing any other code. I think is was left out of Java is that I remember using this feature often to address issues related to compiling a program on ...
    Categories
    Tutorial , Import
  9. Use an Interface to Compare Objects in Generics

    by , 11-30-2011 at 12:25 AM
    A key aspect of using generics is being able to compare different objects. Consider an example where you are comparing different species of felines.
    Java Code:
    public class ComparableFeline implements Comparable<ComparableFeline> {
    
    	@Override
    	public int compareTo(ComparableFeline arg0) {
    		return 0;
    	}
    }
    So if you wanted to narrow ComparableFeline for use only among different types of domesticated cats instead of other felines such as ...

    Updated 11-30-2011 at 09:37 AM by Advanced Java

    Categories
    Generics
  10. Use Lists instead Arrays in Generics

    by , 11-30-2011 at 12:20 AM
    Generics were added to java in release 1.5. It allows you to avoid needing to cast objects as they are read from a collection. Instead you tell the compiler what types of objects that you want to hold in the collection. Often it is difficult to know which generic to use when you have two or more who seem to be able to complete the same function. Consider the choice between whether to use a list or an array. Now a key thing to remember is that arrays are covariant and know and enforce their element ...
  11. Wildcards with Generics

    by , 11-30-2011 at 12:16 AM
    Unlike with arrays, it is important to remember with generics that is not possible to upcast an object from its superclass to its subclass. So for example, this will create a compile time error.

    Java Code:
    import java.util.*;
    
    public class NonCovariantGenerics { 
    // Compile Error: incompatible types: 
    List<Shape> slist = new ArrayList<Rectangle>();
    In this listing List of Rectangle is not type-equivalent to a List of Shape, even if an Rectangle ...
  12. Supertype Wildcards with Generics

    by , 11-30-2011 at 12:12 AM
    In my last tip, I showed how you can use a wildcard with generics. It is also possible to use a supertype wildcards. In this case the wildcard is bounded by any base class of a particular class, by specifying <? super MyClass> or even using a type parameter: <? super T> (although you cannot give a generic parameter a supertype bound; that is, you cannot say <T super MyClass>). This allows you to safely pass a typed object into a generic type. Thus, with supertype wildcards you ...
  13. Unbounded Wildcards with Generics

    by , 11-30-2011 at 12:09 AM
    When you as a programmer decide to use an unbounded wildcard <?>, it appears to mean anything. and so using an unbounded wildcard seems equivalent to using a raw type. There are time that the compiler is indifferent as to using raw type or <?>. If you use <?> it can be considered as a decoration. When you are using the raw type, the generic parameter can hold any type. In this example I show an important use of unbounded wildcards. If you are dealing with multiple generic parameters, ...
  14. Unnecessary Object Creation with Immutable Objects

    by , 11-30-2011 at 12:05 AM
    Programmers are often not aware of how to create objects that can be reused. The key to understanding how to create reusable objects is that they must be immutable. So for example if you a running a loop in which each loop a string instance is required, rather than create a new instance every time it executes, you simply reuse the existing instance.

    Rather than create a new String instance.

    Java Code:
    public void BuildingLoop{
    	int total = 10;
    	
    	for(int i=0;
    ...
  15. Unnecessary Object Creation with Mutable Objects

    by , 11-29-2011 at 11:59 PM
    Continuing with our last tip on immutable objects, we now look at re-using mutable objects. Although you cannot modify a mutable object, you can avoid unnecessarily waste memory by reusing the existing instance. So consider for example mutable Date objects. Once the values are computed, they are never modified. So for example if you a running a loop in which each loop a string instance is required, rather than create a new instance every time it executes, you simply reuse the existing instance. ...
  16. Dynamic Type Safety in Pre Java 5 Code Collections

    by , 11-29-2011 at 11:40 PM
    Sometimes is might be necessary to pass generic containers to pre-Java5 code. In those cases, it is possible that the old-style code can corrupt your containers. In order to address this issue, there are a set of utilities in the java.util.Collections package to address the issues around type-checking. There are a number of static methods designed for this. They all take the container that you are checking as the first argument and the type you want to enforce as the second argument. They are shown ...
  17. Comparing Objects and HashCode

    by , 11-29-2011 at 11:30 PM
    Another thing that many programmers forget when they make objects that they need to compare in their applications is that it is not enough to assume that if your object is a subclass of Object, that the hashCode method that you can use it in a HashMap, Hashtable or HashSet. In fact, the Java specification states that you must always override the hashCode when ever you override the equals method. Otherwise, you have violated one of the key tenants of Java in not ensuring that equal objects have equal ...
  18. Sorting Collections

    by , 11-29-2011 at 06:23 PM
    If you are interested in sorting your collections, the good news is that since the inception of Java 5 it is now possible for collections and arrays to be sorted. This is very useful because it is a hell of a lot easier to find something in a sorted set than in an unsorted set. There are a number of sorting algorithms that can be used for your collections. There are listed below.
    • Bubble Sort algorithm
    • Bi-direction Bubble Sort algorithm
    • Quick Sort algorithm
    • Generic Sorting algorithm
    ...
    Categories
    Tutorial , Sorting
  19. Composition Syntax for Reuse

    by , 11-29-2011 at 06:21 PM
    One of the best features of Java is code reuse. But like any tool, it needs to be used effectively. Most programmers might be lead to think that when I talk of reuse only relates to extending a class. Now there is nothing wrong with reusing a class by extending it or using it directly. This type of reuse is using the class without the risk changing the existing code. First we can create objects of the existing class inside the new class. We call this composition. It is a new class composed of objects ...
  20. How to do Overloading with Generics

    by , 11-29-2011 at 05:12 PM
    Overloading is a technique often used in object orientated programming languages such as java. In the context of generics unfortunately, classic type of overloading will produce a compile time error because of erasure.

    Java Code:
    import java.util.List;
    
    public class OverloadingExample<W, T> {
    	void f(List<T> v) {
    	}
    
    	void f(List<W> v) {
    	}
    }

    What you must do to overcome this problem is to provide ...
  21. Generics - Exceptions

    by , 11-29-2011 at 04:35 AM
    Due to erasure, exception use is limited with generics. As exceptions are know at both compile and at run time, it renders redundant the use of catch clauses. This also means that a generic class cannot inherit directly or indirectly from Throwable.

    One can though use type parameters in the throws clause of a method declaration. This will allow you to write generic code that varies with the type of a checked exception:

    Java Code:
    import java.util.List;
    
    public interface
    ...
  22. Using Imports to Change Behaviour

    by , 11-29-2011 at 04:24 AM
    One thing that would be great to have is a feature that I know well from my previous experience coding in C. That is the possibility to conditional compile in order to modify the behaviour of the application. A feature that is missing from Java is Cís conditional compilation, which allows you to change a switch and get different behaviour without changing any other code. I think is was left out of Java is that I remember using this feature often to address issues related to compiling a program ...
    Categories
    Classes
  23. Managing Initialization and Class Loading

    by , 11-29-2011 at 04:20 AM
    Java avoids many of the problems found with other languages related to the startup and initialization of objects in the right order, by taking a different approach to loading. The key to this is that everything in Java is an object. Each class exists in its own separate file. Therefore files are not loaded until the code is needed. The expression of this is that ďclass code is loaded at the point of first use.Ē This is usually when the first object of that class is constructed, but loading also ...
  24. Basics of Exceptions

    by , 11-29-2011 at 04:09 AM
    One of the key aspects of programming is understanding how to handle exceptions. An exception is a problem that prevents the continuation of the current method or scope. Itís important to distinguish an exceptional condition from a normal problem, in which you have enough information in the current context to somehow cope with the difficulty. With an exception, you cannot continue processing because you lack the information necessary to address the problem in its current context. The only recourse ...

    Updated 11-30-2011 at 04:19 AM by Java Exception

    Categories
    Error Handling , Tutorial
  25. Catching an Exception

    by , 11-29-2011 at 04:07 AM
    Following on from our previous tip on exceptions, in this tip we will look at catching an exception. One of the key concepts to understand is the concept of a guarded region. It is a section of code that might produce exceptions and is followed by the code to handle a particular exception. They are often grouped into all the possible exceptions for a particular method or piece of code.

    The try block
    The first part of the region is the try block. This is when youíre inside a ...
  26. How to Handle Input with Spring MVC

    by , 11-29-2011 at 03:57 AM
    Spring MVC is structured to handle state management, workflow and validation. It is an application of the Model-View-Controller pattern. As with all things Spring, it follows the principles of loose coupling and flexibility. When a request is made, it reaches the DispatcherServlet whose role is to forward the request to the relevant Spring MVC controller, it is the controller that processes the request. In this article we will look in depth in what a Spring Controller does when it processes input. ...
  27. How to Inject AspectJ Aspects into Spring

    by , 11-29-2011 at 03:53 AM
    For those of you who have already worked with Spring AOP knows that it is sufficient for many of the applications of aspects that are generally done in a system. Still it is weak in certain areas of AOP solution in contrast to AspectJ that has added a number of powerful features over time. There are now many types of AspectJ pointcuts that arenít possible with Spring AOP.

    For example, Springís proxy-based AOP is not able to advise on object creation. This is because constructors in ...
    Categories
    AOP , Spring 3
  28. Introduction to Spring Web Flow

    by , 11-29-2011 at 03:49 AM
    When you are developing an user interaction for an application, one of the most challenging aspects is to define a proper narrative for the behavior of the application so that the user can get into a flow while using the application. Sometimes it is necessary to step a user through a process, such as ordering a plane ticket. This is called a flowed application. Now is a possible to use any web framework, such as Struts or Java Server Faces, in order to do this. Unfortunately none of them properly ...
  29. Data Access with Java Persistence API

    by , 11-29-2011 at 03:44 AM
    Spring integration with Java Persistence API (JPA) came about on the release of Spring 2.0. It is very easy to use JPA with Spring because it is all based on plain old java beans (POJOs). In this tip, we will outline the steps required for you to use JPA with Spring.

    JPA-based applications are based on the implementation of EntityManagerFactory in order to get an instance of an EntityManager. There are two kinds of entity managers from the JPA Specification that both implement the ...

    Updated 11-30-2011 at 04:23 AM by Spring Framework

    Categories
    JPA , Spring 3 , EntityManager , EntityManagerFactory
  30. The Benefits of JAXP

    by , 11-29-2011 at 02:58 AM
    One of the most important technologies available in java is the APIs used to work with XML. There are basically two ways to work with XML documents. SAX involves an event driven means of processing XML using callbacks to handle the relevant events. DOM involves using an in-house tree structure of the XML document. Sun Microsystems created a Java API for XML Processing (JAXP) toolkit which makes XML manageable for all developers to use. It is a key component for exploiting all the possibilities with ...

    Updated 11-30-2011 at 03:01 PM by Java XML

    Categories
    JAXP , DOM , SAX.
Page 30 of 48 FirstFirst ... 20282930313240 ... LastLast