File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Bean not working with JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Bean not working with JSP" Watch "Bean not working with JSP" New topic

Bean not working with JSP

Charessa Reilly
Ranch Hand

Joined: May 26, 2011
Posts: 39
OK, maybe I'm oversharing, but I want to be thorough. Note I asked this question a different way using very different code.

Here's my jsp file - myq.jsp

Here's displayCollection.tag used by that jsp:

Here's the java class IrCollection (used as a bean in the tag):

and here's the InquireRecord java class used by IrCollection:

The JSP does this: set the mgr variable, which is passes to the tag, the tag then creates an instance of IrCollection using that mgr variable. (Yes, putting that populateCollection() method call in the setMgrid() method is probably Bad Practice, but it works, usually). The IrCollection objects builds an ArrayList of InquireCollection objects from an Access database. It then sets it's size property based on how many InquireCollection instances it put into the ArrayList. Once that's all done, the tag spits out 2 things: The size property and the mgrid property.

When I view the JSP, it gives me 0 for the size and Chris Novish for the mgrid.

I think this could be one of the following:
  • Not finding any matching records of the database
  • Not actually executing the populateCollection() method
  • some how forgetting the information it put into that ArrayList?

  • I"m sure there's another possibility, but I don't know.

    Here's what gets me. Here's a test class I made called TestCollection:

    if I run that I get a size of 4 and a mgrid of Chris Novish.

    Same data in, and it works as expected.

    So... why won't JSP do it?
    Paul Clapham

    Joined: Oct 14, 2005
    Posts: 19973

    You set the mgrid, then you do some JDBC stuff, then you set the size. So if you see mgrid being set but you don't see size being set, my guess is that some exception is thrown and then ignored between the two steps. Check wherever System.out is diverted to and look for stack traces there.
    Mike Zal
    Ranch Hand

    Joined: May 04, 2011
    Posts: 144

    Your code never gets to perform the database execution. Here is what is happening in your code.

    You call the tag file with mgr attribute value set as "Chris Novish"
    Inside the tag file, the useBean tag creates a session scope bean with default values (zero length array, size = 0, and mgrid = null)
    The setProperty tag sets the value of the bean to mgrid to "Chris Novish"
    Immediately after, you print the size and mgrid properties which value the values 0 and "Chris Novish"

    Also, there is no reason to save the length of the array as an instance variables, just has getSize return the length of the iRecords array.

    Another big no-no, you should not have public instance variables. You should always follow the rules of encapsulation.

    Charessa Reilly
    Ranch Hand

    Joined: May 26, 2011
    Posts: 39
    The setter for mgrid triggers the populateCollection() method which fills the ArrayList.
    I agree. Here's the link:
    subject: Bean not working with JSP
    It's not a secret anymore!