helloservlet.java

来自「Java的面向对象数据库系统的源代码」· Java 代码 · 共 65 行

JAVA
65
字号
// You can redistribute this software and/or modify it under the terms of// the Ozone Library License version 1 published by ozone-db.org.//// The original code and portions created by SMB are// Copyright (C) 1997-@year@ by Per Nyfelt. All rights reserved.//// $Id: OzoneServiceMBean.java,v 1.1 2002/09/09 13:59:26 per_nyfelt Exp $package web;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.*;import javax.naming.InitialContext;import db.Hello;import db.HelloImpl;import org.ozoneDB.OzoneInterface;public class HelloServlet extends HttpServlet {    private OzoneInterface db;    public Hello hello;    public static final String OBJ_NAME = "hej";    public void init() throws ServletException {        try {            System.out.println("[HelloServlet] connecting to ozone...");            db = (OzoneInterface) new InitialContext().lookup(OzoneInterface.class.getName());            System.out.println("[HelloServlet] Connected!");            db.reloadClasses();            System.out.println("[HelloServlet] Classes reloaded");            hello = (Hello) db.objectForName(OBJ_NAME);            if (hello == null) {                System.out.println("Storing new Hello object");                hello = (Hello) db.createObject(HelloImpl.class.getName(), OzoneInterface.Public, OBJ_NAME);            } else {                System.out.println("Found existing Hello object");            }            System.out.println("db.Hello has the following greeting: " + hello.getGreeting());        } catch (Exception e) {            e.printStackTrace();        }    }    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        String message;        try {            hello = (Hello) db.objectForName("hej");            message = "<html> <head> </head> <body> " + hello.getGreeting() + "</body> </html>";            System.out.println("[HelloServlet] Successfully used object");        } catch (Exception e) {            System.out.println("[HelloServlet] Can't connect to ozone." + e.toString());            e.printStackTrace();            message = "<html> <head> </head> <body> " + e.toString() + "</body> </html>";        }        PrintWriter out = response.getWriter();        System.out.println("[HelloServlet] Sending the following resonse back to client: " + message);        out.println(message);        out.flush();        out.close();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?