Saturday, December 02, 2006

JavaServer Faces Technology Benefits

One of the greatest advantages of JavaServer Faces technology is that it offers a clean separation between behavior and presentation. Web applications built using JSP technology achieve this separation in part. However, a JSP application cannot map HTTP requests to component-specific event handling nor manage UI elements as stateful objects on the server, as a JavaServer Faces application can. JavaServer Faces technology allows you to build web applications that implement the finer-grained separation of behavior and presentation that is traditionally offered by client-side UI architectures.

The separation of logic from presentation also allows each member of a web application development team to focus on his or her piece of the development process, and it provides a simple programming model to link the pieces. For example, page authors with no programming expertise can use JavaServer Faces technology UI component tags to link to server-side objects from within a web page without writing any scripts.

Another important goal of JavaServer Faces technology is to leverage familiar UI-component and web-tier concepts without limiting you to a particular scripting technology or markup language. Although JavaServer Faces technology includes a JSP custom tag library for representing components on a JSP page, the JavaServer Faces technology APIs are layered directly on top of the Servlet API, as shown in Figure 3-2. This layering of APIs enables several important application use cases, such as using another presentation technology instead of JSP pages, creating your own custom components directly from the component classes, and generating output for various client devices.

Most importantly, JavaServer Faces technology provides a rich architecture for managing component state, processing component data, validating user input, and handling events.

Friday, November 03, 2006

1. How could Java classes direct program messages to the system console, but error messages, say to a file?


A. The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed:

Stream st = new Stream(new FileOutputStream("output.txt")); System.setErr(st); System.setOut(st);

2. What's the difference between an interface and an abstract class?

A. An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.

3. Why would you use a synchronized block vs. synchronized method?

A. Synchronized blocks place locks for shorter periods than synchronized methods.

4. Explain the usage of the keyword transient?

A. This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).

5. How can you force garbage collection?

A. You can't force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately.

6. How do you know if an explicit object casting is needed?

A. If you assign a superclass object to a variable of a subclass's data type, you need to do explicit casting. For example:

Object a; Customer b; b = (Customer) a;

When you assign a subclass to a variable having a supeclass type, the casting is performed automatically.

7. What's the difference between the methods sleep() and wait()

A. The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

8. Can you write a Java class that could be used both as an applet as well as an application?

A. Yes. Add a main() method to the applet.

9. What's the difference between constructors and other methods?

A. Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.

10. Can you call one constructor from another if a class has multiple constructors

A. Yes. Use this() syntax.

11. Explain the usage of Java packages.

A. This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.

12. If a class is located in a package, what do you need to change in the OS environment to be able to use it?

A. You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Let's say a class Employee belongs to a package com.xyz.hr; and is located in the file c:devcomxyzhrEmployee.java. In this case, you'd need to add c:dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows:

c:>java com.xyz.hr.Employee

13. What's the difference between J2SDK 1.5 and J2SDK 5.0?

A.There's no difference, Sun Microsystems just re-branded this version.

14. What would you use to compare two String variables - the operator == or the method equals()?

A. I'd use the method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object.

15. Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?

A. Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first.

16. Can an inner class declared inside of a method access local variables of this method?

A. It's possible if these variables are final.

17. What can go wrong if you replace && with & in the following code:

String a=null; if (a!=null && a.length()>10) {...}

A. A single ampersand here would lead to a NullPointerException.

18. What's the main difference between a Vector and an ArrayList

A. Java Vector class is internally synchronized and ArrayList is not.

19. When should the method invokeLater()be used?

A. This method is used to ensure that Swing components are updated through the event-dispatching thread.

20. How can a subclass call a method or a constructor defined in a superclass?

A. Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass's constructor.

21. What's the difference between a queue and a stack?

A. Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule
22. You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?

A. Sometimes. But your class may be a descendent of another class and in this case the interface is your only option.
23. What comes to mind when you hear about a young generation in Java?

A. Garbage collection.
24. What comes to mind when someone mentions a shallow copy in Java?

A. Object cloning.
25. If you're overriding the method equals() of an object, which other method you might also consider?

A. hashCode()
26. You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use:
ArrayList or LinkedList?

A. ArrayList
27. How would you make a copy of an entire Java object with its state?

A. Have this class implement Cloneable interface and call its method clone().
28. How can you minimize the need of garbage collection and make the memory use more effective?

A. Use object pooling and weak object references.
29. There are two classes: A and B. The class B need to inform a class A when some important event has happened. What Java technique would you use to implement it?

A. If these classes are threads I'd consider notify() or notifyAll(). For regular classes you can use the Observer interface.
30. What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?

A. You do not need to specify any access level, and Java will use a default package access level.

Wednesday, November 01, 2006

FREE: SCBCD 5.0 Beta Certification Exam

