Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Embedded jetty7 with annotations

Hi,

I'm using the following code to run jetty:

public class JettyServer {
private final static String PATH = System.getProperty("user.dir");

public static void main(String[] args) throws Exception {
System.setProperty("jetty.home", PATH);

Server server = new Server(8080);

ContextHandlerCollection contexts = new ContextHandlerCollection();
server.setHandler(contexts);

DeploymentManager dm = new DeploymentManager();
dm.setContexts(contexts);
server.addBean(dm);

ContextProvider provider = new ContextProvider();
provider.setMonitoredDirName(PATH + "/contexts");
provider.setScanInterval(1);
dm.addAppProvider(provider);

server.setStopAtShutdown(true);
server.start();
server.join();
   }
}

And I have my servlet defined as:

public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private static final String DATA_SOURCE = "jdbc/webdocs";

@Resource(name = DATA_SOURCE)
private DataSource ds1;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
Context ctx = new InitialContext();
DataSource ds2 = (DataSource) ctx.lookup(DATA_SOURCE);

PrintWriter w = resp.getWriter();
w.println("ds1: " + ds1);
w.println("ds2: " + ds2);
} catch (NamingException e) {
throw new ServletException(e);
}
}
}

And the jetty-web.xml file has the following:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
      <New id="webdocs" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/webdocs</Arg>
<Arg>
<New class="oracle.jdbc.pool.OracleDataSource">
<Set name="URL">jdbc:oracle:thin:@localhost:1521:integra</Set>
<Set name="User">publicar_dev</Set>
<Set name="Password">????</Set>
</New>
</Arg>
</New>
</Configure>

Now, my problem is that when I go to http://localhost:8080/hello I get:

ds1: null
ds2: oracle.jdbc.pool.OracleDataSource@94af2f

In this case the @Resource (name = data_source) is not working. 
I have jetty-annotations in my class path and I'm using jetty-7.2.2.v20101205 for Linux

I have used this approach with Tomcat and Glassfish with great success, but I'm not getting the same with the jetty.

Thanks.
--
Filipe Sousa

Back to the top