Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Standard Actions

 
Nithya Venkatraman
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Rodrigo W Bonatto
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to know where did you read that it would throw an exception.
I tried your code and it also works fine for me.

So, now I'm in a doubt with you question.

Rodrigo
 
Ritu varada
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
----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.----

I was also looking at the same thing today. I am not sure where you read the above comment but, this is what it says in the HFS book.

If type is used without class, the bean must already exist.


Since you have a "employee" bean already in request scope, the jsp:setProperty statements work.

This is what I think. Hopefully, who knows this for sure will reply. Thanks.
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
If you specify only the type attriute then the bean should alreday be instantiated. In your case you have already instantiated the employee bean in your servlet , thats means in your JSP the useBean will be able to get it.
Since your Employee bean extends Person it will work. It will be like

Person person = Employee (existing object);

<jsp:useBean id="employee" type="myclasses.Person" scope="request"/>

In this case it will look for the employee(the id attribute of useBean) in the request scope and since you have one in the same scope it will work.


Hope tha helps.
 
Rodrigo W Bonatto
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Inside the JSP generated servlet at the exactly point that the reference of the existing bean is created, whe will have: (Tomcat 5.0.28)

...
....
beanPackage.Person employee = null;
synchronized (request) {
employee = (beanPackage.Person) _jspx_page_context.getAttribute "employee", PageContext.REQUEST_SCOPE);
if(employee == null){
throw new java.lang.InstantiationException("bean employee not found within scope");
}
}
....
...

When the type attribute is specified, the generated servlet cast the existing bean to the type we have specified in the type attribute.
Doing it in a simple java core, we'll have a compiling error, because the abstract class doesn't have Employee's methods.

So, what's the explanation for this?

Rodrigo
(sorry for my english) :roll:
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rodrigo,
Sorry couldn't get u .. whats the problem with the type casting.

Thanks
 
Ritu varada
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kathy has lready given a good explanation for this.

http://www.coderanch.com/t/168204/java-Web-Component-SCWCD/certification/jsp-useBean-setProperty-HFS-query
 
Rodrigo W Bonatto
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My doubt is just with this rule:

"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 I tried the code posted by Nithya and it works perfectly, so, there is a book mistake or there is anything I don't understood?

Rodrigo
 
Ritu varada
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rodrigo, see my above post. Kathy has already explained this! If your doubt persists even after reading that, let us know and we can solve it together! Cheers!
 
Rodrigo W Bonatto
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritu

Thanks for your help!

Rodrigo
[ January 19, 2005: Message edited by: Rodrigo W Bonatto ]
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The link ritu has given explains it. Sorry for not getting your question previously.

Cheers.
 
Nithya Venkatraman
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot...

I am just going thru the explanation by Kathy...


Thanks

Nithya
 
Krzysiek Hycnar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There're more surprises like that. For exemple you can use sth like... ${pageContext.request.requestURI} (found somewhere that in HFS) and this might give you a hard time on the exam, because the full signature of the getRequest() method is: public ServletRequest PageContext.getRequest(), and the getRequestURI() method is defined in HttpServletRequest. IMHO - reflection is the answer

Cheers,
Krzysiek
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic