I have been trying to print a ResultSet inside a JSP with the help of a Servlet through an ArrayList.The JDBC part seems all right.The ArrayList can be obtained inside the JSP and can be printed.But when I want to iterate the list passed onto the JSP with the <c:forEach>
tag,the it is saying that my bean class 'PureJavaClasses.employeeClass' doesn't have the attribute 'Id'. I have tried a bunch of different solutions posted on SO but all in vain.I am out of ideas about what to do.I am a total beginner in JavaEE.
Here is my Bean class:
public class employeeClass implements java.io.Serializable {
private int id;
private String name;
private String job;
private int subjectId;
private int salary;
public employeeClass() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSubjectid() {
return subjectId;
}
public void setSubjectid(int subjectId) {
this.subjectId = subjectId;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "id=" + id +
", name=" + name +
", job=" + job +
", subjectId=" + subjectId +
", salary=" + salary;
}
}
Here is the servlet:
private void show_table(HttpServletRequest request,HttpServletResponse response){
Connection connection = ServletLogin.connection;
String query = "SELECT * FROM `dog`.`employee`";
ArrayList<employeeClass> list = new ArrayList<>();
try {
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
while(rs.next()){
employeeClass obj2 = new employeeClass();
obj2.setId(rs.getInt("ID"));
obj2.setName(rs.getString("Name"));
obj2.setJob(rs.getString("Job_Designation"));
obj2.setSubjectid(rs.getInt("Subject ID"));
obj2.setSalary(rs.getInt("Salary"));
list.add(obj2);
}
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("listEmp",list);
try {
request.getRequestDispatcher("/Pages/EmployeeTable.jsp").forward(request,response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Here is my EmployeeTable.jsp :
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.ArrayList"%>
<html>
<head>
<title>Employee List</title>
</head>
<body>
<% ArrayList list1 =(ArrayList) request.getAttribute("listEmp"); %>
<%=list1%>
<table>
<th>ID</th>
<th>Name</th>
<th>Job Designation</th>
<th>Subject ID</th>
<th>Salary</th>
<c:forEach items="${listEmp}" var="list">
<tr>
<td><c:out value="${list.Id}"/></td>
<td><c:out value="${list.name}"/></td>
<td><c:out value="${list.job}"/></td>
<td><c:out value="${list.subjectId}"/></td>
<td><c:out value="${list.salary}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
Here is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>ServletLogin</servlet-name>
<servlet-class>ServletLogin</servlet-class>
</servlet>
<servlet>
<servlet-name>ServletEmployee</servlet-name>
<servlet-class>ServletEmployee</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletLogin</servlet-name>
<url-pattern>/ServletLogin</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletEmployee</servlet-name>
<url-pattern>/ServletEmployee</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
I think you need to fix by providing correct names. Like use id instead of Id, etc. That should solve it.
<td><c:out value="${list.id}"/></td>