There must be a server-side handler that will process the AJAX request. This could be a servlet, Struts action, another JSP, or even a non-Java, server-side component. The only requirement of this component is that it return an XML file that represents a list of results based on the query passed to it from the client-side AJAX functions.
There are helper classes to assist in building the XML if you don't want to do it by hand...see the advanced guide for more information.
For each JSP in which you wish to use the AJAX tag, simply define it as with any other tag library.
<%@ taglib uri="http://ajaxtags.sourceforge.net/tags/ajaxtags" prefix="ajax" %>
Map the resources which are stored in jar file to an url
<servlet> <servlet-name>sourceloader</servlet-name> <servlet-class> net.sourceforge.ajaxtags.servlets.SourceLoader </servlet-class> <!-- you don't need this, don't use spaces in url-pattern!--> <init-param> <param-name>prefix</param-name> <param-value>/ajaxtags</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>sourceloader</servlet-name> <!-- if you have /js/ in use in you webapp just map the url (use the prefix)--> <url-pattern>/ajaxtags/js/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>sourceloader</servlet-name> <url-pattern>/img/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>sourceloader</servlet-name> <url-pattern>/css/*</url-pattern> </servlet-mapping>
With AJAX, you'll need JavaScript. Fortunately, everything you'll need is included with a javascript loader file. Incidentally, the reason why a (bulky) JavaScript source file was chosen versus having each JSP tag generate only what it needs is because it's assumed that most browsers cache external resources like stylesheets, images, and JavaScript files. This should increase performance slightly. Besides, this core JavaScript is generic enough to use for other purposes that the tag library does not yet provide.
<script type="text/javascript" src="js/prototype.js"></script> <script type="text/javascript" src="js/scriptaculous/scriptaculous.js"></script> <script type="text/javascript" src="js/overlibmws/overlibmws.js"></script> <script type="text/javascript" src="<%=request.getContextPath()%>/js/ajaxtags.js"></script>
Many tags rely heavily on CSS to help visually represent their function. For example, in the case of the codeajax:autocomplete/code tag, CSS transforms a simple unordered list (i.e., UL > LI tags) into a dropdown list.
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/ajaxtags.css" /> <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/displaytag.css" />
Once everything is set up, all that's left is to pick you tag. See how they're used.