1

Hello i'm new to hava and i'm having a problem viewing my records from an arraylist in JSP page, whenever i load the page i get: [content.animalBean@1e8614a, content.animalBean@14b52aa, content.animalBean@2026f3, content.animalBean@dd20b6, content.animalBean@18eb00c] 1 which is not the database records here is my code: selectAnimalServlet:

package content;

import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class selectAnimalServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) 
                       throws ServletException, java.io.IOException {

try
{       

    List<animalBean> beans = DAO.selectListAnimal(); 
    request.setAttribute("beans", beans); 
    request.getRequestDispatcher("checkAnimal.jsp").forward(request, response); 


}

catch (Throwable theException)      
{
     System.out.println(theException); 
}
       }
    }

AnimalBean:

package content;

public class animalBean {

    private String animalName;
    private String animalDob;
    private String animalGender;
    private String animalSource;
    private String animalBreed;
    private String animalRemark;

    public String getAnimalName() {return animalName;}
    public String getAnimalDob() {return animalDob;}
    public String getAnimalGender() {return animalGender;}
    public String getAnimalSource() {return animalSource;}
    public String getAnimalBreed() {return animalBreed;}
    public String getAnimalRemark() {return animalRemark;}

    public void setAnimalName(String animalName) {this.animalName = animalName;}
    public void setAnimalDob(String animalDob) {this.animalDob = animalDob;}
    public void setAnimalGender(String animalGender) {this.animalGender = animalGender;}
    public void setAnimalSource(String animalSource) {this.animalSource = animalSource;}
    public void setAnimalBreed(String animalBreed) {this.animalBreed = animalBreed;}
    public void setAnimalRemark(String animalRemark) {this.animalRemark = animalRemark;}


}

DAO class:

package content;


import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class DAO    
{
   static Connection currentCon = null;
   static ResultSet rs = null;  



   public static loginAuth login(loginAuth bean) {

      //preparing some objects for connection 
      Statement stmt = null;    

      String username = bean.getUsername();    
      String password = bean.getPassword();   

      String searchQuery =
            "select * from user where username='"
                     + username
                     + "' AND password='"
                     + password
                     + "'";

   // "System.out.println" prints in the console; Normally used to trace the process
   System.out.println("Your user name is " + username);          
   System.out.println("Your password is " + password);
   System.out.println("Query: "+searchQuery);

   try 
   {
      //connect to DB 
      currentCon = dbConnection.getConnection();
      stmt=currentCon.createStatement();
      rs = stmt.executeQuery(searchQuery);          
      boolean more = rs.next();

      // if user does not exist set the isValid variable to false
      if (!more) 
      {
         System.out.println("Sorry, you are not a registered user! Please sign up first");
         bean.setValid(false);
      } 

      //if user exists set the isValid variable to true
      else if (more) 
      {
         String firstName = rs.getString("FirstName");
         String lastName = rs.getString("LastName");

         System.out.println("Welcome " + firstName);
         bean.setfname(firstName);
         bean.setlname(lastName);
         bean.setValid(true);
      }
   } 

   catch (Exception ex) 
   {
      System.out.println("Log In failed: An Exception has occurred! " + ex);
   } 

   //some exception handling
   finally 
   {
      if (rs != null)   {
         try {
            rs.close();
         } catch (Exception e) {}
            rs = null;
         }

      if (stmt != null) {
         try {
            stmt.close();
         } catch (Exception e) {}
            stmt = null;
         }

      if (currentCon != null) {
         try {
            currentCon.close();
         } catch (Exception e) {
         }

         currentCon = null;
      }
   }

return bean;

   }    




   public static List<animalBean> selectListAnimal() throws SQLException {

       Statement stmt = null;
       List<animalBean> beans = new ArrayList<animalBean>(); 
       try { 
           currentCon = dbConnection.getConnection();
           String animalSearchQuery = "select a.aname ,a.dob,  a.gender , a.source, s.sname, a.remark from animal as a , specie as s where a.specie_id = s.specie_id and a.available ='y'";        
           stmt=currentCon.createStatement(); 
           rs = stmt.executeQuery(animalSearchQuery);       

           while (rs.next()) { 
               animalBean bean = new animalBean();
               bean.setAnimalName(rs.getString("aname"));
               bean.setAnimalDob(rs.getString("dob"));
               bean.setAnimalGender(rs.getString("gender"));
               bean.setAnimalSource(rs.getString("source"));
               bean.setAnimalBreed(rs.getString("sname"));
               bean.setAnimalRemark(rs.getString("remark"));
               beans.add(bean);
                 }


       } finally { 
           if (rs != null) try { rs.close(); } catch (SQLException logOrIgnore) {} 
           if (stmt != null) try { stmt.close(); } catch (SQLException logOrIgnore) {} 
           if (currentCon != null) try { currentCon.close(); } catch (SQLException logOrIgnore) {} 
       } 
       return beans; 


       }



   }

and last the JSP page animalCheck.jsp:

<%@ page language="java" 
         contentType="text/html; charset=windows-1256"
         pageEncoding="windows-1256"
         import="content.animalBean"
         import="content.DAO"




   %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Animal list</title>
<link rel="stylesheet"
      href="./css/styles.css"
      type="text/css"/>
</head>
<body>
<table class="title">
  <tr><th>Zoo keeper</th></tr>
</table>


<h1>Animal list</h1>
 <center>
 <table width="100 % " id='table1'  border="1" cellspacing="2" cellpadding="2"> 
    <tr class="tab-highlighted-2"> 
        <td class="tab-highlighted-2" width="15"> 
          <div align="left">Name</div> 
        </td> 
        <td class="tab-highlighted-2" width="20"> 
          <div align="left">Age</div> 
        </td>
         <td class="tab-highlighted-2" width="15"> 
          <div align="left">Gender</div> 
        </td>
        <td class="tab-highlighted-2" width="15"> 
          <div align="left">Status</div> 
        </td>    
        <td class="tab-highlighted-2" width="15"> 
          <div align="left">Breed</div> 
        </td>  
        <td class="tab-highlighted-2" width="15"> 
          <div align="left">Remarks</div> 
        </td> 

    </tr> 

    <c:forEach items="${beans}" var="view"> 
        <tr> 
            <td>${view.animalName} </td> 
            <td>${view.animalDob}</td>
            <td>${view.animalGender}</td>
            <td>${view.animalSource}</td>
            <td>${view.animalBreed}</td>
            <td>${view.animalRemark}</td>

        </tr> 
    </c:forEach> 
</table> 

         </center>

</body></html>

I've been struggeling on this since 2 days and i checked many websites and followed many guides but still nothing worked for me :( I appreciate any kind of help

user1031152
  • 209
  • 4
  • 10
  • 15
  • Where is your taglib directive in the jsp? Are those not required any more? Also, you don't need the imports. – Dave Newton Nov 05 '11 at 14:20
  • Can you try something like this on top of your page <% List animals = (animalBean)request.getAttribute("beans"); %> and then change your c:forEach tag to point it to animals instead of beans? – r0ast3d Nov 05 '11 at 14:25
  • i'm really new to this so i don't know what is taglib, but if you mean the standard, JSP, JSTL jar files i added them and then the page started to act crazy one time open and the other not so i Ctrz + z – user1031152 Nov 05 '11 at 14:26

2 Answers2

1

You forgot to declare the JSTL core taglib. Add the following to top of your JSP:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

See also:


Unrelated to the concrete problem, there are several other problems in your code:

  • You should never declare DB resources as static. This is not threadsafe and is prone to resource leaking. Declare them inside the very same method block as you're executing the SQL query.
  • You have a SQL injection hole in login() method. Use PreparedStatement.
  • You don't need to use @page import in your JSP if you aren't using any scriptlets.
  • Classnames are supposed to start with uppercase.
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • now i'm receving java.lang.NoClassDefFoundError: javax/servlet/jsp/tagext/TagLibraryValidator after adding jstl-1.2.jar under WEB-INF/lib – user1031152 Nov 05 '11 at 15:26
  • same thing also for jstl-1.1.0.jar – user1031152 Nov 05 '11 at 15:43
  • You seem to already have asked another question for this, so continue there: http://stackoverflow.com/questions/8021370/java-lang-noclassdeffounderror-javax-servlet-jsp-tagext-taglibraryvalidator – BalusC Nov 05 '11 at 20:15
  • thanks for the help now it's working and the problem is solved – user1031152 Nov 06 '11 at 00:11
0

Can you try something like this on top of your page

 <% List<animalBean> animals = (animalBean)request.getAttribute("beans"); %> 

and then change your c:forEach tag to point it to animals instead of beans?

r0ast3d
  • 2,639
  • 1
  • 14
  • 18
  • Multiple annotations found at this line: - Type mismatch: cannot convert from animalBean to List - Type mismatch: cannot convert from animalBean to List this error on (animalBean)request.getAttribute – user1031152 Nov 05 '11 at 14:37
  • -1 for suggesting *scriptlets* while OP is trying use taglibs. – BalusC Nov 05 '11 at 14:55