Are you a developer who is responsible for designing and implementing applications using Enterprise JavaBeans 3.0? If so, this is your opportunity to get involved in the creation of the Sun Certified Business Component Developer (SCBCD) 5.0 exam!!!!!
» Beta Dates: December 8, 2006 – January 2, 2007
» Registration Start Date: November 24, 2006

Details Here>>

Tuesday, October 31, 2006

Struts from Scratch

This article lays out the steps for installing Struts and a basic "Hello World!" sample application 'from scratch.' It assumes that you are brand new to Struts and that you're also fairly new to Java ServerPages (JSP) and programming in general.

The goal of this article is to go through the basics of installing and building a Struts application; the details of the application itself are not covered. The application is taken from my recent book, Struts Kick Start, and is covered in detail there. The application and build files described in this article can be downloaded from the book's companion Web site (http://www.strutskickstart.com).

Although some of the material in this article may seem very basic to an experienced Java developer, I've had a number of people express frustration to me that they were unable to locate a set of very basic instructions for installing Struts and getting a beginning application running. This is the problem addressed by this article.

Before we begin, it's worth stating that Struts does require some knowledge of Java and JSP to be used effectively. If you are brand new to Java Programming or JSP, it's recommended that you get some base knowledge of those technologies before diving too deeply into Struts. Spending a little extra time preparing yourself will make learning Struts a lot less frustrating for you.
Preparing your Computer to Install Struts

Before you install Struts, there are some configuration steps that need to be completed on your machine. This section leads you through those steps and gets you ready to install and begin working with Struts.

While this article assumes you have a computer running one of the Microsoft Windows operating systems, modifying the steps to work under Linux (or some other Unix variant) would require only minor changes.

Installing Java

Struts is based on Java, so running it requires you have Java installed. This article uses Sun's Java 2 Platform Standard Edition v1.4.1 (also known as JDK 1.4.1). The JDK 1.4.1 can be downloaded free from Sun's Web site at http://java.sun.com/j2se/1.4.1/index.html. Earlier versions of Sun's JDK can be used, but 1.4.1 is the most current version.

If you've done any programming in Java, it's likely that you already have this (or some other) version of Java installed. To see if you have Java installed already (or to see what version you have), from a command line, simply type "java -version". The output from this command run on my computer is as follows:

// Testing your Java Installation.

