• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sending database data from Servlet to JSP

 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic