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 |
|
Installation
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 |
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:
<code> 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) ... ... ... </code>
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:- 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.
Figure 2. New Web Project - For Project Name enter stocktraderserver (see Figure 3). For a location, I chose
C:\dev\Devx\EclipseRcpSpringRemoting\StockTraderServer
. Click Finish.Figure 3. New Project Name
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:
- 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).
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:- 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.
Figure 5. Add Spring Capabilities - 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).
Figure 6. Spring Has Been Added - 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:
<code> <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>springDispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springDispatcher</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping> </web-app> </code>
- 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
tags in your web.xml, plus be appended with servlet.xml. As a result, because your servlet name/text between the
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:
<code> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> </beans> </code>
- 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 - 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).