| | 29 | === Automatic Data Binding to Request Handler == |
| | 30 | |
| | 31 | Hello.java class has serveral parameter values name and year: |
| | 32 | {{{ |
| | 33 | |
| | 34 | public class Hello implements RequestHandler |
| | 35 | { |
| | 36 | private String name = ""; |
| | 37 | private int year = 2000; |
| | 38 | |
| | 39 | public void handle(HttpServletRequest request, HttpServletResponse response) |
| | 40 | { |
| | 41 | PrintWriter out = response.getWriter().println("Hello " + name + "(" + year + ")"); |
| | 42 | } |
| | 43 | |
| | 44 | public void setName(String name) { this.name = name; } |
| | 45 | public void setYear(int year) { this.year = year; } |
| | 46 | } |
| | 47 | |
| | 48 | |
| | 49 | }}} |
| | 50 | |
| | 51 | Our RequestDispatcher automatically set these parameter values from a given HTTP request. For example, an HTTP request http://localhost:8989/hello.action?name=leo&year=2007 will invoke setName("leo") and setYear(2007). |
| | 52 | |
| | 53 | Note that, although the query string in the HTTP request consists of string values, our BeanUtil library |
| | 54 | detects the data type to which the string should be translated by reading the class definition (in this example, Hello class). |
| | 55 | |
| | 56 | |
| | 57 | |