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
A J2EE-compliant Web server (Tomcat, Resin, Jetty, etc.)
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)
Before beginning, take the following steps to make sure you are using the JDK and not just the JRE:
- Go to Window -> Preferences, expand the Java node, and click Installed JRE.
- Click Add, and click the Browse button for the JRE Home Directory. Go to your JAVA_HOME directory, and click OK.
- 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.
|Figure 1. Verify You're Using the JDK, Not the JRE|
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
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
... ... ...
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:
- Click File -> New -> Project. Choose Java Project and click Next.
- A New Project Wizard will appear. Under MyEclipse -> J2EE Projects, choose Web Project (see Figure 2) and click Next.
- For Project Name enter stocktraderserver (see Figure 3). For a location, I chose
C:\dev\Devx\EclipseRcpSpringRemoting\StockTraderServer. Click Finish.
Add Third-Party Libraries
Now add some third-party libraries:
- In your unzipped spring-framework-1.2.8 distribution, you'll find the following jars:
Copy them all to the WEB-INF/lib directory.
- 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
- Right click on your project StockTradeServer in the package explorer of Eclipse.
- Select properties, and click Java Build Path.
- Click the Libraries tab.
- 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).
Add Spring to the StockTraderServer
Now, incorporate the Spring Framework into your project. The easiest way is to use the MyEclipse plugin:
- In the Eclipse menu, Click MyEclipse -> Add Spring Capabilities....
- 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.
- 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.
- MyEclipse displays an S symbol in the Package Explorer to inform you that Spring has been added (see Figure 6).
- 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"?>
- 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).
|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">
- 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).
|Figure 8. springDispatcher-servlet.xml Has Been Added to MyEclipse-Spring|