You are here: Platform Specific Issues > Web Environment > Servlets

Servlets

Running db4o as the persistence layer of a Java web application is easy. There is no installation procedure - db4o is just another library in your application. There are only two issues that make web applications distinct from standalone programs from db4o's point of view. One is the more complex classloader environment - db4o needs to know itself and the classes to be persisted. Please refer to the classloader chapter for more information.

The other issue is configuring, starting and shutting down the db4o correctly. This can be done at the Servlet API layer or within the web application framework you are using.

On the Servlet API layer, you could bind db4o server handling to the Servlet.

You can implement the ServletContextListener-interface, open the database when the web application starts and close when it ends.

@Override
public void contextInitialized(ServletContextEvent event) {
    ServletContext context = event.getServletContext();
    String filePath = context.getRealPath("WEB-INF/"
            + context.getInitParameter(KEY_DB4O_FILE_NAME));
    EmbeddedObjectContainer rootContainer = Db4oEmbedded.openFile(filePath);
    context.setAttribute(KEY_DB4O_SERVER, rootContainer);
    context.log("db4o startup on " + filePath);
}


@Override
public void contextDestroyed(ServletContextEvent event) {
    ServletContext context = event.getServletContext();
    ObjectContainer rootContainer = (ObjectContainer) context.getAttribute(KEY_DB4O_SERVER);
    context.removeAttribute(KEY_DB4O_SERVER);
    close(rootContainer);
    context.log("db4o shutdown");
}
Db4oServletListener.java: db4o-instance for the web-application

Additionally you can implement the ServletRequestListener-interface and open a db4o-session on each request.

@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
    EmbeddedObjectContainer rootContainer = (EmbeddedObjectContainer) requestEvent
            .getServletContext().getAttribute(Db4oServletListener.KEY_DB4O_SERVER);

    ObjectContainer session = rootContainer.openSession();
    requestEvent.getServletRequest().setAttribute(KEY_DB4O_SESSION, session);
}


@Override
public void requestDestroyed(ServletRequestEvent requestEvent) {
    ObjectContainer session = (ObjectContainer) requestEvent
            .getServletRequest().getAttribute(KEY_DB4O_SESSION);

    close(session);
}
Db4oServletListener.java: a db4o-session for each request

This listener has to be registered in the web.xml.

<context-param>
    <param-name>database-file-name</param-name>
    <param-value>database.db4o</param-value>
</context-param>
<listener>
    <listener-class>com.db4odoc.servlet.Db4oServletListener</listener-class>
</listener>
web.xml: register the listener for the web application

Now db4o should be available to your application classes. Each request has it own object-container. You can get the instance via its key, like this:

ObjectContainer container = 
        (ObjectContainer)req.getAttribute(Db4oServletListener.KEY_DB4O_SESSION);
ServletExample.java: Get the session container

However, We strongly suggest that you use the features provided by your framework and that you consider not exposing db4o directly to your application logic. (There is nothing db4o-specific about these recommendentations, we would vote for this in the presence of any persistence layer.)