The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Bean to Jsp problem

2 replies on 1 page. Most recent reply: Nov 6, 2002 9:10 PM by Jay Kandy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Michael Crutcher

Posts: 2
Nickname: mcrutcher
Registered: Nov, 2002

Java Bean to Jsp problem Posted: Nov 6, 2002 10:56 AM
Reply to this message Reply
Advertisement
I'm really new to jsp/servlets, so I might be missing something completely obvious. I apologize for the length. Let me first describe what I'm trying to do.

1. I want a jsp page that contains a number of dynamic list boxes
2. I want these list boxes as paramaters of a java bean by this I mean <jsp:getProperty name='beanName' property = 'member_List'/> should return a string that corresponds to a list with all of the correct values
3. The getProperty methods at the bean should connect to the database, pull all values out of a table, and format the results as a string conforming to the html list requirments

Here's a simple jsp:

<%@page contentType="text/html"%>
<html>
<head><title>List Box Tester</title></head>
<body>
<jsp:useBean id="listbox" scope="page" class="listbox.ListBoxBean"/>
<jsp:getProperty name="listbox" property="member_List"/>
</body>
</html>

Here's a simple getProperty method within the ListBoxBean:
public String getMember_List() {
String output = "";
try {

String url = "jdbc:odbc:****";
String username = "*****";
String password = "*****";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection(url, username, password);
java.sql.Statement statement = connection.createStatement();
String query = "sel mbr_id, mbr_short from tamus.mbr group by 1,2" +
"order by 1;";
ResultSet rs = statement.executeQuery(query);
output += "<select name='member_List'>\n";

while (rs.next()) {
output += "<option value='" + rs.getString(1) + "'>" +
rs.getString(2) + "\n";
}
output += "</select>";

statement.close();
connection.close();

} catch (Exception ex) {
ex.printStackTrace();
}


return output;
}

My problem is that nothing is showing up. The string does not get passed from the bean to the jsp. I'm not sure what the problem is. If anybody could suggest a good way to debug this I'd be most appreciative (Tomcat on Windows 2000). What's wierd is that I wrote a tester class with this main method:

public static void main(String[] args) {
ListBoxBean test = new ListBoxBean();
System.out.println(test.getMember_List());
}

and it prints out the list exactly as it should be formatted.

Am I missing something completely obvious?

A couple of other questions:

1. How can I share the connection within the bean?
2. How can I close the connection when done?

Any help would be greatly appreciated, I'm tearing what little hair I have left out.

Thanks,
Michael Crutcher
Texas A&M University


Michael Crutcher

Posts: 2
Nickname: mcrutcher
Registered: Nov, 2002

Re: Java Bean to Jsp problem Posted: Nov 6, 2002 12:48 PM
Reply to this message Reply
I've found where the program is failing:

connection = DriverManager.getConnection(url, username, password);

Any text directed to the string output before this command is reflected in the html page returned, anything after this within the try-catch block is not. So it's generating an exception. The question, then, is why? An exception isn't thrown when the tester program calls the method, why is one thrown when called from jsp?

Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: Java Bean to Jsp problem Posted: Nov 6, 2002 9:10 PM
Reply to this message Reply
Exception is not necessarily thrown for all "issues". Make sure connection != null. If it is, we have got some other problem. Also check if the driver is available to Tomcat in the classpath. If you would, post the stack trace - we can analyze that.

Flat View: This topic has 2 replies on 1 page
Topic: Networking questions: Previous Topic   Next Topic Topic: Homework Help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use