Getting Started with Ajax
June 23, 2005
Serving XML Data as a Servlet
In order to properly fulfill Ajax requests from the client, we create a
We will abstain from delving into the details of
The client side Javascript function
javax.servlet.http.HttpServlet called TableServlet. When a client
sends a request to the servlet with xmlGet(...), the doGet(...)
function of TableServlet is called. Likewise, when a client sends a request
to the servlet with xmlPost(...), the doPost(...) function is
called. To simplify things, we will handle both of these request methods the same and delegate
the call to a new function in TableServlet called handleRequest(...).
Here's what it will look like:
01 /**
02 * Delegation method which handles GET and POST requests.
03 *
04 * @param request
05 * the request the client has made of the servlet
06 * @param response
07 * the response the servlet sends to the client
08 * @throws ServletException
09 * @throws IOException
10 */
11 public void handleRequest(HttpServletRequest request,
12 HttpServletResponse response) throws ServletException, IOException {
13
14 // Set the response content type indicating that we are
15 // responding with xml.
16 response.setContentType("text/xml");
17
18 try {
19
20 // get the page from the query string
21 String page = request.getParameter(TableServlet.KEY_PAGE);
22 if (page == null || page.length() == 0) {
23 page = "1";
24 }
25
26 // Delegate business logic to somebody else who will eventually
27 // supply the table xml.
28 String tableXml = TableManager.getXml(Integer.parseInt(page));
29
30 // Send the table's xml out through the response.
31 response.getWriter().println(tableXml);
32
33 } catch(IllegalArgumentException e) {
34 // Http 1.1 Status Code 400 = "Bad Request"
35 response.sendError(400, e.getMessage());
36 }
37 catch (IOException e) {
38 throw e;
39 } catch (Exception e) {
40 // Log all other exceptions
41 // Rethrow the exception as an unchecked RuntimeException
42 throw new RuntimeException(e.getMessage());
43 }
44 }
handleRequest(...) contains the bulk of the logic in TableServlet.
The function receives an incoming request, prepares a response by setting its
content-type to text/xml
(line 16),
determines the page of the table being
requested
(lines 20-24),
delegates retrieval of the appropriate table data to the
TableManager
(line 28),
and responds with the requested XML
(line 31).
We will abstain from delving into the details of
TableManager since it is
outside the scope of this article. For our purposes of getting a better understanding
of Ajax, it is sufficient to describe the format of the supplied XML table data. The following
is a sample of how the data is described.
01 <table>
02 <header>
03 <cell key="name">Name</cell>
04 <cell key="address">Address</cell>
05 <cell key="city">City</cell>
06 <cell key="state">State</cell>
07 <cell key="zip">Zip</cell>
08 </header>
09 <row>
10 <cell key="name">Hank</cell>
11 <cell key="address">1B Something Street</cell>
12 <cell key="city">Marietta</cell>
13 <cell key="state">GA</cell>
14 <cell key="zip">30339</cell>
15 <row>
16 <row>
17 <cell key="name">Kevin</cell>
18 <cell key="address">1 Two Street</cell>
19 <cell key="city">Wake Forest</cell>
20 <cell key="state">NC</cell>
21 <cell key="zip">27587</cell>
22 <row>
23 </table>
tableResponseHandler() parses the response XML,
manually constructs an HTML <table>, and inserts this table into the
'tableSection' DIV of the original document. Although this method of transforming
the data works, it is not very clean and is difficult to reuse.
Download Example Source Code

