1 package web; 9 10 import java.io.IOException ; 11 import java.io.PrintWriter ; 12 import javax.servlet.ServletException ; 13 import javax.servlet.http.*; 14 import javax.naming.InitialContext ; 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 OBJ_NAME = "hej"; 25 26 27 public void init() throws ServletException { 28 try { 29 System.out.println("[HelloServlet] connecting to ozone..."); 30 db = (OzoneInterface) new InitialContext ().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 e) { 43 e.printStackTrace(); 44 } 45 } 46 47 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 48 String 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 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 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 |