File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending database data from Servlet to JSP

 
Sean Casey
Ranch Hand
Posts: 625
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 33674
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Craig Jackson
Ranch Hand
Posts: 405
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 625
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic