This is because
JavaScript by nature is an asynchronous language, while Java is synchronous. This means
there is no mechanism to make the remote call via Ajax and wait for it to return (other
than specifying the call is to be synchronous, which you can do at a low level with the
XMLHttpRequest object; however, it blocks everything in the browser, meaning it becomes
unresponsive while awaiting the request??™s completion, so it??™s generally a bad idea and not
quite what we want). Therefore, the callback mechanism is the logical answer.
Once the request completes, the callback function, doMathCallback(), is called. Notice the
parameter to the method. This is a representation of the return type of the method called,
which DWR has marshaled for us. This can be virtually any data type, intrinsic or custom, and
in this case it??™s just a simple integer. Once we have the value, the code simply constructs the
appropriate markup to display the equation, and inserts it into a new
element named
resultDiv.
CHAPTER 2 n GETTING TO KNOW DWR 64
There is one new piece to the puzzle, and that??™s the dwr.xml file that we have to create
to tell DWR a few things. Create the dwr.xml file from the code in Listing 2-8 and save it to
firstdwr/WEB-INF.
Pages:
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156