Welcome!

From the Trenches of the Enterprise Software

Yakov Fain

Subscribe to Yakov Fain: eMailAlertsEmail Alerts
Get Yakov Fain via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Related Topics: Java EE Journal, Java Developer Magazine

J2EE Journal: Article

Taking a Sun Java Studio Creator for a Drive

Taking a Sun Java Studio Creator for a Drive

If you do not really enjoy the process of creation of Web applications with JavaServer Pages, try Sun Java Studio Creator (JSC), which at the time of this writing is available as an Early Access release. There are several general reviews of this product on the Internet, but I'll show you how in less than an hour you can create a Web application with a logon page that performs database user authentication, and displays the main application page for a valid user. If this does not impress you, I can add that I had to write not more than 20 lines of code for this application - the rest was drag and drop.

First, download and install an Early Access release of JSC from wwws.sun.com/software/products/jscreator/ . Installation process is simple, and the product comes with Sun Application Server and PointBase DBMS.

Getting Familiar with IDE
After installation is done, start JSC and create a new project called TestLogon by selecting menus File | New Project. In several seconds, you'll see the windows that looks like this:

 

The big white rectangle in the middle is a page editing area, and we'll be dragging and dropping JSP Standard components from Pallete to the editing area. Right-click in the middle, select Properties and you'll see the Properties sheet in the top right part of the screen. The tabs at the bottom of the editing area allow you to switch between design and JSP source code panels.

The Project Navigator sheet on the right will let you select generated Java code for compilation, editing and debugging. There you can also specify page navigation for your application and add additional jar files, if needed.

Our first Page1.jsp will consist of three output text components (labels), two text fields and a Submit button.

Drag and drop the output text component on the white area, snap it to the proper location on the underlying grid, and resize it by stretching the rectangle. According to the Properties sheet, JSC has assigned the id outputText1 to this component. Right click on the component and type Enter Name in the Value filed. Place another output text component under the first one, and set its value to Enter Password.

Drag the text field component for the user name and drop it by the first output text. Look at the Properties sheet - its id is textField1. Place a secret field component under the text filed - its id is secretField1.

Now drop the button on the screen - it already has a default value Submit.

Let's add some color to the page - click anywhere in the white area and pick up a nice background color from the Properties sheet. Change the title of the page to be My Logon Page.

After all these manipulations my screen looks as follows:

 

If you want to see what the generated code looks like, just click on the tab Source at the bottom of the page. This is what I've got:


<?xml version="1.0"?>
<jsp:root version="1.2" xmlns:f="http://java.sun.com/jsf/core"
 xmlns:h="http://java.sun.com/jsf/html" xmlns:jsp="http://java.sun.com/JSP/Page">
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <html>
      <head>
       <title>Page1 Title</title>
      </head>
      <body bgcolor="#00ffff" rave-layout="grid">
        <h:form binding="#{Page1.form1}" id="form1">
         <h:outputText binding="#{Page1.outputText1}" id="outputText1" 
           style="position: absolute; left: 24px; top: 24px; width: 
                             120px; height: 26px" value="Enter Name"/>
         <h:outputText binding="#{Page1.outputText2}" id="outputText2" 
           style="position: absolute; left: 24px; top: 72px; width: 
                         120px; height: 26px" value="Enter Password"/>
         <h:inputText binding="#{Page1.textField1}" id="textField1" 
           style="position: absolute; left: 168px; top: 24px; width: 
                                               120px; height: 23px"/>
         <h:commandButton action="#{Page1.button1_action}" 
            binding="#{Page1.button1}" id="button1" style="position: 
                    absolute; left: 24px; top: 120px" value="Submit"/>
         <h:outputText binding="#{Page1.outputText3}" id="outputText3" 
            style="position: absolute; left: 168px; top: 120px; width: 
                                                120px; height: 26px"/>
         <h:inputSecret binding="#{Page1.secretField1}" 
            id="secretField1" style="position: absolute; left: 168px; 
                               top: 72px; width: 120px; height: 23px"/>
        </h:form>
       </body>
      </html>
    </f:view>
</jsp:root>

Now we can build and deploy the project using provided Sun Application Server. Just press the big green arrow on the top toolbar, Ant will build and deploy the project, and JSC will start the application server and display our logon page:

 

Let's drop one more output text field outputText3 by the Submit button - we'll use it to see that the server responds when we submit the data.

JavaServer Faces uses event-driven interaction between a JSP and the server-side classes. For example, when a user clicks on the button Submit, appropriate listener intercepts this event and executes the code that you provide.

