how to handle ResultSet from jsp?

Hi there,

I use a servlet to query the db and put the result into a Vector v of beans(each bean represents one row in the ResultSet). The Vector v is then put in the request by setAttribute() and forward to the display.jsp. In the display.jsp, I tried

<%@page import="java.util.*,Financial.TransactionBean" contentType="text/html"%>

<%@page pageEncoding="UTF-8"%>

<jsp:useBean id="transaction" class="Financial.TransactionBean" scope="session"/>

//cut off...

<body>

<ul>

<%

Vector v = (Vector)request.getAttribute("list");

Iterator i = v.iterator();

while (i.hasNext())

{

transaction = (TransactionBean)i.next();

%>

<li>

<%-- <a href="CORE/Financial/FinancialServlet?cmd=get&id= --%>

<a>

<jsp:getProperty name="transaction" property="id" /> ">

<jsp:getProperty name="transaction" property="day" />

<jsp:getProperty name="transaction" property="amount" />

</a>

<% } %>

</ul>

//cut off....

I though for every bean in the Vector, i can extract it's properties but I got an error :

Cannot find any information on property 'id' in a bean of type 'Financial.TransactionBean'

What can possibly go wrong?

[1386 byte] By [lnthai2002a] at [2007-11-27 5:14:47]
# 1
your bean has no method "getId()" most likely.
gimbal2a at 2007-7-12 10:36:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

You r right, the getID() wasnt recognized as accessor it should be getId() - the capitalized "D" is the source of error.

However, after i change it, it doesnt seem that the bean (null) i create in the display.jsp file is referenced to the actual bean objectin the Vector as instructed in the <%%> tags. Thus, using <jsp:getProperty name="transaction" property="day" /> always give null.

Is there any other way to send the resultSet to jsp and display all record in it?

I was thinking about creating a list bean which contains many bean, each coresponds to a record and overwrite the toString() method for the list bean so that it return a well format of all record. However, this way doesnt give me the flexibility of formating data inside the bean.

Hope anyone have a suggestion

lnthai2002a at 2007-7-12 10:36:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

how about dumping the very aged scriptlets and switching to the use of JSTL? JSTL has an "sql" tag library with which you can work with database queries in a JSP, if you really want to do that.

For example, check this link:

http://www.ibm.com/developerworks/java/library/j-jstl0520/

gimbal2a at 2007-7-12 10:36:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

But i wanna try servlet centric design. Everything has to go to the servlet, then sharing data should be encapsulated in bean and send to jsp to display. I dont want any jsp contain any business code including db access. The code above is copied directly from the boo : "web development with javaserver pages". Is there any reason that action script scriptlet <%%> can not access bean in action script <jsp:useBean>? I did tried to print all beans in the vector v within the scriptlet, they do contain data, just that the bean is not linked to the real object. Is there anyway to fix it?

lnthai2002a at 2007-7-12 10:36:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Servlet centric design is fine, and in fact preferred.

I would probably use a List (ArrayList) rather than a Vector but thats really just semantics. They are the same thing in effect, but ArrayList is "newer" and "better"

Your mistake in the page is mixing up scriptlet variables with <jsp:property> tags.

You have declared a scriptlet variable "transaction"

This has no direct relationship to a bean "transaction" - you can't access it directly via the <jsp:getProperty> tags.

If you want to retain the scriptlet code, then use

<%= transaction.getId() %> rather than <jsp:getProperty> tag for it.

The preferable way is to use JSTL <c:forEach> loop to eliminate the scriptlet code:

<body>

<ul>

<c:forEach var="transaction" items="${list}">

<a href="CORE/Financial/FinancialServlet?cmd=get&id=${transaction.id}">

${transaction.id} ${transaction.day} ${transaction.amount}

</a>

</c:forEach>

</ul>

...

evnafetsa at 2007-7-12 10:36:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...