KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > ajax > CountryListAjaxServlet


1 package net.sf.uitags.ajax;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.servlet.ServletException JavaDoc;
6 import javax.servlet.http.HttpServlet JavaDoc;
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9
10 /**
11  * A demo servlet that returns a list of countries in a continent.
12  *
13  * @author jonni
14  */

15 public final class CountryListAjaxServlet extends HttpServlet JavaDoc {
16   private static final long serialVersionUID = 100L;
17
18   /**
19    * Expects a parameter named "searchValue" that contains the name of a
20    * continent, and writes a list of countries in the specified continent
21    * to the output stream. Only the continents "Europe" and "South America"
22    * are supported. The countries are in string, which if evaluated in
23    * the browser will produce a JavaScript array containing
24    * select box option objects.
25    */

26   protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
27       throws ServletException JavaDoc, IOException JavaDoc {
28     setResponseHeaders(response);
29     response.getWriter().write(getResponseMessage(request));
30   }
31
32   private void setResponseHeaders(HttpServletResponse JavaDoc response) {
33     response.setContentType("text/plain");
34     response.setHeader("Cache-Control", "no-cache");
35   }
36
37   private String JavaDoc getResponseMessage(HttpServletRequest JavaDoc request) {
38     String JavaDoc continent = request.getParameter("searchValue");
39
40     if ("Europe".equalsIgnoreCase(continent)) {
41       return
42           "new Array(" +
43           " new Option(\"Albania\", \"Albania\")," +
44           " new Option(\"Andorra\", \"Andorra\")," +
45           " new Option(\"Armenia\", \"Armenia\")," +
46           " new Option(\"Austria\", \"Austria\")," +
47           " new Option(\"Azerbaijan\", \"Azerbaijan\")," +
48           " new Option(\"Belarus\", \"Belarus\")," +
49           " new Option(\"Belgium\", \"Belgium\")," +
50           " new Option(\"Bosnia Herzegovina\", \"Bosnia Herzegovina\")," +
51           " new Option(\"Bulgaria\", \"Bulgaria\")," +
52           " new Option(\"Crotia\", \"Crotia\")," +
53           " new Option(\"Cyprus\", \"Cyprus\")," +
54           " new Option(\"Czech Republic\", \"Czech Republic\")" +
55           ");";
56     }
57     else if ("South America".equalsIgnoreCase(continent)) {
58       return
59           "new Array(" +
60           " new Option(\"Argentina\", \"Argentina\")," +
61           " new Option(\"Bolivia\", \"Bolivia\")," +
62           " new Option(\"Brazil\", \"Brazil\")," +
63           " new Option(\"Chile\", \"Chile\")," +
64           " new Option(\"Colombia\", \"Colombia\")," +
65           " new Option(\"Ecuador\", \"Ecuador\")," +
66           " new Option(\"Guyana\", \"Guyana\")," +
67           " new Option(\"Paraguay\", \"Paraguay\")," +
68           " new Option(\"Peru\", \"Peru\")," +
69           " new Option(\"Suriname\", \"Suriname\")," +
70           " new Option(\"Uruguay\", \"Uruguay\")," +
71           " new Option(\"Venezuela\", \"Venezuela\")" +
72           ");";
73     }
74     else {
75       // There are better ways of indicating error, just being lazy here.
76
return
77           "new Array(" +
78           " new Option(\"Invalid search param\", \"\")" +
79           ");";
80     }
81   }
82 }
83
Popular Tags