Double-click on the button Submit, and JSC will open the corresponding method in the underlying Java class. In our case it's a method button1_action() in the class Page1. Just type in the following two lines shown in blue to get the entered value, append it to the word Validating, and send the whole phrase back to the browser as the value of the field outputText3.


public String button1_action() {
    // Add your event code here...
    String name = (String) this.textField1.getValue();
    outputText3.setValue("Validating "+ name);
    return null;
}

Save this code and re-deploy the application by clicking on the green arrow. Enter any name and press the button Submit. Now the browser sends a request to your server, JSF calls the method button1_action(), and you'll see the response:

 

The next goal is to to validate the entered name against a database table.

Working With a Database
Java Studio Creator includes a PointBase DBMS with a sample database Travel. You have to start the PointBase server by selecting system menus Start | Programs | Sun Microsystems | J2EE 1.4 SDK | Start PointBase. This server starts quickly and displays a message in a command window that it's listening to the port 9092. Minimize this command window and return to JSC.

In the Server Navigator sheet, under the Data Sources expand the database Travel, which has the following data in the table Person:

PersonID Name Jobtitle FrequentFlyer
1 McNealy, Scott CEO true
2 Schwartz, Jonathan VPO/CXO - SGMS true
3 Green, Rich VP true
4 Joy, William VP/CXO - SGMS false
5 Gosling, James VP true

Every time when the user hits the button Submit on the logon page, we'll try to find the entered name in the list of names retrieved from the database table Person. In our simple application the password is password for any user.

The next step is to link our page with the data - drag and drop the table Person onto Page1.jsp. At this point JSF creates two new components - PersonRowSet, which is a JDBC RowSet and a PersonModel (remember similar implementation of the MVC pattern in some Swing components?) . You can see these names in the Document Outline sheet. Check out the constructor of the class Page1 - JSC has generated the following code:


public Page1() {
   // Creator-managed initialization code
   try {
     personRowSet.setDataSourceName("java:comp/env/jdbc/Travel");
     personRowSet.setCommand("SELECT * FROM ROOT.PERSON");
     personModel.setWrappedData(personRowSet);
   }
   catch ( Exception e) {
     log("Page1 Initialization Failure", e);
     throw new FacesException(e);
   }
   // User provided initialization code
}

Add the following line to this try/catch block to populate the personRowSet:


personRowSet.execute(); 

Now we should add the code that compares the entered name with names retrieved from the table Person. Modify the method button1_action() in Page1.java to process the RowSet. If the names match and the user has entered the word password , we'll assume that it's a valid user:


    public String button1_action() {
        // Add your event code here...
        String name = (String) this.textField1.getValue();
        String pwd = (String) this.secretField1.getValue();
        outputText3.setValue("Validating "+ name);
        boolean isValidUser=false;
        
        try{
            personRowSet.beforeFirst();

            while(personRowSet.next()){
             String personName = personRowSet.getString("NAME");

             if (name.equals(personName) && "password".equals(pwd)){
                outputText3.setValue( name + " is a valid user");
                isValidUser = true;
                break;
             }
            }
            if (!isValidUser){
               outputText3.setValue( name + ": logon failed");
            }
        } catch ( Exception e) {
            log("Can not access personRowSet: ", e);
            throw new FacesException(e);
        }

        return null;
    }
Here's what I've got after entering an valid name and password:

 

To test this application against another DBMS right click on the Data Sources and select Add Data Source option to configure a data source for any other DBMS. It'll work as long as you have a JDBC 3.0 driver for this DBMS.

Page Navigation
To make our logon example more realistic, let's introduce another page -the main page of some application that has to be displayed for valid users.

JSC has a cool Page Navigation tool, where you can define navigation rules for your application. Right click on our Page1.jsp and select Page Navigation from the menu. You'll see the following screen:

 

Now, right click on the Page1.jsp icon, select New Page and enter its name as WelcomePage. Click on the Page1.jsp icon again, drag the line to the WelcomePage icon and change the name of the line to validUser:

 

Click on the Source tab to see the content of the file Navigation.xml that JSC has generated:


<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces
 Config 1.0//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
    <navigation-rule>
        <from-view-id>/Page1.jsp</from-view-id>
        <navigation-case>
            <to-view-id>/WelcomePage.jsp</to-view-id>
            <from-outcome>validUser</from-outcome>
        </navigation-case>
    </navigation-rule>
</faces-config>

This is how JavaServer Faces maps the outcome of the current page to the JSP that has to be displayed next.

Double click on the WelcomePage icon and add an output text component there with the value Welcome to My Application.

