Can you please post the contents of CDViewer.displayAll() method. I would be interested in seeing how you are getting a session from the session factory. Are you passing your own connection object?
CDViewer.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class CDViewer extends HttpServlet {
private SessionFactory sessionFactory;
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
sessionFactory = new Configuration().addClass(CD.class).buildSessionFactory();
} catch(Exception e) {
e.printStackTrace();
}
}
private void displayAll(PrintWriter out, Session session) {
List cds;
try {
out.println("<html>");
out.println("<table border='1'>");
out.println("<tr><td>Title</td><td>Artist</td><td>cost</td></tr>");
cds = session.find("from CD");
Iterator iter = cds.iterator();
while (iter.hasNext()) {
CD cd = (CD)iter.next();
out.println("<tr><td>");
out.println(cd.getTitle());
out.println("</td><td>");
out.println(cd.getArtist());
out.println("</td><td>");
out.println(cd.getCost());
out.println("</td></tr>");
}
} catch(Exception e)
{
e.printStackTrace();
}
out.println("</table>");
out.println("</html>");
out.println("hello");
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response. getWriter();
Session session = null;
try {
session = sessionFactory.openSession();
String action = request.getParameter("submit");
if (action.equals("Pull All CDs")) {
displayAll(out, session);
} else {
out.println("Bad Input");
}
session.flush();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}
And one more question, does this work if you replace the xml file with the properties file?
Yes , it does .
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">test</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/products
</property>
<property name="hibernate.connection.driver">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<mapping resource="CD.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hibernate.properties
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql:///products
hibernate.connection.username root
hibernate.connection.password test