-1

I'm trying get data from database and display them on webpage.

I expected table with data of entity, but have this:

enter image description here

I have next some classes:

Department:

package entity;

import javax.persistence.*;
import java.io.Serializable;


@Entity
@Table(name = "DEPT")
@NamedQuery(name = "Department.getAll", 
query = "select d from Department    d")
public class Department implements Serializable {
@Id
@Column(name = "DEPTNO")
private int DEPTNO;

@Column(name = "DNAME")
private String dname;

@Column(name = "LOC")
private String loc;

public Department() {
}

public int getDEPTNO() {
    return DEPTNO;
}

public void setDEPTNO(int DEPTNO) {
    this.DEPTNO = DEPTNO;
}

public String getDname() {
    return dname;
}

public void setDname(String dname) {
    this.dname = dname;
}

public String getLoc() {
    return loc;
}

public void setLoc(String loc) {
    this.loc = loc;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Department that = (Department) o;

    if (DEPTNO != that.DEPTNO) return false;
    if (!dname.equals(that.dname)) return false;
    return loc.equals(that.loc);

}

@Override
public int hashCode() {
    int result = DEPTNO;
    result = 31 * result + dname.hashCode();
    result = 31 * result + loc.hashCode();
    return result;
}

@Override
public String toString() {
    return "Department{" +
            "DEPTNO=" + DEPTNO +
            ", dname='" + dname + '\'' +
            ", loc='" + loc + '\'' +
            '}';
}
}

DepartmentService:

public class DepartmentService {

public EntityManager entityManager = Persistence.createEntityManagerFactory("persistenceUnit").createEntityManager();

public List<Department> getAll(){

    TypedQuery<Department> typedQuery = entityManager.createNamedQuery("Department.getAll", Department.class);
    return typedQuery.getResultList();
}
}

ShowAllServlet:

@WebServlet(name = "ShowAllServlet", urlPatterns = "/showAll")
public class ShowAllServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    DepartmentService departmentService = new DepartmentService();
    req.setAttribute("result", departmentService.getAll());
}
}

index.jsp:

<%@ page import="entity.Department" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<div class="main">
<jsp:include page="/showAll"></jsp:include>
<table id="mainTable">
    <tr>
        <th>DEPTNO</th>
        <th>DNAME</th>
        <th>LOC</th>
    </tr>
    <%--@elvariable id="result" type="java.util.List"--%>
    <c:forEach items="${result}" var="obj">
        <tr>
            <td>
                <c:out value="${obj.DEPTNO}"></c:out>
            </td>
            <td>
                <c:out value="${obj.dname}"></c:out>
            </td>
            <td>
                <c:out value="${obj.loc}"></c:out>
            </td>
        </tr>
    </c:forEach>

</table>
</div>
</body>
</html>

I have checked, request have attribute "result".

enter image description here

Roman C
  • 49,761
  • 33
  • 66
  • 176
IceFrgoe
  • 21
  • 1

2 Answers2

0

First try to get out variables from <c:out> tag, you don't need them, second you need to make some more imports:

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.lang.String"%>
<%@page import="javax.portlet.PortletSession"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Radu Lozovanu
  • 167
  • 4
  • 17
0

Looks like you are using JSP very old version. As far as you are using annotations on servlet you should use at least Servlet 3.0 libraries that should be available on web server.

If you have web.xml check the version in the header tag for the correct Servlet version, that should be at least 2.4. If you have a question why you should use it because this version and higher enables EL by default using isELIgnored="false". You can modify the page if you have a requirement to ignore EL on all pages with exception to use EL on that page.

<%@ page isELIgnored ="false" %>     

If you have any libraries supplied with the web application that has implemented servlet, but lower version you should remove them.

If you are using pom.xml specify scope of the library, that is available on the server as provided.

Use the version of JSTL that can be used with the Servlet version of the web application. You can download JSTL using Can not find the tag library descriptor for “http://java.sun.com/jsp/jstl/core” answer.

Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176