Local Repository Access

The content repository within this web application can be accessed locally by other web applications within the same servlet container. Local access is much faster than remote access.

The content repository is made available both through JNDI and the web application context.

Accessing the repository through JNDI

By default the repository is only made available in a dummy JNDI directory local to this web application. However, you can make the repository globally available if your servlet container allows a web application to modify the global JNDI directory or you are using some other JNDI directory that can manage unserializable Java objects.

To bind the the repository to such a JNDI directory, you need to modify the java.naming parameters in either the /WEB-INF/web.xml deployment descriptor or the jackrabbit/bootstrap.properties file. You need to redeploy this web application to activate the changes.

Use the following code to access a repository bound in a JNDI directory:

import javax.jcr.Repository;
import javax.naming.Context;
import javax.naming.InitialContext;

Context context = new InitialContext(...);
Repository repository = (Repository) context.lookup(...);

Accessing the repository through servlet context

This web application makes the repository available as the javax.jcr.Repository attribute in the application context. If your servlet container supports cross-context access, you can access the repository directly using that attribute.

For example in Apache Tomcat you can enable cross-context access by setting the crossContext attribute to true in the <Context/> configuration.

Use the following code to access a repository through the servlet context:

import javax.jcr.Repository;
import javax.servlet.ServletContext;

ServletContext context = ...; // context of your servlet
ServletContext jackrabbit =
    context.getContext("");
Repository repository = (Repository)
    context.getAttribute(Repository.class.getName()).

Using the jackrabbit-jcr-servlet component

The jackrabbit-jcr-servlet component contains utility classes for use within JCR web applications. With that component you can hide both the above and the remote access options from your code, and use just the following to access a repository:

import javax.jcr.Repository;
import org.apache.jackrabbit.servlet.ServletRepository;

public class MyServlet extends HttpServlet {

    private final Repository repository = new ServletRepository(this);

    // ...

}

See the jackrabbit-jcr-servlet documentation for more details.