My Listener Code is as follows
package org.javaimplant.newsfeed.Init;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.javaimplant.newsfeed.Servlets.DataSourceServlet;
import org.javaimplant.newsfeed.data.DataAccessObject;
public class Init implements ServletContextListener {
private Logger logger=Logger.getLogger(this.getClass());
public void contextInitialized(ServletContextEvent sce)
{
ServletContext servletContext = sce.getServletContext();
try {
contextInitialized2(servletContext);
}
catch (Exception e)
{
logger.error("Initialization failed.", e);
throw new RuntimeException(e);
}
logger.debug("Initialization succeeded.");
}
private void contextInitialized2(ServletContext servletContext)
throws Exception
{
InitialContext enc = new InitialContext();
NamingEnumeration<NameClassPair> list = enc.list("java:comp/env/datasource");
while (list.hasMore())
{
System.out.println(list.next().getName());
}
Context compContext = (Context) enc.lookup("java:comp/env");
DataSource dataSource = (DataSource) compContext.lookup("datasource");
// DataAccessObject.setDatasource(dataSource);
DataSourceServlet.setDataSource(dataSource);
}
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
}
My context file is in project root which is as follows
<Context path="/newsfeed" docBase="D:\My Projects\springmvc\newsfeed\src\main\webapp">
<Resource name="datasource"
type="javax.sql.DataSource"
auth="Container"
maxActive="10"
maxIdle="3"
maxWait="10000"
username="gaurav"
password="gaurav"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/publisher?autoReconnect=true" />
</Context>
Web.xml is as follows
<!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>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>org.javaimplant.newsfeed.Init.Init</listener-class>
</listener>
<servlet>
<servlet-name>home</servlet-name>
<servlet-class>org.javaimplant.newsfeed.Servlets.HomeServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>data</servlet-name>
<servlet-class>org.javaimplant.newsfeed.Servlets.DataSourceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>data</servlet-name>
<url-pattern>/data</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<resource-ref>
<description>dataSource</description>
<res-ref-name>datasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
I am getting the following error when I run the code.I don't know why my application is unable to read the context from the xml file.Kindly help
javax.naming.NamingException: Name is not bound to a Context at org.javaimplant.newsfeed.Init.Init.contextInitialized(Init.java:29)
Ok so Eclipse maintains its own separate server configuration in workspace you have to make changes there