KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > web > HelloServlet


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by Per Nyfelt. All rights reserved.
6
//
7
// $Id: OzoneServiceMBean.java,v 1.1 2002/09/09 13:59:26 per_nyfelt Exp $
8
package web;
9
10 import java.io.IOException JavaDoc;
11 import java.io.PrintWriter JavaDoc;
12 import javax.servlet.ServletException JavaDoc;
13 import javax.servlet.http.*;
14 import javax.naming.InitialContext JavaDoc;
15
16 import db.Hello;
17 import db.HelloImpl;
18 import org.ozoneDB.OzoneInterface;
19
20 public class HelloServlet extends HttpServlet {
21
22     private OzoneInterface db;
23     public Hello hello;
24     public static final String JavaDoc OBJ_NAME = "hej";
25
26
27     public void init() throws ServletException JavaDoc {
28         try {
29             System.out.println("[HelloServlet] connecting to ozone...");
30             db = (OzoneInterface) new InitialContext JavaDoc().lookup(OzoneInterface.class.getName());
31             System.out.println("[HelloServlet] Connected!");
32             db.reloadClasses();
33             System.out.println("[HelloServlet] Classes reloaded");
34             hello = (Hello) db.objectForName(OBJ_NAME);
35             if (hello == null) {
36                 System.out.println("Storing new Hello object");
37                 hello = (Hello) db.createObject(HelloImpl.class.getName(), OzoneInterface.Public, OBJ_NAME);
38             } else {
39                 System.out.println("Found existing Hello object");
40             }
41             System.out.println("db.Hello has the following greeting: " + hello.getGreeting());
42         } catch (Exception JavaDoc e) {
43             e.printStackTrace();
44         }
45     }
46
47     public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException JavaDoc, IOException JavaDoc {
48         String JavaDoc message;
49         try {
50             hello = (Hello) db.objectForName("hej");
51             message = "<html> <head> </head> <body> " + hello.getGreeting() + "</body> </html>";
52             System.out.println("[HelloServlet] Successfully used object");
53         } catch (Exception JavaDoc e) {
54             System.out.println("[HelloServlet] Can't connect to ozone." + e.toString());
55             e.printStackTrace();
56             message = "<html> <head> </head> <body> " + e.toString() + "</body> </html>";
57         }
58         PrintWriter JavaDoc out = response.getWriter();
59         System.out.println("[HelloServlet] Sending the following resonse back to client: " + message);
60         out.println(message);
61         out.flush();
62         out.close();
63     }
64 }
65
Popular Tags