aspose file tools*
The moose likes JSP and the fly likes Returning ResultSet from servlet to jsp ->NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Returning ResultSet from servlet to jsp ->NullPointerException" Watch "Returning ResultSet from servlet to jsp ->NullPointerException" New topic
Author

Returning ResultSet from servlet to jsp ->NullPointerException

Aditya Dhekney
Greenhorn

Joined: Jan 06, 2004
Posts: 7
Hey all, i've been stuck on this for too long now...just trying to return a ResultSet from a servlet to jsp page.
Had a bunch of problems earlier...which i think were fixed but...now i get a "java.lang.NullPointerException" in my jsp page when i try to get elements from the ResultSet object.
Here is the latest version of my code:
---------------------------------------------------------------------------------
Servlet:
String QueryStr="select ProdName from products";
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(QueryStr); //get resultset
sbean.setInventory(rs); //set ResultSet in bean
req.getSession(true).setAttribute("s_resbean",sbean); //create session/request variable, set to bean
---------------------------------------------------------------------------------
Bean:
package beans;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import javax.sql.*;
public class SearchBean extends HttpServlet{
private int searchFlag=0;
private ResultSet inventory;
public SearchBean(){
}
public int getSearchFlag(){
return searchFlag;
}
public ResultSet getInventory(){
return inventory;
}
public void setInventory(ResultSet rs){
this.inventory=rs;
}
public void setSearchFlag(){
this.searchFlag=1;
}
}
--------------------------------------------------------------------------------------------------
jsp:
<%@ page language="java" import="java.lang.*,java.sql.*,javax.sql.*,PopLists.PopInvLists,beans.SearchBean"%>
<jsp:useBean scope="session" id="s_resbean" class="beans.SearchBean" />
...
<% ResultSet categories=PopInvLists.getCat();
ResultSet manuf=PopInvLists.getManuf();
ResultSet supplier=PopInvLists.getSupplier();
ResultSet cars=PopInvLists.getCars();
ResultSet search=(ResultSet)request.getAttribute("s_resbean");
%>
...
<% while(search.next()){
String pname=search.getString("ProdName");
}
%>
---------------------------------------------------------------------------------------------
It craps out when i try to loop through the "search" ResultSet.
I can loop through the rest of the ResultSets no problem....just this one doesn't work because it's set in a servlet, not a simple java class.
Just to clarify, i am populating some dropdown lists on entry to the screen, which the user will use to perform a search. Once the search btn is clicked, the servlet is called, gets the request info for the search, performs search, and returns the resultset to the original screen. I want to eventually display the result under the search criteria.
Someone....Please Please please tell me how to get this working...it should be very simple, but i just can't get it to work.
Thanks in advance,
Aditya
P.S.
Also, if i try:
ResultSet search=(ResultSet)session.getAttribute("s_resbean");
in the jsp page, i get a "java.lang.ClassCastException"
What exactly should i be doing to get this ResultSet back to my jsp page?
Amit More
Greenhorn

Joined: Oct 16, 2003
Posts: 24
Hi Aditya,
Can you provide the code for forwarding your request from servlet to jsp.
Because it does matter.
Are u using RequestDispatcher object or something different?
Let me know.


Thanks,<br />Amit
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You're writing an 's_resbean', which is a beans.SearchBean then you are trying to read it back as a ResultSet.
Can I say I hat ethe idea of passing database resources around in web-applications. You should have a level below which database operations exist, and not allow knowledge about the database to leak above this layer. Therefore you'd be better off convertind from database fields to custom Java objects as quickly as possible and forget about passing database objects around.
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

also, you're writing it to the session then reading it from the request.

Dave
Aditya Dhekney
Greenhorn

Joined: Jan 06, 2004
Posts: 7
Originally posted by Amit More:
Hi Aditya,
Can you provide the code for forwarding your request from servlet to jsp.
Because it does matter.
Are u using RequestDispatcher object or something different?
Let me know.

RequestDispatcher dispatcher=getServletContext().getRequestDispatcher(url);
dispatcher.forward(req,res);
Aditya Dhekney
Greenhorn

Joined: Jan 06, 2004
Posts: 7
Originally posted by David O'Meara:
You're writing an 's_resbean', which is a beans.SearchBean then you are trying to read it back as a ResultSet.
Can I say I hat ethe idea of passing database resources around in web-applications. You should have a level below which database operations exist, and not allow knowledge about the database to leak above this layer. Therefore you'd be better off convertind from database fields to custom Java objects as quickly as possible and forget about passing database objects around.
Dave

Ok...i'll try and put it in an ArrayList first...i'm assuming i send this back to the jsp page the same way i am the ResultSet. Is this true? I.E. set a bean, get info from bean in jsp.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning ResultSet from servlet to jsp ->NullPointerException