A site devoted mostly to everything related to Information Technology under the sun - among other things.

Saturday, December 8, 2007

A Web Application Using Only Java

There has always been an issue in J2EE Web development in that there are more than one way of skinning the cat when it comes to developing a Web application. One could do some, either, or all of the following in the same application:

• put the code right in the page.

• put the code in plain-text page libraries that are included in the main pages.

• use tag libraries.

• write the code inside a Servlet.

• use JSTL

One can instead use Google Web Toolkit (GWT) which is a Java to JavaScript compiler. You write code in Java, and GWT compiles it into JavaScript that works in a standard browser. One of Google’s biggest issues seems to have been to create a common way of writing code that would be browser agnostic. They’re not there yet, but I like to think that write-once-run-anywhere has been moved a couple of steps forward.

Consider coding the follwoing page:



Here is the code to build it using GWT - All Java, All of the Time!


signupButton = new Button();
flexTable.setWidget(6, 1, signupButton);
signupButton.setTabIndex(7);
signupButton.setText("Signup");



signupButton.addClickListener(new ClickListener()
{



public void onClick(Widget sender)
{
if (signupButton.isEnabled())
{
if (validating)
validateSignup();
else
beginSignup();
}
}
});

And

SignupRemoteService.Util.getInstance().signup(
signupInfo, new AsyncCallback()

{

public void onFailure(

Throwable caught)

{

showErrorMessage("There seems to be a problem" +

" with the data you entered." +

" Please check the information" +

" and try again.");

signupButton.setEnabled(true);

}

public void onSuccess(

Object result)

{

String errorMessage =

(String) result;

if (errorMessage != null)

{

showErrorMessage(errorMessage);

signupButton.setEnabled(true);

}

else

{

flexTable.getRowFormatter().setVisible(

5, true);

showInfoMessage("Almost there." +

" Please check your email" +

" and copy and past the " +

"Authentication Code into" +

" the box above.");

validating = true;

signupButton.setEnabled(true);

cancelButton.setEnabled(true);

}

}

});

Google supplies an emulator that allows you to run the code, set breakpoints and even do on-the-fly changes.

In addition to debugging, unit testing works like any other Java application. Since all of the code is standard Java, you can simply write unit tests against any of the code and run it using the standard Java Runtime Environment. If you need the ability for your unit tests to run against the hosted code, Google made that possible using the emulator and a special test case class from GWT.

GWT works with Eclipse. It ships with a tool to create an Eclipse GWT project and for less than $50 you can get GWT Designer, which among other things allows you to create Eclipse code using a WYSIWYG designer.

For server side code GWT ships with two solutions. You can either use standard JavaScript Notation Object (JSON) or use GWT Remote Procedure Calls (RPC), which works with Servlets. This means that you end up with two code bases--a client code base and a server code base. The client code base becomes JavaScript and the server code base is deployed as a Servlet. In practice, you simply wrap it all up in a Java Web Archive (WAR) and deploy it on Tomcat or another standard Servlet container.

Since the GWT server side is just standard Java deployed on a Servlet container, you can use all your favorite Java libraries like Hibernate with Annotations, Spring, Log4J, Jackcess (to import access files) and so many other mature Java libraries on the server side.

No comments:

About Me

My photo
I am a senior software developer working for General Motors Corporation.. I am interested in intelligent computing and scientific computing. I am passionate about computers as enablers for human imagination. The contents of this site are not in any way, shape, or form endorsed, approved, or otherwise authorized by HP, its subsidiaries, or its officers and shareholders.

Blog Archive