2010年9月24日 星期五

JNDI on GWT 2.0 RPC

I try GWT RPC to connect MySQL with JDBC. I found GWT 2.0 with eclipse uses Jetty 6.0. Followings are my processes and they are working.

1. Create jetty-web.xml at WEB-INF like this.
<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<Set name="war"><SystemProperty name="jetty.home"/>/war</Set>

<Set name="contextPath">/</Set>


<New id="DSTest" class="org.mortbay.jetty.plus.naming.Resource">
   
   <Arg>java:comp/env/jdbc/test</Arg>

   <Arg>

    <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">

    <Set name="Url">jdbc:mysql://localhost:3306/test</Set>

    <Set name="User">user</Set>

    <Set name="Password">password</Set>

    </New>

   </Arg>

  </New>

</Configure>


2. Add resource ref in web.xml like this.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<resource-ref>

<description>DB Connection</description>

<res-ref-name>jdbc/test</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>


</web-app>



3. After 1 and 2, you will failure at class not found if you start your GWT application. It seems GWT not include JNDI jars in Jetty. So, I copy jetty-naming-xxx.jar and jetty-plus-xxx.jar to WEB-INF/lib

4. You can use "java:comp/env/jdbc/test" to get the DataSource.
張貼留言