RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Build a Java App Server Foundation for Thick-Client Deployment

Web 2.0, rich Internet applications, and heavy JavaScript have become the latest rage. But why build a Web application that tries to look and act like a thick client when you can leverage a traditional J2EE/Web application server architecture to easily deploy an actual thick client?

o one doubts the impact the Internet and its partner in crime the Web browser have had on the world. If you're like me, you happily check the news, the latest sport scores, or the value of your portfolio after a bad day on the NASDAQ through a Web browser. But Web applications have limitations due to their reliance on HTTP. As you would expect, the Web application world recognizes the weaknesses of the HTTP Web client and is trying to improve upon it with "Web 2.0" applications, Rich Internet Applications (RIA), AJAX, Lazlo, Flex, etc.

All of these technologies are attempts to bring a thick-client feel to thin Web clients. But in my assessment, chopping and painting a Volkswagen to make it feel like a Porsche will never make it drive like a Porsche. I'm not trying to start a Web client versus desktop application war; I think each has its purpose. But after spending lots of time developing with Dojo, script.aculo.us, AJAX, Prototype, etc., I feel that much of the Web 2.0 stuff is just a hack to circumvent HTTP's limiting request/response paradigm and ultimately to push HTTP to do more than it was intended to do.

I wouldn't be surprised at all if one day a feature-rich Web 2.0 application's download size approaches that of a well-developed desktop application deployed via Java Web Start. With that in mind, I propose building a thick client for your end users instead, while leveraging a traditional J2EE/Web application server architecture. This tutorial will get you started by walking through the creation and deployment of a thick-client stock trader application.

