aspose file tools*
The moose likes Servlets and the fly likes Sending database data from Servlet to JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sending database data from Servlet to JSP" Watch "Sending database data from Servlet to JSP" New topic
Author

Sending database data from Servlet to JSP

Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Hi,
I'm running into a problem. I have a servlet that queries a database and then takes that resultSet and adds it as an attribute to the request and then the servlet forwards this to a jsp page. Inside the jsp I try to extract the data from the resultset, but I keep getting a NullPointerException. I know the getQuery method to get a database query works fine. I suspect there is a problem with me setting the attribute. I was told that I could set anything as an attribute. Here's the relevant code from the servlet:

and here's the relevant code from the jsp:

I keep getting a NullPointerException from the scriptlet above. Am I doing something wrong? Thanks for any help.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30516
    
150

Sean,
You can't access a resultset object after the statement or connection is closed. I would think this would throw a SQLException, but I guess it could throw a null pointer. Try copying the data into a java object (like ArrayList) and accessing it that way.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
As already posted by Jeanne, your resultset probably does not exist at this point. it's not a good idea to pass resultsets around anyway. To verify that you can do...
Object results = (Object)request.getAttribute("key1");
Then check if results is null.
Just use a collection to store your values and then pass that around. You can also encapsulate your results in a class and then store that in the collection.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
As stated above. I would suggest you wrap your playerID in a Transfer Object or Value Object, a collection is good also. For example, I would build the TO or VO in your getQuery(sql) method and pass it back to the calling program.
Craig
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Thanks for all the helpful replies. The main culprit to my problem was that I didn't have the mysql driver in the tomcat/common/ directory. As soon as I put it there, and then used an ArrayList everything worked beautifully. Thanks for all the help.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Sean Casey:
The main culprit to my problem was that I didn't have the mysql driver in the tomcat/common/ directory. As soon as I put it there, and then used an ArrayList everything worked beautifully.

Just to clarify....
The main culprit was the fact that you were trying to pass a ResultSet back to your JSP page and access it there. As was stated, a ResultSet it tied to a connection and without that connection, your ResultSet has no data.
The reason you fixed your problem really has nothing to do with the Driver (unless there were other issues you left out). The fix was putting the data in an ArrayList and passing that to your JSP.
I just didn't want you to be mislead on your solution.
[ January 25, 2004: Message edited by: Gregg Bolinger ]

GenRocket - Experts at Building Test Data
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending database data from Servlet to JSP