KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > FibonacciServlet


1 /* Copyright 2002-2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.samples;
23
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.math.BigInteger JavaDoc;
27
28 import javax.servlet.http.HttpServlet JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import nu.xom.Attribute;
33 import nu.xom.Document;
34 import nu.xom.Element;
35 import nu.xom.ProcessingInstruction;
36 import nu.xom.Serializer;
37
38
39 /**
40  * <p>
41  * Demonstrates a servlet that processes XML-RPC requests.
42  * </p>
43  *
44  * @author Elliotte Rusty Harold
45  * @version 1.0
46  *
47  */

48 public class FibonacciServlet extends HttpServlet JavaDoc {
49
50     
51     public void doGet(
52       HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
53       throws IOException JavaDoc {
54     
55         //read the query string
56
int numberOfGenerations;
57         String JavaDoc generations = request.getParameter("generations");
58         try {
59             numberOfGenerations = Integer.parseInt(generations);
60         }
61         catch (Exception JavaDoc ex) { // NumberFormat or NullPointerException
62
// use default value of 10
63
numberOfGenerations = 10;
64         }
65      
66         response.setContentType("text/xml; charset=UTF-8");
67         OutputStream JavaDoc out = response.getOutputStream();
68     
69         Element root = new Element("Fibonacci_Numbers");
70         Document doc = new Document(root);
71         ProcessingInstruction stylesheet = new
72           ProcessingInstruction("xml-stylesheet",
73           "type='text/css' HREF='/xml/styles/fibonacci.css'");
74         doc.insertChild(stylesheet, 0);
75     
76         BigInteger JavaDoc low = BigInteger.ONE;
77         BigInteger JavaDoc high = BigInteger.ONE;
78         for (int i = 1; i <= numberOfGenerations; i++) {
79             Element fibonacci = new Element("fibonacci");
80             Attribute index
81               = new Attribute("index", String.valueOf(i));
82             fibonacci.addAttribute(index);
83             fibonacci.appendChild(low.toString());
84             root.appendChild(fibonacci);
85        
86             BigInteger JavaDoc temp = high;
87             high = high.add(low);
88             low = temp;
89         }
90         Serializer serializer = new Serializer(out, "UTF-8");
91         serializer.write(doc);
92         serializer.flush();
93         out.close();
94     
95     }
96
97     
98     public void doPost(
99       HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
100       throws IOException JavaDoc {
101         
102         doGet(request, response);
103         
104     }
105
106 }
Popular Tags