This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The container will not directly send ServletContext object to the tag file. It sends JspContext object instead. The container can get ServletContext object through jspContext object by casting it to PageContext. Same is true for ServletConfig object.
" You don't have ServletContext though. A tag File uses JspContxt instead of ServletContext.."( Page 510.Qn1).
Thanks Chinmaya Chowdary and thanks Bear Bibeault for the light.
No Bear Bibeault, I am not trying to get SevletContext in tag file using scriptlets.You are right. Scriptlets have absolutely no place anywhere in JSP 2.0 application.It was kind of ignorant of me.
I tried to get ServletContext in both jsp and tag file using EL implicit object pageContext.I thought we could access all the jsp implicit objects using pageContext implicit object of EL.But i was wrong since there are no getApplication() and getConfig() methods in PageContext class.But yes,there are getServletContext() and getServletConfig() methods there.
code in both jsp and tag file:
output in both jsp and tag file:
using pageContext to get ServletConfig jsp
using pagecontext to get ServletContext 2
So, yes i was able to get ServletContext in tag file without using scriptlets (as far as my understanding goes at present).
I am using IDE( Netbeans with Glassfish)
Since no one confirmed Bindu's last post I can asume HFSJ is wrong in that part, and a ServletContext is available, also there are more implicit objects than request and response... can't I? Please correct me if I'm wrong.
Joined: Apr 21, 2008
During translation time, jsp engine will translates the tag file('header.tag') into 'header.java' file. The translated page will not extends 'HttpJspBase' class as incase of jsp does. This class extends the 'HttpServlet' which extends 'GenericServlet' and this implements the Servlet api specification. Any class that extends HttpJspBase, will become servlet after initialization.
Incase of tag files the generated class doesn't extend the HttpJspBase class. At runtime this will not become servlet(Since it doesn't implement Servlet api).
Rather the container provides JspContext object. With the help of this class, tag file is getting all implicit objects as incase of jsp file is having. To be clear see the generated source file.
If we say in jsp, we will get ServletContext object. Incase of tag file we don't, since it does not implement Servlet api.
You don't have a ServletContext, though - a Tag File uses a JspContext instead of a ServletContext.
Here authors has mention the word You don't have a ServletContext. Here we dont' have. Instead we can get ServletContext object using JspContext api. I think there is a difference between the words 'have' and 'get'.
Joined: Sep 18, 2009
I see what you mean... Thanks for the explanation! BUT... JspContext only gives you acces to the "out" implicit jsp object. It's PageContext the one that gives you access to the rest of implicit objects. I read somewhere that you can cast that JspContext to PageContext in order to get the rest of objects(i.e. ServletContext), but I think it said that this wasn't part of the specs and it depends on the Container being used... anyone could clarify this please?
Joined: Apr 21, 2008
Alex Serna wrote: I read somewhere that you can cast that JspContext to PageContext in order to get the rest of objects(i.e. ServletContext)
Yes. To be clear see the generated source code of jsp.
Alex Serna wrote: but I think it said that this wasn't part of the specs and it depends on the Container being used...
The PageContext class is an abstract class, designed to be extended to provide implementation dependent implementations thereof, by conformant JSP engine runtime environments.
If we see the PageContext api, this is an abstract class and many of the methods are also abstract. The specification gives the flexibility for the implementors to implement i.e. to extend and provide implementation to those abstract methods.