Likes

WCD-LAB @HOME 11

Create a servlet that generates delayed responses using the asynchronous servlet API and asynchronous JavaScript techniques.


Task - Create a New Project and Asynchronous Servlet
1. Create a new project named 5L31-nlll;bl. In the project create a ser.iet called Upsate in the package web.
2. Set the servlet to support asynchronous operation. Do this using the element asyncSuspor^ed = true in the gvleb Servlet annotation.
3. Delete the contents of the process Request method.

4. In the now-empty process Request method:
a. Start the asynchronous processing and keep a handle on the AsyncCootext in a final variable.
b. Consult the sample code in the "Simple Asynchronous Client Example" section in Module 11 of the student guide for more guidance on these steps.

5. Create an anommous inner class, implementing the Runnable interface. In the rur method of this class:

3. Extract the request and response objects from the -syncCc-ntext and store them m local variables.
b. Introduce a delay between 2 and 15 seconds.
c. Select a word from this list at random: wonderful, unexpected, strange, elegant
(you can add more adjectives if you choose, they will simply be used to construct a changing random remark). Return the chosen word as the entire body of the response (do not include air.- HTML tags whatsoever).
d. Call the cc<rplete method on the isyncCcntext.

6. Test the code. Run the application. To do this, build and deploy the application, and then enter the URL:
http: //localhost:8d80/SL314mlllabl/Update directly into the browser’s URL window.
Task - Create an Asynchronous Client

1. Edit the index, jsp file and the following text just after the top-level headmg
(</hl>):
<p>Oh my, that was... <span id="adjective">let me think...</span>!
<p>
2. Create a JavaScript block, after the end of the </body> tag. The script opening and closing tags are exemplified in the "Simple Asynchronous Example" section m Module 11 of the student guide.

3. In the JavaScript block, add the following elements. Refer again to the "Simple Asynchronous Client Example" section in Module 11 of the student guide if you need hints or guidance on the JavaScript syntax.
a. Add variable declarations for two variables called req and tcUpdate.
b. Use the docunent. jetElerentByld method to obtain a reference to the adjective span that was created in step one of tins task.
c. Initialize the req variable using either a new XMLHttpRequest. or a new AcciveXObject. dependmg on the browser support.
d. Declare a function called :endReque=t().
e. In the =endRequest() function, install a readystatechange handler. In the handler, when a successful response is received, update the innerHTHL element of the tcUpsate variable using the responseText of the request.
f. After the declaration of the readystatechange handler, call the open and send methods on the req object to send the HTTP request to the server. The request should send a GET request, to the URL Update.
g. Close the definition of the sendRequest method, then call the sendRequest method directly.

Task - Test the Application
1. Run the application.
2. The page should be displayed and show text that says Oh my, that was... let me think...!
3. After a few seconds, the text let ne think... should change, and continue changing at variable intervals of a few seconds.

No comments: