Handling Data for Ajax with JSON (Page 1 of 4 )
This article concludes a three-part series that focuses on XML and JSON for Ajax. It shows you how to build and run an Ajax character decoder application, and how to pass data with JSON. It is excerpted from chapter four of the book
Ajax on Java , written by Steven Douglas Olson (O'Reilly, 2007; ISBN: 0596101872). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Building the Application
Now that we have reviewed the code, let's build it and try it out. It's very similar to the example in the previous chapter, and because I don't like to overwrite my work, I put the new application in its own directory tree. The directory structure I used is shown in Figure4-2.
This is just a guide so you can see how I built the sample. You can name your directories differently as long as you know how to configure the application server properly.
Figure 4-2. Directory structure for Ajax Character Decoder (second version)
The web.xml file for the project is presented in Example 4-8.
Example 4-8. web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <servlet> <servlet-name>AjaxResponseServlet</servlet-name> <servlet-class> com.AJAXbook.servlet.AjaxResponseServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AjaxResponseServlet</servlet-name> <url-pattern>/response</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
The build.xml file for the project is shown in Example 4-9.
Example 4-9. build.xml
<?xml version="1.0"?> <project name="AJAX-CODECONVERTER " default="compile" basedir=".">
<property environment="env"/> <property name="src.dir" value="src"/> <property name="test.dir" value="test"/> <property name="war.dir" value="war"/> <property name="db.dir" value="db"/> <property name="class.dir" value="${war.dir}/WEB-INF/classes"/> <property name="test.class.dir" value="${test.dir}/classes"/> <property name="lib.dir" value="${war.dir}/WEB-INF/lib"/> <property name="webapp.dir" value="${env.TOMCAT_HOME}/ webapps/ajaxcodeconverter-lab2"/>
<path id="ajax.class.path"> <fileset dir="${lib.dir}"> <include name="*.jar"/> </fileset> </path>
<target name="testenv"> <echo message="env.TomcatHome=${env.TOMCAT_HOME}"/> <echo message="env.ANT_HOME=${env.ANT_HOME}"/> </target>
<target name="init"> <mkdir dir="${class.dir}"/> <mkdir dir="${test.class.dir}"/> </target>
<target name="compile" depends="init" description="Compiles all source code."> <javac srcdir="${src.dir}" destdir="${class.dir}" debug="on" classpathref="ajax.class.path"/> </target>
<target name="clean" description="Erases contents of classes dir"> <delete dir="${class.dir}"/> <delete dir="${test.class.dir}"/> </target>
<target name="deploy" depends="compile" description="Copies the contents of web-app to destination dir"> <copy todir="${webapp.dir}"> <fileset dir="${war.dir}"/> </copy> </target>
</project>
Next: Running the Application on Tomcat >>
More XML Tutorials Articles More By O'Reilly Media
Please enable JavaScript to view the comments powered by Disqus. blog comments powered by