c:\dev>java -version
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition
(build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

If running this command doesn't show that you have a version of Java installed, you need to install a JDK on your computer before you can install Struts. Installing the JDK is like installing any other application on your machine. I'd recommend just following the defaults unless you have specific reasons not to. You can download the JDK from the link above.

Once Java is installed, you need to ensure that you have two environment settings configured. They are:

* Set JAVA_HOME to point to the top level directory where your JDK is installed. On my machine, I installed the JDK 1.4.1 in the directory C:\j2sdk1.4.1_01 so that's the value I would use as my JAVA_HOME.
* Add JAVA_HOME\bin to your path so that you can execute the java program itself. In my case this means adding the directory c:\j2sdk1.4.1_01\bin to my PATH variable.

Because each version of Windows (or Unix shell) provides a slightly different way of setting environment variables, you'll have to refer to other documentation to determine how to do this for your environment. After you've completed these steps, you should be able to open a new window, type the java -version command and see output similar to the example above.
Installing Ant So You Can Build Web Archive (WAR) Files

Once you've verified that you have Java installed correctly, the next thing you need do is install Ant. Ant is a program used to compile, build, and deploy Java programs. It, like Struts, is an open source program from the Apache Software Foundation. Ant is widely used by Java developers for all kinds of tasks related to building, testing, and deploying Java applications. Ant is available for free from the the Apache Ant Project's Web site at http://ant.apache.org/.

You need to have Ant installed because there is more to running Struts programs than simply compiling your java files. Struts applications are stored in what are called Web ARchive files (also called "WAR" files). Ant coordinates the process of taking your Java files, your JSP files, and all the other files that make up a Struts application and using them to build the WAR files that are needed to run the application.

WAR files are defined as part of the Java 2 Enterprise Edition (J2EE) standard. This is the same standard that defines the operation of Tomcat (and other servlet engines). Among the other Java technologies defined by the J2EE standard are Enterprise Java Beans (EJBs), the Java Message Service (JMS), and Java Mail. Using JSP and WAR files is a great way to get introduced to the J2EE standard.

Installing Ant is required to take advantage of the automated build scripts provided for the sample application. If you are currently a Java developer, you likely have Ant already installed. If you don't have it installed, this section provides the steps you need to follow to install it.

Similar to verifying your Java installation, you can verify your Ant install by typing the command ant -version. The results of running this command on my computer are shown below:

// Testing your Ant Installation.

c:\dev>ant -version
Apache Ant version 1.5.1 compiled on October 2 2002


To install Ant, simply download its binary files from http://ant.apache.org and unzip the downloaded file into a convenient directory. For myself, I installed Ant into a directory named C:\jakarta-ant-1.5, but then renamed the directory to C:\ant to keep things simple.

Similar to installing Java, you now need to change two environment settings for everything to work. They are:

* Set ANT_HOME to point to the top level directory where Ant is installed. On my machine, this value is C:\ant.
* Add ANT_HOME\bin to your path so that you can execute the Ant program itself. In my case, this means adding the directory C:\ant\bin to my PATH variable.

To verify your installation, you can type ant -version to verify that Ant will run. The output should be similar to what you see in the section above. Now Ant should be installed and configured correctly and we're ready to install the Apache Tomcat server.
Installing Tomcat

Apache Tomcat is referred to as a "Servlet Engine" or "Servlet Container." This means that it conforms to the standards defined for running applications based on Java Servlets and Java ServerPages. (In fact, Tomcat is the 'Reference Implementation' of these standards.) Because Struts is based on Java Servlet and Java ServerPages technologies, you need a servlet container for your Struts applications to run.

There are a great number of other servlet containers available including Weblogic, Websphere, Resin, and many more. We're using Tomcat because it is widely available and easy to find and install. If you need Tomcat, you can download a copy of it from the Apache Jakarta Project at http://jakarta.apache.org/tomcat.

There are a number of ways to install Tomcat. We will install using the Win32 installation file jakarta-tomcat-4.1.12.exe. This install uses an installshield-like installer from nullsoft. The only change to the default installation I'd recommend is overriding the installation location to some path that does not contain spaces (I used \dev\tomcat). I've seen situations where having embedded spaces in the path to the Tomcat files can cause problems during the Ant build process.

After you've completed the Tomcat installation, you should be able to start Tomcat and then open a browser on your machine and point it to the location http://localhost:8080/ to see the default Tomcat info page.
Installing Struts

Now that we have all the preliminary work completed, it's finally time to install Struts!

There are two sets of files that make up Struts: The Struts binaries (as they are referred to on the Struts site) and the Struts source files. This section only discusses the binaries because these are what you use to develop Struts applications. If you don't already have a copy of Struts, you can download it via the Jakarta Struts home page at http://jakarta.apache.org/struts. Get the binaries of the latest release of the 1.1 version of Struts.

From the perspective of someone developing applications, the Struts binaries are really just a collection of files. These files (.jar's, .tld's, .properties, and si forth) contain the Struts application files along with configuration information, error messages, and so on.

Installing Struts is really just copying all these files on to your computer in a way that makes it convenient for your application to access them. This is why there's no setup.exe installation file for Struts—all you do is unzip Struts into a directory and start working!

On my computer, I unzipped Struts into the C:\dev\jakarta-struts-1.1-rc1 directory and then renamed it to C:\dev\struts to make it easier to work with. This is all that's required to install Struts.

Compiling and Running the Hello World Example Application

One of the critical factors in being successful as a developer is making sure you have an efficient Edit/Compile/Test process. The goal of this section is to provide a simple and efficient way to get this process organized.

There are many options with regard to the 'editing' part; they range from text editors such as Notepad, vi, and emacs up through powerful IDEs such as JBuilder, IntelliJ, and Eclipse. (Personally, I use Eclipse from http://www.eclipse.org). This article doesn't cover editing options, so let's discuss the build and test parts of the equation in a bit more detail.

The value that Ant provides is that it greatly simplifies the build and test process. Ant uses an XML script to direct all the actions required for compiling the Java files, building the WAR file for you, and then deploying the WAR file into your servlet container. In the case of Tomcat, deploying the WAR file means just copying it into the TOMCAT_HOME\webapps directory (where TOMCAT_HOME represents the top-level directory for your Tomcat installation, for example C:\dev\tomcat).
Getting the Example Application

The example application is available at http://www.strutskickstart.com. It is the Hello World! application. To install it, simply download it and copy it into a convenient directory on your computer.

For this sample application, an Ant script is already written. This Ant script loads the properties it needs to work on a particular computer from a properties file. This properties file contains the directory paths it needs to locate the Tomcat installation on your machine. This file is named build.properties and is shown below:

// The build.properties file.

tomcat.home=c:/dev/tomcat
webapps.home=c:/dev/tomcat/webapps

To modify this file, simply edit it and replace these two values with the appropriate values for your computer.

MVC Diagrams


Struts 1.3.5 Released


The Apache Struts team is pleased to announce that Struts 1.3.5 has been promoted to General Availability.Struts 1.3.5 is available in a full distribution, or as separate library, source, example and documentation distributions.More from Apache>>

Thursday, October 26, 2006

Sun names likely license for open-source Java


Sun Microsystems likely will use the Community Development and Distribution License to govern the forthcoming open-source Java software project, CEO Jonathan Schwartz said Wednesday.More>>