2 Replies Latest reply on Dec 22, 2005 7:28 AM by claudia.pt

    My jsp always return null

    claudia.pt

      Hello!!
      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

        • 1. Re: My jsp always return null
          claudia.pt

          I think the problem is in method getPlace because is returning an objecto of type Place instead of a List object...

          • 2. Re: My jsp always return NullPointerException
            claudia.pt

            OK!!
            My servlet works.
            The problem was in method getPlace that was returning a Place instead a List.
            But i still have the JSP problem: NullPointerException.
            It seams that servlet cannot pass the List to the JSP.
            In Servlet I have:

            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub

            // HttpSession session = request.getSession(false);



            // 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
            java.util.List result = test.getPlace();
            request.setAttribute("nameAtt", result);

            try{
            request.getRequestDispatcher("/testServ.jsp").forward(request, response);
            }catch(ServletException se){
            System.out.println("Erro no Dispatcher");
            }

            }


            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub

            doGet(request, response);

            }

            In JSP I have:
            <%
            java.util.List result = (java.util.List) request.getAttribute("nameAtt");


            for (java.util.Iterator iter = result.iterator(); iter.hasNext();)
            { com.samples.model.Place place = (com.samples.model.Place) iter.next();
            out.println("Sitio: " + place.getName() ); }


            %>

            What is wrong??

            Thankx,
            Cláudia