aspose file tools*
The moose likes JSP and the fly likes JspContext to PageContext cast Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JspContext to PageContext cast" Watch "JspContext to PageContext cast" New topic
Author

JspContext to PageContext cast

Swapnil Dharane
Ranch Hand

Joined: Apr 22, 2012
Posts: 36
Hi All,
We all know that an object of a superclass cannot be casted to an object of a subclass.Now here we get the deviation from this rule

(Just to be clear,PageContext is a subclass of JspContext.Actually,it's a interface Hierarchy.Check the following link
Java Class Hierarchy)

public void doTag()throws JspException,IOException
{
JspWriter out=getJspContext().getOut();
out.println("Tag gets going");
PageContext context=(PageContext)getJspContext(); //Here is a typecast from JspContext to PageContext
context.getRequest();
getJspContext().setAttribute("names", "here they are");
getJspBody().invoke(null);

}
More formally,I am using reference variable of type pagecontext for JspContext object returned by getJspContext() method.I am not getting ClassCastException.

Can anyone tell me how this happened???
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

There is no "deviation from the rule".

What is being returned from the method is a PageContext instance.

Remember that it is possible to refer to an extended class by one of its ancestors. In this case, the method could return any class that extends JspContext. In this case, it's a PageContext.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Swapnil Dharane
Ranch Hand

Joined: Apr 22, 2012
Posts: 36
Bear Bibeault wrote:There is no "deviation from the rule".

What is being returned from the method is a PageContext instance.

Remember that it is possible to refer to an extended class by one of its ancestors. In this case, the method could return any class that extends JspContext. In this case, it's a PageContext.


but when we see the return type of getJspContext() it returns JspContext and if getJspContext() was to return pageContext instance,there would have been no need to cast the result of getJspContext() method to PageContext.
Then why do we cast it?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

Swapnil Dharane wrote: if getJspContext() was to return pageContext instance,there would have been no need to cast the result of getJspContext() method to PageContext.

Incorrect. This is just basic Java. If you have a reference to a subtype of an instance, you need to cast it to use it as the extended type.
Swapnil Dharane
Ranch Hand

Joined: Apr 22, 2012
Posts: 36
Bear Bibeault wrote:
Swapnil Dharane wrote: if getJspContext() was to return pageContext instance,there would have been no need to cast the result of getJspContext() method to PageContext.

Incorrect. This is just basic Java. If you have a reference to a subtype of an instance, you need to cast it to use it as the extended type.


All right.So you want to say that getJspContext() method returns a PageContext instance.But we still need to cast it

Can you explain this with an example related to PageContext and JspContext?
Swapnil Dharane
Ranch Hand

Joined: Apr 22, 2012
Posts: 36
Swapnil Dharane wrote:
Bear Bibeault wrote:
Swapnil Dharane wrote: if getJspContext() was to return pageContext instance,there would have been no need to cast the result of getJspContext() method to PageContext.

Incorrect. This is just basic Java. If you have a reference to a subtype of an instance, you need to cast it to use it as the extended type.


All right.So you want to say that getJspContext() method returns a PageContext instance.But we still need to cast it

Can you explain this with an example related to PageContext and JspContext?

As we can cast JspContext to PageContext,I can see only two possibilities.Correct me if I am wrong
1)PageContext is not exactly a subclass of JspContext.If it was a subclass of jspContext we could not cast JspContext reference to it.
2)PageContext and JspContext have some relationship other than inheritance.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

It is exactly the same as a method that stays it returns on Object, but actually returns a String. When you get the instance back, you need to cast it to a String to use it as a String.
Swapnil Dharane
Ranch Hand

Joined: Apr 22, 2012
Posts: 36
Got it finally........
But please tell me if I was wrong with the two possibilities I stated in previous post
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

Both suppositions are wrong.

Swapnil Dharane
Ranch Hand

Joined: Apr 22, 2012
Posts: 36
Bear Bibeault wrote:Both suppositions are wrong.



Then correct me please :D .And Please explain How do we describe the relationship with these classes
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

1)PageContext is not exactly a subclass of JspContext.

It is a subclass of JspContext. It's easy enough to check this sort of thing by referencing the javadoc.

If it was a subclass of jspContext we could not cast JspContext reference to it.

Nonsense. Of course you can. This is basic Java. Consider:

Perfectly legal. As String is a subclass of Object, a reference to a String can be its super class Object. But in order to get at it as a String, it needs to be cast.

Same thing happening here. You have a reference to a PageContext as its JspContext superclass. It's perfectly legal to upcast the superclass reference to the actual subclass.

2)PageContext and JspContext have some relationship other than inheritance.

Nope. See above.
Swapnil Dharane
Ranch Hand

Joined: Apr 22, 2012
Posts: 36
Thanks Bear..... That means getJspContext() returns JspContext reference which actually refers PageContext instance right?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

Yes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JspContext to PageContext cast