Abstract class - Person
package myclasses;
public abstract class Person{
private
String name ;
public Person(){
System.out.println("Person Constructor");
}
public void setName(String name){
this.name= name ;
}
public String getName(){
return name;
}
}
Concrete subclass - Employee
package myclasses;
public class Employee extends Person{
private int empId;
public Employee(){
System.out.println("Employee Constructor");
}
public void setEmpId(int empId){
this.empId = empId;
}
public int getEmpId(){
return empId;
}
}
Servlet - EmployeeServlet
package myclasses;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class EmployeeServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
Employee e =new Employee();
req.setAttribute("employee", e);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/EmployeeJsp.jsp");
rd.forward(req,res);
}
}
The
JSP thats called EmployeeJsp
<html>
<body>
Testing the forwarded page
<jsp:useBean id="employee" type="myclasses.Person" scope="request"/>
<jsp:setProperty name="employee" property="name" value="Nithya"/>
<jsp:setProperty name="employee" property="empId" value="1"/>
The name is ${employee.name}
The empId is ${employee.empId}
</body>
</html>
My web.xml has:
<servlet>
<servlet-name>MyEmployee</servlet-name>
<servlet-class>myclasses.EmployeeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyEmployee</servlet-name>
<url-pattern>/MyServlet.do</url-pattern>
</servlet-mapping>
When i type
http://localhost:8080/mywebapp/MyServlet.do, I get the following output
Testing the forwarded page The name is Nithya The empId is 1
As per the book and other resources we should get an error because:
If only the type is given for an existing bean, then only the types properties can be set and not that of concrete class.
But the jsp does not throw any error.
Why so??