File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt in Standard Actions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt in Standard Actions" Watch "Doubt in Standard Actions" New topic
Author

Doubt in Standard Actions

Nithya Venkatraman
Ranch Hand

Joined: Apr 11, 2004
Posts: 39
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??


Nithya<br />SCJP(1.2), SCWCD(1.4)
Rodrigo W Bonatto
Ranch Hand

Joined: Aug 20, 2004
Posts: 62
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


SCJA, SCJP, SCJD, SCWCD, SCBCD5, SCDJWS, SCEA5
Ritu varada
Ranch Hand

Joined: Sep 08, 2004
Posts: 117
----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.


SCJP,SCWCD(1.4)
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
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.


Sawan<br />SCJP,SCWCD,SCBCD<br /> <br />Every exit is an entry somewhere.
Rodrigo W Bonatto
Ranch Hand

Joined: Aug 20, 2004
Posts: 62
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

Joined: Aug 24, 2004
Posts: 250
Hi Rodrigo,
Sorry couldn't get u .. whats the problem with the type casting.

Thanks
Ritu varada
Ranch Hand

Joined: Sep 08, 2004
Posts: 117
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

Joined: Aug 20, 2004
Posts: 62
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

Joined: Sep 08, 2004
Posts: 117
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

Joined: Aug 20, 2004
Posts: 62
Hi Ritu

Thanks for your help!

Rodrigo
[ January 19, 2005: Message edited by: Rodrigo W Bonatto ]
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
Hi,
The link ritu has given explains it. Sorry for not getting your question previously.

Cheers.
Nithya Venkatraman
Ranch Hand

Joined: Apr 11, 2004
Posts: 39
Thanks a lot...

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


Thanks

Nithya
Krzysiek Hycnar
Ranch Hand

Joined: Jan 02, 2004
Posts: 74
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Standard Actions