Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Build a Java App Server Foundation for Thick-Client Deployment


advertisement
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:

    <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:
    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:

      <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>
    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 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).

      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:

      <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>

    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

    Related Reading


    More Insights






    Currently we allow the following HTML tags in comments:

    Single tags

    These tags can be used alone and don't need an ending tag.

    <br> Defines a single line break

    <hr> Defines a horizontal line

    Matching tags

    These require an ending tag - e.g. <i>italic text</i>

    <a> Defines an anchor

    <b> Defines bold text

    <big> Defines big text

    <blockquote> Defines a long quotation

    <caption> Defines a table caption

    <cite> Defines a citation

    <code> Defines computer code text

    <em> Defines emphasized text

    <fieldset> Defines a border around elements in a form

    <h1> This is heading 1

    <h2> This is heading 2

    <h3> This is heading 3

    <h4> This is heading 4

    <h5> This is heading 5

    <h6> This is heading 6

    <i> Defines italic text

    <p> Defines a paragraph

    <pre> Defines preformatted text

    <q> Defines a short quotation

    <samp> Defines sample computer code text

    <small> Defines small text

    <span> Defines a section in a document

    <s> Defines strikethrough text

    <strike> Defines strikethrough text

    <strong> Defines strong text

    <sub> Defines subscripted text

    <sup> Defines superscripted text

    <u> Defines underlined text

    Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

     
    Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.