D
dinesh
Hi. I'm trying to call a database access bean through a JSP. (the search
field is irrelevent, it's referencing a controlling servlet) Here's my code:
menu.jsp
--------------------------------
<%@page import="java.util.*"%>
<jsp:useBean id="DbBean" scope="application" class="showMeItNow.DbBean"/>
<%
String base = (String) application.getAttribute("base");
%>
<table width="150" cellpadding="5" height="75" cellspacing="0" border="0">
<tr>
<td>
<form>
<input type="hidden" name="action" value="search">
<input type="text" name="keyword" size="10">
<input type="submit" value="go">
</form>
</td>
</tr>
<tr>
<td>category</td>
</tr>
<tr>
<tr valign="top">
<%
Hashtable categories = DbBean.getCategories();
Enumeration.categoryIds = categories.keys();
while (categoryIds.hasMoreElements()) {
Object categoryId = categoryIds.nextElement();
out.println("<a href=" + base + "? action=browseCatalog&categoryId=" +
categoryId.toString() + ">" + categories.get(categoryId) + "</a><br>");
}
%>
</td>
</tr>
</table>
</body>
</html>
-----------------------------------------------
web.xml
<!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>
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>ControllerServlet</servlet-class>
<init-param>
<param-name>base</param-name>
<param-value>http://localhost:7001/BibleApp/</param-value>
</init-param>
<init-param>
<param-name>imageURL</param-name>
<param-value>http://localhost:7001/BibleApp/</param-value>
</init-param>
<init-param>
<param-name>dbURL</param-name>
<param-value>jdbc:weblogic:mssqlserver4:users@COMPAQSERVER</param-value>
</init-param>
<init-param>
<param-name>usernameDbConn</param-name>
<param-value>dinesh</param-value>
</init-param>
<init-param>
<param-name>passwordDbConn</param-name>
<param-value>thisisthepassword</param-value>
</init-param>
</servlet>
</web-app>
--------------------------------------------
DbBean.class
--------------
package showMeItNow;
import java.util.*;
import java.sql.*;
import showMeItNow.Poll;
public class DbBean {
public String dbUrl="";
public String dbUserName="";
public String dbPassword="";
public void setDbUrl(String url) {
dbUrl=url;
}
public void setDbUserName(String userName) {
dbUserName=userName;
}
public void setDbPassword(String password) {
dbPassword=password;
}
public Hashtable getCategories() {
Hashtable categories = new Hashtable();
try
{
Connection connection = DriverManager.getConnection(dbUrl, dbUserName,
dbPassword);
CallableStatement cstmt = connection.prepareCall("{? = call
dbo.categoryListing()}");
//register the stored procedure's output paramater!!!
cstmt.registerOutParameter(1, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
ResultSet rs = cstmt.executeQuery();
while (rs.next()) {
categories.put(rs.getString(1), rs.getString(2) );
}
rs.close();
cstmt.close();
connection.close();
}
catch (SQLException e) { }
return categories;
}
}
field is irrelevent, it's referencing a controlling servlet) Here's my code:
menu.jsp
--------------------------------
<%@page import="java.util.*"%>
<jsp:useBean id="DbBean" scope="application" class="showMeItNow.DbBean"/>
<%
String base = (String) application.getAttribute("base");
%>
<table width="150" cellpadding="5" height="75" cellspacing="0" border="0">
<tr>
<td>
<form>
<input type="hidden" name="action" value="search">
<input type="text" name="keyword" size="10">
<input type="submit" value="go">
</form>
</td>
</tr>
<tr>
<td>category</td>
</tr>
<tr>
<tr valign="top">
<%
Hashtable categories = DbBean.getCategories();
Enumeration.categoryIds = categories.keys();
while (categoryIds.hasMoreElements()) {
Object categoryId = categoryIds.nextElement();
out.println("<a href=" + base + "? action=browseCatalog&categoryId=" +
categoryId.toString() + ">" + categories.get(categoryId) + "</a><br>");
}
%>
</td>
</tr>
</table>
</body>
</html>
-----------------------------------------------
web.xml
<!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>
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>ControllerServlet</servlet-class>
<init-param>
<param-name>base</param-name>
<param-value>http://localhost:7001/BibleApp/</param-value>
</init-param>
<init-param>
<param-name>imageURL</param-name>
<param-value>http://localhost:7001/BibleApp/</param-value>
</init-param>
<init-param>
<param-name>dbURL</param-name>
<param-value>jdbc:weblogic:mssqlserver4:users@COMPAQSERVER</param-value>
</init-param>
<init-param>
<param-name>usernameDbConn</param-name>
<param-value>dinesh</param-value>
</init-param>
<init-param>
<param-name>passwordDbConn</param-name>
<param-value>thisisthepassword</param-value>
</init-param>
</servlet>
</web-app>
--------------------------------------------
DbBean.class
--------------
package showMeItNow;
import java.util.*;
import java.sql.*;
import showMeItNow.Poll;
public class DbBean {
public String dbUrl="";
public String dbUserName="";
public String dbPassword="";
public void setDbUrl(String url) {
dbUrl=url;
}
public void setDbUserName(String userName) {
dbUserName=userName;
}
public void setDbPassword(String password) {
dbPassword=password;
}
public Hashtable getCategories() {
Hashtable categories = new Hashtable();
try
{
Connection connection = DriverManager.getConnection(dbUrl, dbUserName,
dbPassword);
CallableStatement cstmt = connection.prepareCall("{? = call
dbo.categoryListing()}");
//register the stored procedure's output paramater!!!
cstmt.registerOutParameter(1, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
ResultSet rs = cstmt.executeQuery();
while (rs.next()) {
categories.put(rs.getString(1), rs.getString(2) );
}
rs.close();
cstmt.close();
connection.close();
}
catch (SQLException e) { }
return categories;
}
}