Return to: The
Learning Curve Journals»
While I'm trying to develop my first Java application using the Java Studio Creator IDE, I realize that I have to become somewhat more fluent in the Java programming language. Despite the IDE's visual design environment, I really do need to get a foundation in its underlying technologies and understand some basic terminology. The Sun Developer Network (SDN) articles and tutorials are a great resource for that. As I browse the articles, I get some insight into the wide spectrum of creative possibilities, but also realize I needed a clearer road map for navigating to my first destination—creating my first application. So now I'm diving into a series of Java technology reference books and SDN articles to get a firsthand understanding of what makes Java technology tick. The more I learn about Java code, the more excited I get. I was brought up on procedure-oriented programming languages like BASIC, Fortran, and C, but Java is a true object-oriented programming language. It does require that I think differently, but it is refreshing to program in a way that more closely represents my real world, a world composed of objects. Good news though: I found Java easier to digest, on the outside anyway, than the menu board at my local Starbucks! The thing that impresses me about the Java Studio Creator IDE is that it doesn't try to hide the Java underlying technologies from me. So I get to learn and understand JavaServer Faces components, JavaBeans technology, classes, objects, converters, validators, and more. And a key reason for this is that the IDE is built on an industry standard: JavaServer Faces technology. IntroductionWhat follows is a debriefing on this technology and other Java essentials I'm learning about: JavaServer Faces, JavaBeans, JavaServer Pages (JSP), and the heart of Java programming—classes, methods, and objects. (By the way, JDBC Rowsets is another important technology worth mentioning, but that is a topic for a future journal on database creation and management.) Contents
JavaServer Faces Technology
I was surprised to learn that central to the Java Studio Creator IDE's structure and foundation—the various windows and the drag-and-drop placement of objects, for example—is the JavaServer Faces framework. This is exciting news because it means that as I develop Java applications, not only is my run-time executable portable, but my source code is portable across development tools as well. For example, I can share code with developers using Borland JBuilder, which is also built on the JavaServer Faces framework, as are many other development platforms, including Sun Java Studio Enterprise. The JavaServer Faces technology defines the framework of a web application; the Java Studio Creator IDE provides a visual representation of this framework. Take a look at the Project Navigator window shown in Figure 1.
The Project Navigator provides an overview of my entire Java project. It is the structure that holds everything together; all project files are contained within the tree. Since JavaServer Faces defines this structure, it allows for a consistent structure between applications and even across development environments. I can even export my whole project as a compressed WAR (Web ARchive) file. All files that make up my project and their relationship to each other will remain intact whether I bring it into another development environment or decompress it and work with files by hand coding. But JavaServer Faces does more than define my application structure, it also provides a set of about 30 prefabricated components I can use to build my applications, as shown in Figure 2. In addition, I can easily import third-party components as they become available. I design my project by dragging and dropping the JavaServer Faces (shortened to JSF on the screen) components onto the design canvas.
JavaServer Faces validators and converters are also easily accessible from the Palette. I drag a text box onto my design page, then drag both a converter and a validator onto the text box. The converter lets me specify what kind of data the text box would accept. Because I want my end user to enter a numerical value in the text box, I drop the Double Converter onto it. JavaServer Faces takes care of generating the Java code that works with my data in the correct Double type. Converters make sure that what my end user enters is actually a number, and the right type of number. For example, if a field lets users enter an integer order quantity, the number 1.5 will not be accepted. Java technology is very stringent about typing data—that is, I can't get away with using data as a String type at one time and then as an Integer type later. I can see why: the Java language wouldn't be as robust, and could crash across platforms, as there is some ambiguity involved in working with data. Converters make sure that Java knows how to process data correctly. As I continue to define my text box, I drag a validator onto it to specify what range or format of data the user would be allowed to enter into the box at runtime. If the user's input is outside my specified range or format, the validator will return an error message asking the user to fix the problem. I can customize this error message, or use the default message. To try out the JavaServer Faces components in the IDE, I'll open a project in design view and take a look at the Palette window (its default location is on the upper left side). A set of Palette categories appear: one set for the JSF Components (organized into three categories: basic, layout and composite), and additional categories for Validators and Converters. There are also categories for data providers. JavaBeans Technology
As with JavaServer Faces, I've seen a lot of talk about JavaBeans technology in articles, forums, and tutorials on the SDN. It is a very rich topic. What I have learned is that the JavaBeans technology defines a set of rules and requires that my Java Page Bean, Session Bean, and Application Bean objects meet these rules and thus are all JavaBeans. In this way, my Java code can be understood and manipulated in the visual environment. As I create blocks of code in the Java Studio Creator IDE, it creates JavaBeans components for me. To demonstrate this, I built a very simple application by following one of the tutorials on the Sun Developer Network (SDN) site for Java Studio Creator: Developing a Web Application. This tutorial led me through the process of building a form that accepted my input and then displayed it after I clicked a Submit button. It is interesting to look behind the scenes here at the two Java pages that the IDE produces. Figure 3 shows the Page Bean (Page1.java) and the JavaServer Page (Page1.jsp) that made my application work. They are both easily accessible by clicking on the tabs (JSP and Java) up top. I can also click on the Design tab to toggle between design and source views for my JavaServer Page.
I'll talk about the JavaServer Page next—that's the page I get to by clicking on the JSP tab—but for now I'll look at the JavaBean, or page returned from the Java tab. I'll call this a Page Bean because its scope is limited to the one page I am on, but it conforms to the JavaBeans standard. My Page Bean contains a lot more code than just the simple code that takes care of returning my text data. In fact, it pulls in a whole bunch of libraries and provides a number of additional classes and methods (as I'll discuss below). A larger application normally contains a number of Page Beans, and might also include a Session Bean and an Application Bean so that classes, methods, and data can be available to all the pages across a web browser's user session or across my entire application. JavaServer Pages (JSP)
I mentioned above that the Java Studio Creator IDE also produced a JavaServer Page (JSP) in my application. This page looks a lot like HTML. It provides the visual formatting of my page and includes all HTML structures needed, such as <body> and <head> statements. But it also includes information specific to JavaServer Faces. My text box, for instance, isn't defined by the usual <input> statement, but by <h:inputText> Enclosed by the carets are parameters that attach it to the correct class in my Page Bean and that also assign its converter and validator. This HTML-looking page with special tags on the inside is a JavaServer Page, or JSP. Later, I'll see that it is saved in my project folder with the .jsp extension to differentiate it from the Page Bean, which has a .java extension. The application server parses and executes this page along with my Page Bean to output the pure HTML that a browser can understand. I enjoy watching the JSP take form as I design my application. The Java Studio Creator IDE lets me toggle between the visual design and the JSP code using the Design and JSP tabs at the top of my design canvas. It is also fun to work directly in the JSP window as I can insert complex HTML formatting (like tables) and then see them take form in the design view. The Heart of Java Programming: Classes, Methods, and Objects
The Java Studio Creator IDE automatically handles much of the complexity of the Java programming language. Even so, it didn't take me long to look behind the scenes and start programming in Java. Getting an understanding of the basics terms of the programming language made designing applications in the IDE simpler for me. Since Java is an object-oriented programming language, data and functions are combined—not separated—into units called objects. Each instance of an object is created from a template, which is called a class. A class is the building block of a Java application. For example, let's say I want to model a light bulb in software with a switch that turns a set of two differently colored lights on and off. I'm going to need two objects: a red light bulb and a blue light bulb. But first, I'll need a light bulb template, or class. With this class, I can more easily produce my objects since each object is so similar in scope. As I design the class, I'm going to need some function (or behavior); that is, a way to turn the light bulb on and off. So I'll create an on routine and an off routine and call these methods; the methods reside within the class. The light bulb class includes lines of code that defines my light bulb with a certain brightness, color, and look, while also defining how the light bulb is allowed to behave. What is nice is that I don't need to know a lot about this light bulb class, just as I don't need to know about the inner workings of the light bulb on my desk to use it. I might even wish to import a light bulb class from a third-party supplier. I create a light bulb object by calling its class. But then I need to interact with the light bulb, and I do so by calling its methods. For example, to turn the light bulb on or off, I call its OnOff() method to do the work of interacting with the light bulb class for me. Java makes it easy for me to produce two versions (objects) of this light bulb in different colors. First I will create an object from a sub-class that inherits the light bulb properties, but defines a new color, red. The generic light bulb class itself then becomes a superclass of its red sub-class counterpart. Here's how it all looks in the code. ClassesMy light bulb class is not in the IDE's API, so I'll find one from a third party and import it (or I'll design one myself). If I were to look at the code, it would contain this: Code Sample 1
Class LightBulb {
}
All the code that defines this light bulb sites between the brackets, which can include variables, initialization code and even other classes. But also within the brackets is a set of methods. MethodsMethods provide the code that gives the class particular forms of behavior. The following LightBulb class includes a method to turn it on: Code Sample 2
Class LightBulb {
// Variable definitions here...
float brightness = 100;
float color;
// Methods here...
void TurnOn() {
// User event code here...
}
}
Subclasses
I need a red version of this LightBulb. I'll create a subclass as follows: Code Sample 3
Class LightBulbRed extends LightBulb{
}
All that I need to code within the brackets is a redefinition of the property that defined the LightBulb color, as all else is inherited from the LightBulb "superclass." ObjectsTo then create my two light bulb objects, I'll use the "new" operator: Code Sample 4
LightBulb p = new LightBulb();
LightBulb q = new LightBulbRed();
p and q are now a reference of type LightBulb that I can call later. With the LightBulb objects now created, I can turn them on through their method. Code Sample 5
p.TurnOn();
q.TurnOn();
Conclusion
Not too difficult! It can be fun to program in such an object-oriented world. So now that I've got an overview of the Java language and its core terminology (and virtual lightbulbs working on my desk), I'll start developing a real-world applications with the Java Studio Creator IDE my next entries. More Developer Resources
For more tech tips, articles, and expert advice for developers, visit the Java Studio Creator developer resources on the Sun Developer Network (SDN) at /jscreator/. |
| ||||||||||||||
|
| ||||||||||||