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 Tag Files and ServletContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Tag Files and ServletContext" Watch "Tag Files and ServletContext" New topic
Author

Tag Files and ServletContext

Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 171


Do tag files have access to ServletContext?.
HFSJ says ...." You don't have ServletContext though. A tag File uses JspContxt instead of ServletContext.."( Page 510.Qn1).

But in following tag file i tried to get ServletContext and use it.
Also, application implicit object is available which is a servletContext.
So,do tag files have access to ServletContext?

header.tag:


TestTag.jsp:

output is:

Testing tag

getting servlet Context to get Minor version 5
using application implicit object to get ContextPath /Test
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Scriptlets? In a tag file?

Perhaps you could research more modern ways to look at your environment in JSP 2.0 environments?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 171

Yes.Why not? We can use scriptlets in Tag Files. But we can't use them in the body of tag.
Offcourse following would be invalid

<myTag:header>
<%= abc().getName() %>
</myTag:header>

I know its not wise to use scriptlets in tag files though.But i was just testing some queries which came in my mind.
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi Bindu.

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).


I think it is wording problem.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Bindu Lakhanpal wrote:We can use scriptlets in Tag Files.

Just because you can, doesn't mean that you should. If you are using tag files, that means JSP 2.0. And scriptlets have no business anywhere in a JSP 2.0 application.

Are you still trying to find the servlet context?
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 171

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.
Thanks again.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

You misunderstood my question. Have you found the way to get the servlet context without the use of scriptlets?
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 171

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)
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 171

I guess hfsj was wrong in this.
I tried to search in jsp spec but could not find.
But i found this webpage about all the implicit objects available to tagfiles and application is one of them.

Alex Serna
Ranch Hand

Joined: Sep 18, 2009
Posts: 58
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.
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Alex.

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.

HFSJ says,
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'.
Alex Serna
Ranch Hand

Joined: Sep 18, 2009
Posts: 58
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?
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
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 api says,
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.
Alex Serna
Ranch Hand

Joined: Sep 18, 2009
Posts: 58

ok. Thank you Chinmaya.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tag Files and ServletContext