Good Evening,
I am literally about to pull my hair out.
Here is my problem:
The web application that I created had been functioning correctly up until the point that I changed the Connection object from a DriverManager to a DataSource. I am 100% positive that the problem has to do with how the datasource is set-up in my servlet class and/or how the listener structured in the web.xml file.
I have been at this for several hours and I have exhausted all of different ways to debug this problem. It seems to be fairly simple issue to resolve, but I cannot see what the problem really is.
Here are my steps took to create the problem.
1.Initially in my code, there is a servlet class named NewsFeedServlet.java. This servlet is responsible for collecting the news feeds from the news.rss xml file and to send it back to the jsp file named home.jsp to display the information.
2. I used a DriverManager to get the news feeds from the MySQL database. Here is the sample of the code:
3. I deployed the web application and had not errors. I was able to type in the url and see the link to the news feeds.Connection connection = DriverManager.getConnection( "jdbc:mysql://localhost/publisher", "publisher", "publisher");
4. I modified the NewsFeedServlet.java to change the DriverManager to a DataSource is a can use a Connection Pool. Here is a sample of what included in and modified:
private static DataSource dataSource; Connection connection = dataSource.getConnection(); public static void setDataSource(DataSource dataSource) { NewsFeedServlet.dataSource = dataSource; }
5. Added a class named Init.java that implements ServletContentListener
package publisher.web; import javax.naming.Context; import javax.naming.InitialContext; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.sql.DataSource; import org.apache.log4j.Logger; public class Init implements ServletContextListener { private Logger logger = Logger.getLogger(this.getClass()); public void contextDestroyed(ServletContextEvent sce) { } private void contextInitialized2(ServletContext servletContext) throws Exception { InitialContext enc = new InitialContext(); Context compContext = (Context) enc.lookup("java:comp/env"); DataSource dataSource = (DataSource) compContext.lookup("dataSource"); NewsFeedServlet.setDataSource(dataSource); } 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."); } }
6. Added a <listener > element to the web.xml file
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>news-feed</servlet-name> <servlet-class>publisher.web.NewsFeedServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>news-feed</servlet-name> <url-pattern>/news.rss</url-pattern> </servlet-mapping> <listener> <listener-class>publisher.web.Init</listener-class> </listener> </web-app>
7. Added a file publisher.xml to configure Tomcat to create a datasource
<Context path="/publisher" docBase="C:/TestWebApps/publisher/web"> <Resource name="datasource" type="javax.sql.DataSource" auth="Container" maxActive="10" maxIdle="3" maxWait="10000" username="publisher" password="publisher" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/publisher?autoReconnect=true" /> </Context>
And the completes my steps. Now the error message I am receiving after I try to deploy the web application:
java.io.FileNotFoundException: http://localhost:8080/publisher/web/news.rss sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) com.sun.syndication.io.XmlReader.<init>(XmlReader.java:237) com.sun.syndication.io.XmlReader.<init>(XmlReader.java:213) website1.web.HomeServlet.doGet(HomeServlet.java:44) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
This seems to be telling me that file news.rss file cannot be found. To repeat myself again, before I made the changes, I did not any errors. But as soon as a changed the necessary changes to configure Tomcat to use the DataSource instead of DriverManager, I received these error message.
Please provide me with any thoughts of how I can modify my code. I have attached the files for your review
Thanks in advance for you help. I truly appreciate it.