An AJAX email contact form using DWR and Scriptaculous
Coming from the Java Server side, my JavaScript skills are pretty limited, a lot of the AJAX frameworks still require a good deal of JavaScript, or wildly complex configurations, not to mention having to take into consideration how you plan to accept and answer the asynchronous calls on the server side. I needed a framework that was easy to configure, was at home in the Servlet Container, and could expose my existing API's to be called asynchronously.
I came across http://getahead.org and their AJAX framework, DWR, and found they had exactly what I needed and at the right price again, free.
Now down to the business of the article. The purpose here is to give some sample source that I worked through in following the directions on GetAhead's website. I had some issues following their directions and figured others might too, some things didn't seem to be working in their examples.
Source for this article is available here: testDWRContactForm.war
Step 1 - Download DWR and add it to your project lib directory
- Download dwr.jar
Step 2 - Add the configuration to load DWR to your web.xml
dwr-invoker DWR Servlet org.directwebremoting.servlet.DwrServlet debug true dwr-invoker /dwr/*
Step 3 - Add dwr.xml to your WEB-INF directory
Step 4 - Create classes to be used by DWR
- I created a Contact class, which is a simple java bean which is specified in the Converter above
- I then created a ContactEmail class which validates the form data and sends the email
Step 5 - Start and test that the dwr installation picked up your ContactEmail class
- Start your server and load your localhost, ex. http://localhost/dwr/
- You should see your ContactEmail class listed here:
Step 6 - Add code and form to jsp or html file to talk to DWR
- The button on the form calls the writeContact() javascript method
- The first call the setValue() utility method of dwr to reset the value of the error message
- The second call uses the getValues() utility method to store the fields of the form in the contact javascript object, I found it important to set the person object up with the field names beforehand as seen in order for the method to getValues() method to populate the fields appropriately
- The checkFields local method is then called with the contact object
- In the checkFields method we call our DWR interfaced object with the contact object specifying reply as the callback function, this is another area where the DWR documentation seemed a little backwards
- The reply method is called automatically when the DWR call returns
- We then check for a "valid" message and determine whether we'll call the setContact method or display an error message
- There are also some minor DOM changes to give indication to the user what's going on
- The success method is called automatically when the DWR call returns from ContactEmail.setContact
- At this point we evaluate whether or not the message was sent successfully, if successful we use some Scriptaculous effects to have the contact form Fade Out and a Success message Appear
%MINIFYHTML6544c61b3431a593f737b8ee6e0719514%Contact Us
* required fields * First Name: * Last Name: Company: Phone: Fax: * E-Mail:
March 21st, 2007 - 11:06
You use prototype library, why you not use the $ function instead of the long “document.getElementById” ?
Great article.
March 21st, 2007 - 12:37
Thanks for the feedback, I only attached the prototype library because scriptaculous requires it, I’m not as studied up on prototype’s use.
September 9th, 2007 - 04:06
Very nice example.
Thts what i was looking for.
September 17th, 2007 - 21:33
Hi
Very interesting information! Thanks!
Bye
October 5th, 2007 - 09:22
Great! Let me try this out
November 14th, 2007 - 01:39
I believe it automagically maps the form field to Contact object. But how about if we have multiple form having duplicate textfield? How do we map it?
Great simplistic tutorial!
November 14th, 2007 - 10:03
If you’re talking about the util methods in DWR, getValues() and setValues(), the mapping is done via the id’s of the fields, in html/xhtml an id should always be unique, therefore multiple fields wouldn’t have an issue with the automagic mapping.
April 5th, 2008 - 01:47
can anyone give me the advantages of using DWR and google web toolkit over other ajax frameworks . also please mention , under which conditions which framework is best and why. Please help me out,I would be highly obliged.
April 7th, 2008 - 14:14
Well the advantages of both would be if you’re at home with Java, you’re going to be able to write more Java to handle your Ajax development than you normally would.
With Google Web Toolkit you basically write all of your code in Java and GWT generates the pages and Javascript code for you, it even has a browser piece during development to do the Java to Javascript interpretation realtime, what you do with it after that as far as integrating it into an existing application or just using the html and javascript generated is up to you. I believe GWT also has a bunch of widgets that will make construction of a full blown Ajax application easier.
DWR is more geared toward asynchronous interaction with Java-based applications. With DWR you can expose existing API’s that you’ve written in Java and return your Javabeans converted to Javascript and DWR does all the heavy lifting for you. DWR is great for adding Ajax to existing Java applications, it abstracts all of the difficulty of making asynchronous calls, but will in most cases still require a fair bit of Javascript in order to manipulate the DOM. It comes with a util.js with some DOM utilities, but I’ve found in my experience that a library such as jQuery is much better suited for this type of work.
My recommendation would be if you’re writing a new application that’s going use a lot of Ajax, give GWT a try. If you’re adding functionality to an existing application DWR and jQuery make a great combination.
January 6th, 2009 - 12:32
Hi. Good site.