My jsp always return null
claudia.pt Dec 21, 2005 9:58 AMHello!!
I need urgent help.
I have a stateless session bean and i'm trying use a servlet+jsp as client.
The method of the bean is:
public Place getPlace(){
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
Place ret = null;
try {
// getConnection()
c = getDataSource().getConnection();
// prepareStatement(sql statement)
ps = c.prepareStatement(Statement.GET_PLACES_STATEMENT);
// executeQuery()
rs = ps.executeQuery();
while (rs.next()){
ret = new Place(rs.getInt(1) , rs.getString(2));
//System.out.println("Id = " + ret.getId() + " Name = " + ret.getName());
}
rs.close();
ps.close();
c.close();
} catch (SQLException se) {
se.printStackTrace();
}
return ret;
} //End of method getPlace()
And the class Place is like this:
public class Place implements java.io.Serializable{
// Attributes
private int Id; // Primary Key
private String Name; // Place Name
// My constructor
public Place(int id, String name){
this.Id = id;
this.Name = name;
}
/**
* Constructor with no arguments
*/
public Place() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return Id;
}
public String getName() {
return Name;
}
}
As result of the invocation of method getPlace() i want in my jsp the list of all places in the table.
My servlet:
public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// Calls EJB
com.samples.TestDataSource test = null;
try{
com.samples.TestDataSourceHome home = com.samples.TestDataSourceUtil.getHome();
test = home.create();
}catch(javax.ejb.CreateException createException) {
System.out.println("CreateException - Couldn't create TestDataSource bean."+ createException.getMessage());
}catch(Exception e) {
e.printStackTrace();
}// test is an EJBObject
// Invoca o método disponivél na interface remota do EJB
//com.samples.model.Place result = test.getPlace();
com.samples.model.Place result = test.getPlace();
request.setAttribute("nameAtt", result);
try{
request.getRequestDispatcher("/testServ.jsp").forward(request, response);
}catch(ServletException se){
System.out.println("Erro no Dispatcher");
}
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
my jsp:
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
Insert title here
<%
//com.samples.model.Place tmp = (com.samples.model.Place)request.getAttribute("nameAtt");
com.samples.model.Place tmp = (com.samples.model.Place) request.getAttribute("nameAtt");
%>
<%=tmp.getName() %>
The problem is that in servlet always appears the last place in the table and in the jsp i get nullPointerException because the object is null.
I can not understand why?!
Please help me.
Thankx, Cláudia