# 18. 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 a db4o point of view. One is the more complex classloader environment - db4o needs to know itself (of course) 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 server 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 context via an appropriate listener. A very basic sketch might look like this:

`
public class Db4oServletContextListener
implements ServletContextListener {
public static final String KEY_DB4O_FILE_NAME = "db4oFileName";
public static final String KEY_DB4O_SERVER = "db4oServer";
private ObjectServer server=null;
public void contextInitialized(ServletContextEvent event) {
close();
ServletContext context=event.getServletContext();
String filePath=context.getRealPath(
"WEB-INF/db/"+context.getInitParameter(KEY_DB4O_FILE_NAME));
server=Db4o.openServer(filePath,0);
context.setAttribute(KEY_DB4O_SERVER,server);
context.log("db4o startup on "+filePath);
}
public void contextDestroyed(ServletContextEvent event) {
ServletContext context = event.getServletContext();
context.removeAttribute(KEY_DB4O_SERVER);
close();
context.log("db4o shutdown");
}
private void close() {
if(server!=null) {
server.close();
}
server=null;
}
}
`

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

`<context-param> <param-name>db4oFileName</param-name> <param-value>db4oweb.yap</param-value> </context-param> <listener> <listener-class> com.db4o.sample.web.Db4oServletContextListener </listener-class> </listener>`

Now db4o should be available to your application classes.

`ObjectServer server= (ObjectServer)context.getAttribute("db4oServer");`

A more complex and 'old school' example without using context listeners comes with the samples section of the db4o3 distribution that's still available from our web site.
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.)
--
generated by Doctor courtesy of db4objects Inc.