What You Need
  • Eclipse 3.1.2
  • A J2EE-compliant Web server (Tomcat, Resin, Jetty, etc.)
  • MyEclipse 4.1.1
  • Java 5 (and working knowledge thereof)
  • A servlet container or J2EE server (This tutorial uses Tomcat 5.5+)
  • Spring 1.2+ (and working knowledge thereof)
  • Installation

    Before beginning, take the following steps to make sure you are using the JDK and not just the JRE:
    1. Go to Window -> Preferences, expand the Java node, and click Installed JRE.
    2. Click Add, and click the Browse button for the JRE Home Directory. Go to your JAVA_HOME directory, and click OK.
    3. Type in JDK under JRE Name, and click OK. Make sure that JDK is checked and click OK (see Figure 1) to exit Windows -> Preferences.
    4. Click to enlarge

      Figure 1. Verify You're Using the JDK, Not the JRE

    Install Tomcat
    This tutorial uses Tomcat 5.5.17, which was the latest release at the time of this writing. Note that because it also uses RMI within Tomcat, you must install Tomcat in a nonwhite space directory. That is, C:\Program Files\Apache Software Foundation will not work and an RMI Server won't run on Tomcat unless Tomcat is installed with no spaces (Click here to read the actual ASF Bugzilla bug. Apache has closed the ticket and is not going to fix it.). I installed Tomcat at C:\devtools\java\apache-tomcat-5.5.17.

    If you should see an exception like the following when you run any portion of this tutorial, you can either remove/not invoke the RMI portions, or change Tomcat's location to one without whitespace:

    	java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
    			java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
    			java.net.MalformedURLException: no protocol: Files/Apache
    	... ... ...
    	Caused by: java.net.MalformedURLException: no protocol: Files/Apache
    			at java.net.URL.(URL.java:567)
    	... ... ...

    Get Spring
    If you don't have it installed already, download the Spring Framework. I chose 1.2.8, which was the latest stable release at the time of this writing. I would recommend downloading the Spring Framework with dependencies, as it has a few third-party libraries you will need. Also download the source and javadoc as a resource. Once you're finished, unzip the download.

    Okay! Now you are all set up and ready to rock and roll.

    Creating the Web Application/Server-Side Project

    First, create a Java Project in Eclipse:
    1. Click File -> New -> Project. Choose Java Project and click Next.
    2. A New Project Wizard will appear. Under MyEclipse -> J2EE Projects, choose Web Project (see Figure 2) and click Next.

      Click to enlarge

      Figure 2. New Web Project

    3. For Project Name enter stocktraderserver (see Figure 3). For a location, I chose C:\dev\Devx\EclipseRcpSpringRemoting\StockTraderServer. Click Finish.

      Click to enlarge

      Figure 3. New Project Name
    Add Third-Party Libraries
    Now add some third-party libraries:
    1. In your unzipped spring-framework-1.2.8 distribution, you'll find the following jars:
      • Table 1 – third-party External jars to add to your classpath
      • spring.jar – located under the folder dist
      • commons-logging.jar – located under folder lib\jakarta-commons
      • log4j-1.2.13.jar – located under folder lib\log4j
      Copy them all to the WEB-INF/lib directory.
    2. Right click on your project StockTradeServer in the package explorer of Eclipse.
    3. Select properties, and click Java Build Path.
    4. Click the Libraries tab.
    5. Click Add JARs... and add the jars you just copied. Also, click Add External JARS..., go to your spring dependencies 1.2.8 unzipped folder, and add junit.jar, which is located in the lib/junit directory (see Figure 4).

      Click to enlarge

      Figure 4. Java Build Path

    Add Spring to the StockTraderServer
    Now, incorporate the Spring Framework into your project. The easiest way is to use the MyEclipse plugin:
    1. In the Eclipse menu, Click MyEclipse -> Add Spring Capabilities....
    2. When the wizard comes up, uncheck both MyEclipse Libraries and Spring core and click Next (see Figure 5).

      Note: You uncheck MyEclipse Libraries because you have already added spring.jar to your classpath. Alternatively, you can add your spring jar(s) using the MyEclipse option. I decided not to because by default MyEclipse adds a number of other jars that aren't necessary. If you'd like to remove these extra jars from the default MyEclipse settings, go to Window -> Preferences -> MyEclipse -> Project Capabilities -> Spring.

      Click to enlarge

      Figure 5. Add Spring Capabilities
    3. Create a new Spring bean configuration file, and create the applicationContext.xml in the WEB-INF directory using the browse button. When done, click Finish.
    4. MyEclipse displays an S symbol in the Package Explorer to inform you that Spring has been added (see Figure 6).

      Click to enlarge

      Figure 6. Spring Has Been Added
    5. Unfortunately, MyEclipse did not add Spring's DispatcherServlet to your Web Project. Do this now:

      Open web.xml, which you can find in your Package Explorer by expanding to WebRoot -> WEB-INF. Now, add the following to your web.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app version="2.4" 
    6. Now you must create the WebApplicationContext for Spring. Spring provides a clearly defined rule for doing this: you create a new .xml file under the WEB-INF directory. This new file must take the name of the text you placed between the <servlet-name> tags in your web.xml, plus be appended with –servlet.xml.

      As a result, because your servlet name/text between the <servlet-name> tags is springDispatcher, add a new file called springDispatcher-servlet.xml under the WEB-INF directory (see Figure 7).

      Click to enlarge

      Figure 7. springDispatcher-servlet.xml Has Been Added to the Project

      Now open springDispatcher-servlet.xml and enter the following, which is the base XML template for a Spring Application Context:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    7. Now you must add the springDispatcher-servlet.xml to the MyEclipse plugin. Right click on the stocktradeserver project node in Package Explorer. On the left-hand side, select MyEclipse-Spring. Click Add..., navigate to and select springDispatcher-servlet.xml, and click OK. Press OK again to close the Properties window (see Figure 8).

      Click to enlarge

      Figure 8. springDispatcher-servlet.xml Has Been Added to MyEclipse-Spring

    Close Icon
    Thanks for your registration, follow us on our social networks to keep up-to-date