Hobione's Weblog

Living & Breathing in Web 2.0 Era

Another day with DWR 2.0 – error to Execute

Since demo application worked, I wanted to leverage my DWR knowledge in my existing servlet application. I have a shopping cart application that I wanted to add ajax callback for a field and I am having some serious issues.

Marshalling issue Since ‘has-a’ relationship exist in ItemOrder.java, so I had to do marshalling on ItemOrder and CDTypeVO in dwr.xml. Here is the code for marshalling both beans.

<convert converter=”bean” match=”cdorder.domain.ItemOrder” />
<create creator=”new” javascript=”ItemOrder”>
<param name=”class” value=”cdorder.domain.ItemOrder”/>

<convert converter=”bean” match=”cdorder.domain.CDTypeVO” />
<create creator=”new” javascript=”CDTypeVO” >
<param name=”class” value=”cdorder.domain.CDTypeVO”/>

This resolved all red error, but IteamOrder bean still not functioning properly. Every time I click those Execute button, I am getting a nasty pop up Error message. Not sure, what am I missing.

DWR Pop up Error Here is the architecture description: I have a form which has ‘Add’ buttons. When an Add button get clicked, it submits a form and request goes to OrderPageServlet.java and here what it does as follows:

  1. Get a shopping cart from session variable
  2. If shopping cart is null, instantiate a empty shopping cart and put it in the session

i. cart = (ShoppingCart)session.getAttribute(“shoppingCart”);

ii. // New visitors get a fresh shopping cart.

iii. // Previous visitors keep using their existing cart.

iv. if (cart == null) {

v. cart = new ShoppingCart();

vi. session.setAttribute(“shoppingCart”, cart);

vii. }

  1. Now, based on CD Id that has been passed, add item to the cart in Servlet

i. cart.addItem(cdID,type);

  1. ShoppingCart has an ArrayList that keep tracks all order
  2. Add item to the list in the ShopptingCart.java

i. private ArrayList itemsOrdered = new ArrayList();

ii. ItemOrder newOrder = new ItemOrder(cdTypeDAO.getSingleCdType(cdID,false));

iii. itemsOrdered.add(newOrder);

  1. Here is the code snippet of ItemOrder.java

public class ItemOrder {

private CDTypeVO item;

private int numItems;

private String processedDate;

private String processedBy;

public ItemOrder(CDTypeVO item) {




public CDTypeVO getItem() {

return (item);



// public String toString(){

// return item.toString() + ” ” + getNumItems() + ” cdID ” + getCdId() + ” cdType ” + getCDType() + ” description: ” + getDescription() + “\n”;

// }

public void setItem(CDTypeVO item) {

this.item = item;


public int getCdId() {

return (getItem().getCdId());


public String getCDType() {

return (getItem().getCdType());


public String getDescription() {

return (getItem().getDescription());



public int getNumItems() {

return numItems;


public void setNumItems(int n) {

this.numItems = n;


public void incrementNumItems() {

setNumItems(getNumItems() + 1);


public void cancelOrder() {



public String getProcessedDate() {

return processedDate;


public void setProcessedDate(String processedDate) {

this.processedDate = processedDate;


public String getProcessedBy() {

return processedBy;


public void setProcessedBy(String processedBy) {

this.processedBy = processedBy;


public String sayHello(String name) {

return “Hello, ” + name;



  1. Here is the code snippet for CDTypeVO.java Bean\
  2. /*
  3. * CDType.java
  4. *
  5. * Created on December 6, 2004, 10:02 AM
  6. */
  7. package cdorder.domain;
  8. /**
  9. *
  10. * @author hhaque
  11. * VO = Value Object
  12. */
  13. public class CDTypeVO implements java.io.Serializable{
  14. private int cdId;
  15. private String cdType;
  16. private String description;
  17. private String date;
  18. /** Creates a new instance of CDType */
  19. public CDTypeVO() {
  20. }
  21. // public String toString(){
  22. //
  23. // return “cdID = ” + getCdId() +
  24. // ” cdType = ” + getCdType() +
  25. // ” description = ” +getDescription() +” date = “+getDate() + “\n”;
  26. // }
  27. public int getCdId() {
  28. return cdId;
  29. }
  30. public void setCdId(int cdId) {
  31. this.cdId = cdId;
  32. }
  33. public String getCdType(){
  34. return cdType;
  35. }
  36. public void setCdType(String cdType){
  37. this.cdType = cdType;
  38. }
  39. public String getDescription(){
  40. return description;
  41. }
  42. public void setDescription(String description){
  43. this.description = description;
  44. }
  45. public String getDate(){
  46. return date;
  47. }
  48. public void setDate(String date){
  49. this.date = date;
  50. }
  51. }

When I do this


var numItemValue = dwr.util.getValue(‘numItems’);

ItemOrder.sayHello(numItemsValue, loadDataCallback); It does not do anything.

Here is my call back function:

function loadDataCallback(returnValue) {

dwr.util.setValue(‘demoReply’, returnValue);

} </script>

December 14, 2007 Posted by | Ajax Libraries, DWR | Leave a comment

Debug DWR (How to)

Here is the great trick that I have learned today. How to debug DWR? All I had to do to add debug = true in my web.xml file (http://getahead.org/dwr/server/servlet)


Redeploy the application and in the URL I typed in http://localhost:8084/cdOrder/dwr

DWR debugger

Keep in mind that ‘dwr’ is a keyword, it is very cool features and I love it. It helped me to speed up my development time.

December 14, 2007 Posted by | Ajax Libraries, DWR | 1 Comment

DWR and Java Bean Configuration

Jason: DWR dynamically creates a JS file that has a stub that matches the class and methods you’re exposing. so, if you have a Demo class with the method doFoo(), it creates a stub JS class called Demo with the method doFoo() that wraps the Ajax call to the DWR servlet

End of messages received while you were offline at 9:23 AM on Wednesday
Jason: you call Demo.doFoo(<callback>) (iirc) from your page, and it uses the stub plus the plumbing from engine.js to make the XHR call, get the results, and cal your callback function

HobiOne: nice, so question is, if I have 10 beans, I have to create 10 js

Jason: well, you have to configure DWR to expose those beans, then include each JS file in your page
if I were you, i’d use annotations. much easier :)

HobiOne: I see, okay back to your answer, “configure DWR”, do you mean in dwr.xml file

Jason: yes though if you use annotations, I think you set up a different DWR servlet, then just annotate the class and methods you want to expose
it would be nice if DWR offered a KitchenSink.js that included everything else, but I don’t think it does

Sent at 9:27 AM on Wednesday
HobiOne: how about DAO stuff, do I only register beans to dwr.xml or any java classes I make in back end

Jason: well, i’d be REALLY careful what you expose to the outside world

Sent at 9:31 AM on Wednesday
Jason: i.e., i would NOT expose DAOs

HobiOne: got it, only beans, b/c from html, I dont need to talk to DAO, bean should be responsible to communicate with DAOs

A DWR Data Store for Dojo (Putting two powerful AJAX libraries to work for you):

December 14, 2007 Posted by | Ajax Libraries, DWR | Leave a comment