Resources
To configure your servlet for the web container's environment, declare required resources with the @Resource annotation in the servlet. The container will inject those resources right after instantiation (but before init is called).
The deployer is responsible for configuring the web container to provide the values required by the application. The configuration itself is container-specific.
The following Servlet declares a requirement for a String and a JDBC DataSource:
package com.jarfiller.example;
import javax.sql.*;
import java.sql.*;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.annotation.*;
import java.io.*;
public class ResourceServlet extends HttpServlet {
@Resource(name="jdbc/db")
private DataSource db; // inject database connection (more)
@Resource(name="param/webmaster")
private String webmaster; // inject string (more)
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
Connection connection = db.getConnection(); // use injected value
// do something ...
}
catch (SQLException e) {
throw new ServletException(e); // simple error handling (more)
}
}
}
How to get resources with JNDI
Instead of declaring resources with JNDI, you can also look them up manually using the JNDI API. This was also the only way to get resources in Servlet 2.4 API and earlier.
In order to look up resources, you need to declare them in your web.xml like this:
<env-entry> <description>Name of the webmaster</description> <env-entry-name>param/webmaster</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> </env-entry> <resource-ref> <description>A DataSource for our database</description> <res-ref-name>jdbc/db</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
As with @Resource annotations, the deployer must provide values for those resources by configuring a web container (in a container specific way).
With the resources being declared, you can use JNDI to look them up:
import javax.naming.*;
import javax.sql.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class JNDIServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
Context ic = new InitialContext();
Context ctx = (Context) ic.lookup("java:comp/env"); // enter ENC (more)
String wm = (String) ctx.lookup("param/webmaster"); // lookup string (more)
DataSource db = (DataSource) ctx.lookup("jdbc/db"); // lookup DataSource (more)
// do something ...
}
catch (NamingException e) {
throw new ServletException(e); // simple error handling (more)
}
}
}
How to Provide Resources in Tomcat
In Tomcat, resources are configured in files called context.xml. Global resources are defined in $TOMCAT_HOME/conf/context.xml. Web applications can add additional resources by putting a file /META-INF/context.xml into the WAR.
The following example context.xml defines resources for the last two examples. It can be either placed in the WAR's /META-INF/context.xml or at $TOMCAT_HOME/conf/context.xml.
<Context> <Environment name="param/webmaster" value="tim@jarfiller.com" type="java.lang.String" override="false"/> <!-- required for web.xml version (more) --> <Resource name="jdbc/db" auth="Container" type="javax.sql.DataSource" driverClassName="org.apache.derby.jdbc.EmbeddedDriver" url="jdbc:derby:c:\tmp\JarfillerDB;create=true"/> </Context>
For more details read Tomcat's context documentation.

