KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servlets > ServletTest


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s):
22  * --------------------------------------------------------------------------
23  * $Id: ServletTest.java,v 1.6 2004/04/19 06:39:30 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package servlets;
28
29 import java.io.IOException;
30 import java.io.PrintWriter;
31 import java.util.Calendar;
32 import java.util.Vector;
33
34 import javax.naming.Context;
35 import javax.naming.InitialContext;
36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServlet;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40
41 import com.titan.address.AddressHomeLocal;
42 import com.titan.address.AddressLocal;
43 import com.titan.customer.CreditCardHomeLocal;
44 import com.titan.customer.CreditCardLocal;
45 import com.titan.customer.CustomerHomeLocal;
46 import com.titan.customer.CustomerLocal;
47 import com.titan.customer.Name;
48 import com.titan.phone.PhoneLocal;
49
50
51
52 /**
53  * This servlet is used to test O'Reilly examples
54  * @author JOnAS team
55  */

56 public class ServletTest extends HttpServlet {
57
58     /**
59      * Called by the server (via the service method) to allow a servlet to
60      * handle a GET request.
61      * @param request an HttpServletRequest object that contains the request
62      * the client has made of the servlet
63      * @param response an HttpServletResponse object that contains the
64      * response the servlet sends to the client
65      * @throws IOException if an input or output error is detected when the
66      * servlet handles the GET request
67      * @throws ServletException if the request for the GET could not be handled
68      */

69     public void doGet(HttpServletRequest request, HttpServletResponse response)
70         throws IOException, ServletException {
71
72         boolean ok = true;
73         response.setContentType("text/html");
74         PrintWriter out = response.getWriter();
75
76         out.println("<html>");
77         out.println("<head>");
78         out.println("<title>");
79         out.println("O'Reilly Examples</title>");
80         out.println("<link rel=\"stylesheet\" HREF=\"style.css\" />");
81         out.println("</head>");
82         out.println("<body style=\"background : white; color : black;\">");
83         out.println("<h1>Examples 1:1 Relationships</h1>");
84
85         Context initialContext = null;
86         try {
87             initialContext = new InitialContext();
88         } catch (Exception e) {
89             out.println("<p>ERROR: Cannot get initial context for JNDI: " + e + "</p>");
90             return;
91         }
92
93         out.println("<a HREF=\"index.html\"><b>Back to Menu</b></a>");
94
95
96         out.println("<h2>Example showing CreditCard/Customer relationship</h2>");
97
98         out.println("<p>(ie Setting reference in a one-to-one bidirectional relationship)</p>");
99
100         // Connecting to CustomerHomeLocal and CreditCardHomeLocal thru JNDI
101
CustomerHomeLocal customerhome = null;
102         CreditCardHomeLocal cardhome = null;
103         AddressHomeLocal addresshome = null;
104
105         try {
106             customerhome = (CustomerHomeLocal)
107                 initialContext.lookup("java:comp/env/ejb/CustomerHomeLocal");
108             cardhome = (CreditCardHomeLocal)
109                 initialContext.lookup("java:comp/env/ejb/CreditCardHomeLocal");
110             addresshome = (AddressHomeLocal)
111                 initialContext.lookup("java:comp/env/ejb/AddressHomeLocal");
112         } catch (Exception e) {
113             out.println("<p>ERROR: Cannot lookup java:comp/env/ejb/XXHomeLocal: "
114                         + e + "</p>");
115             return;
116         }
117
118         out.println("<h3>Creating Customer 'Smith'.</h3>");
119         Integer primaryKey = new Integer(71);
120         CustomerLocal customer = null;
121         try {
122             customer = customerhome.create(primaryKey);
123         } catch (javax.ejb.CreateException ex) {
124             out.println("<p>Cannot create customer: "
125                         + ex + "</p>");
126             return;
127         }
128         customer.setName(new Name("Smith", "John"));
129
130         out.println("<h3>Creating CreditCard.</h3>");
131         // set Credit Card info
132
Calendar now = Calendar.getInstance();
133         CreditCardLocal card = null;
134         try {
135             card = cardhome.create(now.getTime(), "370000000000001", "John Smith", "O'Reilly");
136         } catch (javax.ejb.CreateException ex) {
137             out.println("<p>ERROR: Cannot create creditcard : " + ex + " </p>");
138             return;
139         }
140
141         out.println("<H3>Linking CreditCard and Customer.</H3>");
142         customer.setCreditCard(card);
143
144         out.println("<H3>Testing both directions on relationship:</H3>");
145         String cardname = customer.getCreditCard().getNameOnCard();
146         out.println("customer.getCreditCard().getNameOnCard() = '" + cardname + "'<br>");
147         Name name = card.getCustomer().getName();
148         String custfullname = name.getFirstName() + " " + name.getLastName();
149         out.println("card.getCustomer().getName() = '" + custfullname + "'<br>");
150         if (!cardname.equals(custfullname)) {
151             out.println("<p>ERROR: The name must be the same</p>");
152             ok = false;
153         }
154         out.println("<H3>Content of Tables:</H3>");
155         try {
156             listCustomers(out, customerhome);
157             listCreditcards(out, cardhome);
158         } catch (Exception e) {
159             out.println("<p>ERROR: exception caught = " + e + "</p>");
160         }
161
162         out.println("<H3>Unlink the beans using CreditCard, test Customer side:</H3>");
163         card.setCustomer(null);
164         CreditCardLocal newcardref = customer.getCreditCard();
165         if (newcardref == null) {
166             out.print("<p>Card is properly unlinked from customer bean</p>");
167         } else {
168             out.print("<p>ERROR: Whoops, customer still thinks it has a card !</p>");
169             ok = false;
170         }
171         // relink to avoid side effect on further test
172
card.setCustomer(customer);
173
174
175         out.println("<h2>Example Showing Customer/Address Relationship</h2>");
176         out.println("<p>(ie Setting reference in a one-to-one unidirectional relationship)</p>");
177
178         // Find Customer 71
179
primaryKey = new Integer(71);
180         try {
181             customer = customerhome.findByPrimaryKey(primaryKey);
182         } catch (Exception e) {
183             out.println("<p>ERROR: Exception : " + e + "</p>");
184             return;
185         }
186
187         AddressLocal addr = customer.getHomeAddress();
188         if (addr == null) {
189             out.println("<h3>Address reference is NULL, Creating one and setting in Customer:</h3>");
190             try {
191                 addr = addresshome.createAddress("333 North Washington", "Minneapolis", "MN", "55401");
192             } catch (Exception e) {
193                 out.println("<p>ERROR: Exception : " + e + "</p>");
194                 return;
195             }
196             customer.setHomeAddress(addr);
197         }
198         out.println("<p>Address Info: "
199                     + addr.getStreet() + ", "
200                     + addr.getCity() + ", "
201                     + addr.getState() + ", "
202                     + addr.getZip() + "</p>");
203         // Modifying Address
204

205         out.println("<H3>Modifying Address through address reference:</H3>");
206         addr.setStreet("445 East Lake Street");
207         addr.setCity("Wayzata");
208         addr.setState("MN");
209         addr.setZip("55432");
210         out.println("<p>Address Info: "
211                     + addr.getStreet() + ", "
212                     + addr.getCity() + ", "
213                     + addr.getState() + ", "
214                     + addr.getZip() + "</p>");
215
216         out.print("<H3>Creating New Address and calling setHomeAddress():</H3>");
217         AddressLocal addrold = addr;
218         try {
219             addr = addresshome.createAddress("700 Main Street", "St. Paul", "MN", "55302");
220         } catch (Exception e) {
221             out.println("<p>ERROR: Exception : " + e + "</p>");
222             return;
223         }
224         out.println("<p>Address Info: "
225                     + addr.getStreet() + ", "
226                     + addr.getCity() + ", "
227                     + addr.getState() + ", "
228                     + addr.getZip() + "</p>");
229         customer.setHomeAddress(addr);
230
231         // Note: Original Address remains in database, orphaned by setHomeAddress call..
232

233
234         out.print("<H3>Retrieving Address reference from Customer via getHomeAddress():</H3>");
235         addr = customer.getHomeAddress();
236         out.print("<p>Address Info: "
237                   + addr.getStreet() + ", "
238                   + addr.getCity() + ", "
239                   + addr.getState() + ", "
240                   + addr.getZip() + "</p>");
241
242         out.print("<H3>Content of Tables:</H3>");
243         try {
244             listCustomers(out, customerhome);
245             listAddress(out, addresshome);
246         } catch (Exception e) {
247             out.println("<p>ERROR: exception caught = " + e + "</p>");
248             return;
249         }
250
251
252         out.println("<h2>Example Showing Customer/Phone Relationship</h2>");
253
254         // Display current phone numbers and types
255
out.println("<h3>Starting content of phone list:</h3>");
256
257         Vector vv = customer.getPhoneList();
258         listPhones(out, vv);
259
260         // add a new phone number
261
out.println("<h3>Adding a new type '1' phone number to Customer 'John Smith'.</h3>");
262         try {
263             customer.addPhoneNumber("612-555-1212", (byte) 1);
264         } catch (Exception e) {
265             out.println("<p>ERROR: Exception : " + e + "</p>");
266             return;
267         }
268
269         out.println("<h3>New content of phone list for customer 'John Smith':</h3>");
270         vv = customer.getPhoneList();
271         listPhones(out, vv);
272
273         // add a new phone number
274
out.println("<h3>Adding a new type '2' phone number to Customer 'John Smith'.</h3>");
275         try {
276             customer.addPhoneNumber("800-333-3333", (byte) 2);
277         } catch (Exception e) {
278             out.println("<p>ERROR: Exception : " + e + "</p>");
279             return;
280         }
281         out.println("<h3>New content of phone list for customer 'John Smith':</h3>");
282         vv = customer.getPhoneList();
283         listPhones(out, vv);
284
285         // update a phone number
286
out.println("<h3>Updating type '1' phone numbers.</h3>");
287         try {
288             customer.updatePhoneNumber("763-555-1212", (byte) 1);
289         } catch (Exception e) {
290             out.println("<p>ERROR: Exception : " + e + "</p>");
291             return;
292         }
293
294         out.println("<h3>New content of phone list for customer 'John Smith':</h3>");
295         vv = customer.getPhoneList();
296         listPhones(out, vv);
297
298         // delete a phone number
299
out.print("<h3>Removing type '1' phone numbers from this Customer.</h3>");
300         customer.removePhoneNumber((byte) 1);
301
302         out.println("<h3>Final content of phone list:</h3>");
303         vv = customer.getPhoneList();
304         listPhones(out, vv);
305         // Note that the phone is still in the database,
306
// but it is no longer related to this customer bean
307

308         // Remove newly created beans for clean
309
out.println("<H2>Remove newly created beans for cleaning.</H2>");
310         try {
311             customer.remove();
312             addrold.remove();
313         } catch (Exception e) {
314             out.println("<p>Exception : " + e + "</p>");
315             return;
316         }
317
318         if (ok) {
319             out.println("<p align=\"center\"><strong>Servlet is OK.</strong></p>");
320         }
321
322         out.println("<a HREF=\"index.html\"><b>Back to Menu</b></a>");
323         out.println("</body>");
324         out.println("</html>");
325     }
326
327     private void listCustomers (PrintWriter out, CustomerHomeLocal chl) throws Exception {
328         out.println("<p><b>Customers</b> Table Content:</p>");
329         out.println("<ul>");
330         java.util.Collection clc = chl.findAllCustomers();
331         if (clc == null) {
332             out.println("<li>Customers table is empty</li>");
333         } else {
334             java.util.Iterator iterator = clc.iterator();
335             while (iterator.hasNext()) {
336                 CustomerLocal cl = (CustomerLocal) iterator.next();
337                 String name = cl.getName().getLastName();
338                 String number = cl.getCreditCard().getNumber();
339                 out.print("<li>customerLastName = '" + name + "', creditCardNumber = '" + number);
340                 if (cl.getHomeAddress() != null) {
341                     String city = cl.getHomeAddress().getCity();
342                     out.print("', city = '" + city);
343                 }
344                 out.println("'</li>");
345                 java.util.Collection phoneNumbers = cl.getPhoneNumbers();
346                 java.util.Iterator phiterator = phoneNumbers.iterator();
347                 out.println("<ul>");
348                 while (phiterator.hasNext()) {
349                     PhoneLocal phone = (PhoneLocal) phiterator.next();
350                     out.println("<li>phoneType = '" + phone.getType()
351                                 + "', phoneNumber = '" + phone.getNumber() + "'</li>");
352                 }
353                 out.println("</ul>");
354
355             }
356         }
357         out.println("</ul>");
358     }
359
360     private void listCreditcards (PrintWriter out, CreditCardHomeLocal cchl) throws Exception {
361         out.println("<p><b>Creditcards</b> Table Content:</p>");
362         out.println("<ul>");
363         java.util.Collection clc = cchl.findAllCreditCards();
364         if (clc == null) {
365             out.println("<li>CreditCards table is empty</li>");
366         } else {
367             java.util.Iterator iterator = clc.iterator();
368             while (iterator.hasNext()) {
369                 CreditCardLocal ccl = (CreditCardLocal) iterator.next();
370                 String number = ccl.getNumber();
371                 String name = ccl.getNameOnCard();
372                 out.println("<li>creditCardNumber = '" + number
373                             + "', nameOnCard = '" + name + "'</li>");
374             }
375         }
376         out.println("</ul>");
377     }
378
379     private void listAddress (PrintWriter out, AddressHomeLocal cchl) throws Exception {
380         out.println("<p><b>Addresses</b> Table Content:</p>");
381         out.println("<ul>");
382         java.util.Collection clc = cchl.findAllAddress();
383         if (clc == null) {
384             out.println("<li>Addresses table is empty</li>");
385         } else {
386             java.util.Iterator iterator = clc.iterator();
387             while (iterator.hasNext()) {
388                 AddressLocal al = (AddressLocal) iterator.next();
389                 String city = al.getCity();
390                 String street = al.getStreet();
391                 out.println("<li>city = '" + city + "', street = '" + street + "'</li>");
392             }
393         }
394         out.println("</ul>");
395     }
396
397     private void listPhones(PrintWriter out, Vector vv) {
398         out.println("<ul>");
399         if (vv.size() == 0) {
400                 out.println("<li>phones list is empty</li>");
401         } else {
402             for (int jj = 0; jj < vv.size(); jj++) {
403                 String ss = (String) (vv.get(jj));
404                 out.println("<li>" + ss + "</li>");
405             }
406         }
407         out.println("</ul>");
408     }
409
410 }
411
Popular Tags