Finally, let's modify a little the code of the method button1_action() to return the value "validUser" in case of successful logon. This is how my while loop looks now:


           public String button1_action() {
        String name = (String) this.textField1.getValue();
        String pwd = (String) this.secretField1.getValue();
        try{
            personRowSet.beforeFirst();

            while(personRowSet.next()){
             String personName = personRowSet.getString("NAME");

             if (name.equals(personName) && "password".equals(pwd)){
                outputText3.setValue( name + " is a valid user");
                return "validUser";
             }
            }

            outputText3.setValue( name + ": logon failed");

        } catch ( Exception e) {
            log("Can not access personRowSet: ", e);
            throw new FacesException(e);
        }

        return null;
    }
}

Run the application again, and if you'll enter the valid name now, you'll see the next page:

 

During development of this application I've kept notes of what I did not like (do not forget - this is an early access release):

  • The name Java Studio Creator does not make any sense to me, since the tool is actually creating Web applications .
  • JSC starts slowly on my 1Ghz/1GB RAM/Windows 2000 notebook.
  • Undo option does not always work
  • Sometimes components were not shown in the designer, while the generated code was in place. And this is a showstopper - how many times are you willing to re-create the page from scratch?
  • After renaming the package, my Java code has disappeared.
  • Copy/paste is not fully functional yet.
  • Some modifications in the GUI designer do not sync up with Java code in the beans.
But overall, this test drive was a pleasant experience for me. After trying Java Studio Creator I want to learn JavaServer Faces technology! As a matter of fact I've already written my first logon application using JSF just now.

I was testing JSC while riding on a bus to work. A guy who was sitting next to me and watching what I was doing said: "It's like Visual Basic!". Well, it's not there yet. The Early Access edition of JSC is more like a concept car - you can get in, turn it on and touch the shiny controls. But when Sun will put it on the road, I'm sure that many Java developers who are currently driving on the server-side roads will buy it. But Sun Microsystems should move fast, because IBM has already announced the release of the WebSphere Studio V5.1.2, which will also include drag and drop programming with JavaServer Faces.

More Stories By Yakov Fain

Yakov Fain is a Java Champion and a co-founder of the IT consultancy Farata Systems and the product company SuranceBay. He wrote a thousand blogs (http://yakovfain.com) and several books about software development. Yakov authored and co-authored such books as "Angular 2 Development with TypeScript", "Java 24-Hour Trainer", and "Enterprise Web Development". His Twitter tag is @yfain

Comments (7) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Alexandre Barão 06/18/04 08:43:24 AM EDT

I do not really enjoy the process of creation of Web applications with JavaServer Pages but this article
shows the easy way to create a Web application... Thanks!
Alverca, Fado - Alemão! :)

jim caprioli 06/18/04 02:59:09 AM EDT

Some thoughts I had while reading your entertaining article.

- Concept cars give you this "if I only had..." feeling.
- I was very disappointing with this stuff.
- NetBeans does the job for me.
- So the guy ''in the bus'' is the VB user hey?
- Sun is a hardware company: ver good in compilers and Java. - I always end up at the site of JCreator when looking for info about this product.
- Worst name ever: "I just can''t remember it!"

GrtZ
jim

Yakov Fain 06/15/04 11:15:59 AM EDT

The article reads: "Add the following line to this try/catch block to populate the personRowSet:
personRowSet.execute();"

This means that you should put this line inside the try/catch block.

Regards,
Yakov Fain

Carol Cassles 06/15/04 11:10:08 AM EDT

Yakov Fain:

I followed the steps in your article very carefully. I got to the point where one adds the code that compares the entered name with names retrieved from the table Person. When I try to compile the source code I get a compiler error.

The error looks like this:

Compiling 1 source file to H:\My Documents\Creator\Projects\LOGONAUTH2\build\WEB-INF\classes
untitled\Page1.java [123] unreported exception java.sql.SQLException; must be caught or declared to be thrown
personRowSet.execute();
^
Are all of us newbees asking you how to "catch or declare" this command or is it just me?

Carol Cassles
casslece@pgcc.edu

Yakov Fain 05/25/04 02:28:03 PM EDT

Well, my laptop is not that bad - it has has 1Gb of RAM and 1Ghz processor :)

Winston Rast 05/25/04 02:07:49 PM EDT

I''m curious how you managed to boot Win2k in 1MB. :)

John Rubier 05/21/04 03:56:44 PM EDT

It runs slow?
I''m surprised much of anything runs at all on your "1Mhz/1MB RAM/Windows 2000 notebook"!

;-)