Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PageContext and Implicit Objects

 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Looking in the JSP API Document and JSP specs.

In the PageContext API

1. The getRequest method returns ServletRequest.
2. The getResponse method returns ServletResponse.
3. The getException method returns Exception

In the JSP Specs the corresponding implicit objects are definded as

1. request of type HttpServletrRquest
2. response of type HttpServletResponse
3. exception of type Throwable.

Do not understand why the upcasting is done for the exception implicit object. Is there is any particular reason, that the getException method does not return Throwable type ?

Thanks
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
::::Just pondering:::::::

Could the reason be as simple as - to ensure that RuntimeExceptions are also trapped and redirection takes place to Error page when they occur.
getException() need not return Throwable for the similar reason.
Invacation of the method would take place for getting objects of class Exception or subclasses there of.


Regards,
Shivani.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It is possible through Exception also. RuntimeException is subclass of Exception.

I understood that all the implicit objects indirectly reference to the objects retrun by the PageContext Object. All other objects are down-casted to specific type to define implicit objects. Why this particular object is up-casted.

Thanks
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry - not RuntimeException but Error and subclasses thereof.
Redirecting to error page in those cases.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The discrepency between the request and response types actually only arises depending on which class your JSP is translated into. It could either be:

javax.servlet.jsp.JspPage [non-HTTP JSPs]
javax.servlet.jsp.HttpJspPage [HTTP JSPs]

These are both interfaces extending Servlet, and HttpJspPage extends JspPage. The only difference is that the HttpJspPage casts its parameters to the HttpServletRequest instead of ServletRequest (similarly for the response).

You should expect containers to translate your pages into the second type wherever possible, so all parameters will be Http* variants. The PageContext API doesn't do this because they don't want to bloat the API by providing new classes for handling the HTTP variants. It's simpler to cast as required.

As for the Exception object being Throwable, I would suggest that since Throwable is the superclass of all exceptions and errors, the JSP developers just decided to use that (as the most general "exception type") in case the JSPs were required to handle Errors as well as Exceptions at some point? Just a suggestion... but I can see no real reason why the two shouldn't be equivalent!
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Charles for your confrimation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic