0

I am currently trying to display a list of property from the database. I have auto generated the entity classes from the database using the tool support. I created the DAO class to access the entity class. I used the servlet to access the DAO and tried to set attribute to the result returned from the DAO class and when I try to use JSTL foreach statement to loop through that attribute from JSP page, it doesn't work. Here is my code:

Property Entity:

@Entity
@Table(name = "PROPERTY")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Property.findAll", query = "SELECT p FROM Property p")})
public class Property implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "PROPERTYID")
    private Integer propertyid;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 500)
    @Column(name = "DESCRIPTION")
    private String description;
    @Size(max = 50)
    @Column(name = "TYPE")
    private String type;
    @Column(name = "NUMBEROFBEDROOM")
    private Integer numberofbedroom;
    @Column(name = "NUMBEROFBATHROOM")
    private Integer numberofbathroom;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "ISFURNISHED")
    private String isfurnished;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "HASGARDEN")
    private String hasgarden;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "SIZE")
    private String size;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "PRICE")
    private String price;
    @Basic(optional = false)
    @NotNull
    @Column(name = "ENTEREDDATE")
    @Temporal(TemporalType.DATE)
    private Date entereddate;
    @Basic(optional = false)
    @NotNull
    @Column(name = "ENTEREDTIME")
    @Temporal(TemporalType.TIME)
    private Date enteredtime;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "property")
    private Paddress paddress;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "propertyid")
    private Collection<Offer> offerCollection;
    @JoinColumn(name = "PROPERTYOWNERID", referencedColumnName = "PROPERTYOWNERID")
    @ManyToOne(optional = false)
    private Propertyowner propertyownerid;
    @JoinColumn(name = "REALESTATEAGENTID", referencedColumnName = "REALESTATEAGENTID")
    @ManyToOne(optional = false)
    private Realestateagent realestateagentid;

    public Property() {
    }

    public Property(Integer propertyid) {
        this.propertyid = propertyid;
    }

    public Property(Integer propertyid, String description, String isfurnished, String hasgarden, String size, String price, Date entereddate, Date enteredtime) {
        this.propertyid = propertyid;
        this.description = description;
        this.isfurnished = isfurnished;
        this.hasgarden = hasgarden;
        this.size = size;
        this.price = price;
        this.entereddate = entereddate;
        this.enteredtime = enteredtime;
    }

    public Integer getPropertyid() {
        return propertyid;
    }

    public void setPropertyid(Integer propertyid) {
        this.propertyid = propertyid;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Integer getNumberofbedroom() {
        return numberofbedroom;
    }

    public void setNumberofbedroom(Integer numberofbedroom) {
        this.numberofbedroom = numberofbedroom;
    }

    public Integer getNumberofbathroom() {
        return numberofbathroom;
    }

    public void setNumberofbathroom(Integer numberofbathroom) {
        this.numberofbathroom = numberofbathroom;
    }

    public String getIsfurnished() {
        return isfurnished;
    }

    public void setIsfurnished(String isfurnished) {
        this.isfurnished = isfurnished;
    }

    public String getHasgarden() {
        return hasgarden;
    }

    public void setHasgarden(String hasgarden) {
        this.hasgarden = hasgarden;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public Date getEntereddate() {
        return entereddate;
    }

    public void setEntereddate(Date entereddate) {
        this.entereddate = entereddate;
    }

    public Date getEnteredtime() {
        return enteredtime;
    }

    public void setEnteredtime(Date enteredtime) {
        this.enteredtime = enteredtime;
    }

    public Paddress getPaddress() {
        return paddress;
    }

    public void setPaddress(Paddress paddress) {
        this.paddress = paddress;
    }

    @XmlTransient
    public Collection<Offer> getOfferCollection() {
        return offerCollection;
    }

    public void setOfferCollection(Collection<Offer> offerCollection) {
        this.offerCollection = offerCollection;
    }

    public Propertyowner getPropertyownerid() {
        return propertyownerid;
    }

    public void setPropertyownerid(Propertyowner propertyownerid) {
        this.propertyownerid = propertyownerid;
    }

    public Realestateagent getRealestateagentid() {
        return realestateagentid;
    }

    public void setRealestateagentid(Realestateagent realestateagentid) {
        this.realestateagentid = realestateagentid;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (propertyid != null ? propertyid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Property)) {
            return false;
        }
        Property other = (Property) object;
        if ((this.propertyid == null && other.propertyid != null) || (this.propertyid != null && !this.propertyid.equals(other.propertyid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.sushan.model.Property[ propertyid=" + propertyid + " ]";
    }

}

DAO Session Bean (LOCAL):

import com.sushan.model.Property;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class PropertyDAO implements PropertyDAOLocal {
    @PersistenceContext(unitName="RealEstateTrialSystemPU")
    private EntityManager em;

    @Override
    public void AddProperty(Property property) {
        em.persist(property);
    }

    @Override
    public void EditProperty(Property property) {
        em.merge(property);
    }

    @Override
    public void DeleteProperty(int propertyId) {
        em.remove(GetProperty(propertyId));
    }

    @Override
    public List<Property> GetAllProperty() {
        return em.createNamedQuery("Property.findAll").getResultList();
    }

    @Override
    public Property GetProperty(int propertyId) {
        return em.find(Property.class, propertyId);
    }

}

Servlet class:

import com.sushan.dao.PropertyDAOLocal;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "PropertyServlet", urlPatterns = {"/PropertyServlet"})
public class PropertyServlet extends HttpServlet {

    @EJB
    private PropertyDAOLocal propertyDAO;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("allProperty", propertyDAO.GetAllProperty());
        request.getRequestDispatcher("/AllProperty.jsp").forward(request, response);
        }

JSP Page:

<table border="1">
            <th>ID</th>
            <th>Description</th>
            <th>Type</th>
            <th>Number Of Bedroom</th>
            <th>Number Of Bathroom</th>
            <th>Furnished State</th>
            <th>Garden</th>
            <th>Size</th>
            <th>Price</th>
            <c:forEach items="${requestScope.allProperty}" var="p">
                <tr>
                    <td>${p.propertyID}</td>
                    <td>${p.description}</td>
                    <td>c:out value=${p.type}</td>
                    <td>c:out value=${p.numberOfBedroom}</td>
                    <td>c:out value=${p.numberOfBathroom}</td>
                    <td>c:out value=${p.isFurnished}</td>
                    <td>c:out value=${p.hasGarden}</td>
                    <td>c:out value=${p.size}</td>
                    <td>c:out value=${p.price}</td>
                </tr>
            </c:forEach>
        </table> 

The data is not displayed and there are no error messages and also the jsp page just has the table headers with no data. I have data in the property table in the database.

Is there something that I am missing?

Sushan Limbu
  • 43
  • 1
  • 8
  • are you sure that propertyDAO.GetAllProperty() is returning the List. Please check that. Also in the jsp page what are the page directives you are using. Just place this piece of code and check In the page directive of your jsp place this attribute isELIgnored="false" Example : <%@ page isELIgnored="false" %> – Avinash Reddy Mar 07 '17 at 04:23
  • Also check this link http://stackoverflow.com/questions/18326255/i-can-not-use-el-cout-value-inquirylistid-id-in-my-jsp/18329930#18329930 – Avinash Reddy Mar 07 '17 at 04:24
  • @AvinashReddy That doesn't work for me and also m getting an error saying 'javax.servlet.ServletException: Wrapper cannot find servlet class com.sushan.controller.PropertyServlet or a class it depends on' :/ – Sushan Limbu Mar 07 '17 at 14:38
  • Is your servlet class in a package com.sushan.controller? – Avinash Reddy Mar 08 '17 at 06:42
  • Are you uisng tomcat server and what is its version.Also what is the url you are using to access the servlet – Avinash Reddy Mar 08 '17 at 07:11
  • @AvinashReddy I have found the solution to that error by recreating the same project and recreating those packages and copying and pasting the classes to it. Now I face the error that I get when I try to use EJB in a servlet. Maybe it is a problem with the EJB class. Maybe I am not getting anything back. So I thought of doing unit testing for the EJB methods :O – Sushan Limbu Mar 08 '17 at 09:49

0 Answers0