I am new to java and im using Eclipse IDE. Im doing a project where Admin is adding employees to the system. and i need to display the list of the employees by retrieving the data from the database and view it in a separate table (separate jsp page). even though the data is added to the database im not able to retrieve it. and i have used a for each loop in the jsp page. when the view page jspPropertyNotFoundException is being called .
Model classes
public class Person {
private String Nic;
private String Name;
private String Email;
private String Phone;
private String Address;
public Person(String Nic, String Name, String Email, String Phone, String Address) {
this.Nic = Nic;
this.Name = Name;
this.Email = Email;
this.Phone = Phone;
this.Address = Address;
}
public void setNic(String Nic) {
this.Nic = Nic;
}
public String getNic() {
return Nic;
}
public class Employee extends Person {
private String YearsOfExperience;
private String Specialization;
private String Salary;
public Employee(String Nic, String Name, String Email, String Phone, String Address, String YearsOfExperience,
String Specialization, String Salary) {
super(Nic, Name, Email, Phone, Address);
this.YearsOfExperience = YearsOfExperience;
this.Specialization = Specialization;
this.Salary = Salary;
}
public String getYearsOfExperience() {
return YearsOfExperience;
}
jsp page i want display the data (EmployeeList.jsp)
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<table>
<c:forEach var="emp" items="${employeeDetails}">
<c:set var="Nic" value="${emp.Nic}"/>
<c:set var="Name" value="${emp.Name}"/>
<c:set var="Email" value="${emp.Email}"/>
<c:set var="Phone" value="${emp.Phone}"/>
<c:set var="Address" value="${emp.Address}"/>
<c:set var="YearsOfExperience" value="${emp.YearsOfExperience}"/>
<c:set var="Specialization" value="${emp.Specialization}"/>
<c:set var="Salary" value="${emp.Salary}"/>
<tbody>
<tr>
<td>${emp.Nic}</td>
<td>${emp.Name}</td>
<td>${emp.Email}</td>
<td>${emp.Phone}</td>
<td>${emp.Address}</td>
<td>${emp.YearsOfExperience}</td>
<td>${emp.Specialization}</td>
<td>${emp.Salary}</td>
</tr>
</tbody>
</c:forEach>
</table>
addEmployee Servlet
@WebServlet("/addEmployeeServlet")
public class addEmployeeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String Nic= request.getParameter("Nic");
String Name= request.getParameter("Name");
String Email= request.getParameter("Email");
String Phone= request.getParameter("Phone");
String Address= request.getParameter("Address");
String YearsOfExperience= request.getParameter("YearsOfExperience");
String Specialization = request.getParameter("Specialization");
String Salary= request.getParameter("Salary");
boolean isTrue;
isTrue = EmployeeDBUtil.AddEmployee(Nic,Name,Email,Phone,Address,YearsOfExperience,Specialization,Salary);
//if inserted to database
if(isTrue == true) {
List <Employee> employeeDetails = EmployeeDBUtil.viewAllEmployee();
request.setAttribute("employeeDetails",employeeDetails);
RequestDispatcher dis1 =request.getRequestDispatcher("EmployeeList.jsp");
dis1.forward(request, response);
}
else {
RequestDispatcher dis2 =request.getRequestDispatcher("Unsuccess.jsp");
dis2.forward(request, response);
}
}
EmployeeDBUtil
public class EmployeeDBUtil {
private static Connection con= null;
private static Statement stmt = null;
private static ResultSet rs = null;
private static boolean isSuccess;
// data from the database
public static List<Employee> viewAllEmployee() {
ArrayList <Employee> emp = new ArrayList<>();
try {
con = DBconnection.getConnection();
stmt = con.createStatement();
String sql = "Select * from employee";
rs= stmt.executeQuery(sql);
while(rs.next()) {
String Nic= rs.getString(1);
String Name = rs.getString(2);
String Email = rs.getString(3);
String Phone =rs.getString(4);
String Address =rs.getString(5);
String YearsOfExperience =rs.getString(6);
String Specialization =rs.getString(7);
String Salary =rs.getString(8);
Employee e = new Employee(Nic,Name,Email,Phone,Address,YearsOfExperience,Specialization,Salary);
emp.add(e);
System.out.println("im here");
}
}catch(Exception e){
e.printStackTrace();
}
return emp;
}
Web Xml file
?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>AddEmployee</servlet-name>
<servlet-class>com.carepoint.servlet.addEmployeeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddEmployee</servlet-name>
<url-pattern>/Add</url-pattern>
</servlet-mapping>
</web-app>
when i add an employee to the system and it goes to the EmployeeList.jsp page but it does show the table or the list . Though the Nic is in person model. This is the error that is shown .
May 09, 2020 7:04:31 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet [jsp] threw exception
org.apache.jasper.el.JspPropertyNotFoundException: /EmployeeList.jsp(315,9) '${emp.Nic}' Property [Nic] not found on type [com.carepoint.model.Employee]
at org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:120)
at org.apache.jsp.EmployeeList_jsp._jspx_meth_c_005fset_005f0(EmployeeList_jsp.java:659)
at org.apache.jsp.EmployeeList_jsp._jspx_meth_c_005fforEach_005f0(EmployeeList_jsp.java:558)
at org.apache.jsp.EmployeeList_jsp._jspService(EmployeeList_jsp.java:441)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:712)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:459)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:384)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312)
at com.carepoint.servlet.addEmployeeServlet.doPost(addEmployeeServlet.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
May 09, 2020 7:04:31 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [AddEmployee] in context with path [/Care-Point-Service-Station] threw exception [An exception occurred processing [/EmployeeList.jsp] at line [315]
312:
313: <c:forEach var="emp" items="${employeeDetails}">
314:
315: <c:set var="Nic" value="${emp.Nic}"/>
316: <c:set var="Name" value="${emp.Name}"/>
317: <c:set var="Email" value="${emp.Email}"/>
318: <c:set var="Phone" value="${emp.Phone}"/>
Stacktrace:] with root cause
org.apache.jasper.el.JspPropertyNotFoundException: /EmployeeList.jsp(315,9) '${emp.Nic}' Property [Nic] not found on type [com.carepoint.model.Employee]
at org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:120)
at org.apache.jsp.EmployeeList_jsp._jspx_meth_c_005fset_005f0(EmployeeList_jsp.java:659)
at org.apache.jsp.EmployeeList_jsp._jspx_meth_c_005fforEach_005f0(EmployeeList_jsp.java:558)
at org.apache.jsp.EmployeeList_jsp._jspService(EmployeeList_jsp.java:441)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:712)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:459)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:384)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312)
at com.carepoint.servlet.addEmployeeServlet.doPost(addEmployeeServlet.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
why cant i retrieve the data ?
Address the following things in your code:
Nic
should be named as nic
. You have violated the convention for all your variables.@WebServlet
, you do not need to declare mapping in web.xml
.Given below is the Minimal, Complete, and Verifiable example:
Person.java:
package beans;
public class Person {
private String nic;
private String name;
private String email;
private String phone;
private String address;
public Person(String nic, String name, String email, String phone, String address) {
this.nic = nic;
this.name = name;
this.email = email;
this.phone = phone;
this.address = address;
}
public String getNic() {
return nic;
}
public void setNic(String nic) {
this.nic = nic;
}
}
Employee.java:
package beans;
public class Employee extends Person {
private String yearsOfExperience;
private String specialization;
private String salary;
public Employee(String nic, String name, String email, String phone, String address, String yearsOfExperience,
String specialization, String salary) {
super(nic, name, email, phone, address);
this.yearsOfExperience = yearsOfExperience;
this.specialization = specialization;
this.salary = salary;
}
}
AddEmployeeServlet.java:
package servlets;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import beans.Employee;
@WebServlet("/addEmployeeServlet")
public class AddEmployeeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Employee> employeeDetails = List.of(new Employee("123", null, null, null, null, null, null, null),
new Employee("456", null, null, null, null, null, null, null));
request.setAttribute("employeeDetails", employeeDetails);
request.getRequestDispatcher("employees.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
employees.jsp:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<table>
<c:forEach var="emp" items="${employeeDetails}">
<tr>
<td>${emp.nic}</td>
</tr>
</c:forEach>
</table>