i'm running tomcat5.0 mysql4.0
in my applications deployment descriptor i've got:
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>ControllerServlet</servlet-class>
<!-- Define initial parameters that will be loaded into
the Servlet Context object in the controller servlet -->
<init-param>
<param-name>base</param-name>
<param-value> </init-param>
<init-param>
<param-name>jdbcDriver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>imageUrl</param-name>
<param-value> </init-param>
<init-param>
<param-name>dbUrl</param-name>
<param-value>jdbc:mysql://localhost/Foode</param-value>
</init-param>
<init-param>
<param-name>dbUserName</param-name>
<param-value>****</param-value>
</init-param>
<init-param>
<param-name>dbPassword</param-name>
<param-value>******</param-value>
</init-param>
</servlet>
i have a bean called dbBean that contains all the methods used by the presentation pages and a servlet that receives and fowards requests to the jsp pages.
my default.jsp page contains a menu.jsp that retrieves the elements from the database. however, when i load the page, the elements do not appear. menu.jsp page compiles but i can't open the connection. this is a portion of the code from the bean:
public class DbBean {
public String dbUrl = "";
public String dbUserName = "";
public String dbPassword = "";
public void setDbUrl(String url){
dbUrl = url;
}
public void setDbUserName(String user){
dbUserName = user;
}
public void setDbPassword(String password){
dbPassword = password;
}
public Hashtable getCategories(){
Hashtable categories = new Hashtable();
try {
Connection conn = DriverManager.getConnection(dbUrl);
Statement s = conn.createStatement();
String sql = "Select CategoryId, Category From Categories" + " ";
ResultSet rs = s.executeQuery(sql);
while (rs.next()){
categories.put(rs.getString(1), rs.getString(2));
}
rs.close();
s.close();
conn.close();
}
catch (SQLException e){}
return categories;
}
and the following is the initializing part of the controller servlet:
/* Initialize global variables */
public void init (ServletConfig config) throws ServletException {
System.out.println("initializing controller servlet.");
ServletContext context = config.getServletContext();
context.setAttribute("base", config.getInitParameter("base"));
context.setAttribute("imageUrl", config.getInitParameter("imageUrl"));
//instantiating the DbBean
DbBean dbBean = new DbBean();
//initializing the DbBean's variables
dbBean.setDbUrl(config.getInitParameter("dbUrl"));
dbBean.setDbUserName(config.getInitParameter("dbUserName"));
dbBean.setDbPassword(config.getInitParameter("dbPassword"));
//put the bean in the servlet context
//the bean will be accessed from the jsp
context.setAttribute("dbBean", dbBean);
try {
//loading the database driver
Class.forName(config.getInitParameter("jdbcDriver"));
}
catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
super.init(config);
}
any help would be appreciated.
in my applications deployment descriptor i've got:
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>ControllerServlet</servlet-class>
<!-- Define initial parameters that will be loaded into
the Servlet Context object in the controller servlet -->
<init-param>
<param-name>base</param-name>
<param-value> </init-param>
<init-param>
<param-name>jdbcDriver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>imageUrl</param-name>
<param-value> </init-param>
<init-param>
<param-name>dbUrl</param-name>
<param-value>jdbc:mysql://localhost/Foode</param-value>
</init-param>
<init-param>
<param-name>dbUserName</param-name>
<param-value>****</param-value>
</init-param>
<init-param>
<param-name>dbPassword</param-name>
<param-value>******</param-value>
</init-param>
</servlet>
i have a bean called dbBean that contains all the methods used by the presentation pages and a servlet that receives and fowards requests to the jsp pages.
my default.jsp page contains a menu.jsp that retrieves the elements from the database. however, when i load the page, the elements do not appear. menu.jsp page compiles but i can't open the connection. this is a portion of the code from the bean:
public class DbBean {
public String dbUrl = "";
public String dbUserName = "";
public String dbPassword = "";
public void setDbUrl(String url){
dbUrl = url;
}
public void setDbUserName(String user){
dbUserName = user;
}
public void setDbPassword(String password){
dbPassword = password;
}
public Hashtable getCategories(){
Hashtable categories = new Hashtable();
try {
Connection conn = DriverManager.getConnection(dbUrl);
Statement s = conn.createStatement();
String sql = "Select CategoryId, Category From Categories" + " ";
ResultSet rs = s.executeQuery(sql);
while (rs.next()){
categories.put(rs.getString(1), rs.getString(2));
}
rs.close();
s.close();
conn.close();
}
catch (SQLException e){}
return categories;
}
and the following is the initializing part of the controller servlet:
/* Initialize global variables */
public void init (ServletConfig config) throws ServletException {
System.out.println("initializing controller servlet.");
ServletContext context = config.getServletContext();
context.setAttribute("base", config.getInitParameter("base"));
context.setAttribute("imageUrl", config.getInitParameter("imageUrl"));
//instantiating the DbBean
DbBean dbBean = new DbBean();
//initializing the DbBean's variables
dbBean.setDbUrl(config.getInitParameter("dbUrl"));
dbBean.setDbUserName(config.getInitParameter("dbUserName"));
dbBean.setDbPassword(config.getInitParameter("dbPassword"));
//put the bean in the servlet context
//the bean will be accessed from the jsp
context.setAttribute("dbBean", dbBean);
try {
//loading the database driver
Class.forName(config.getInitParameter("jdbcDriver"));
}
catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
super.init(config);
}
any help would be appreciated.