• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Tag Files and ServletContext

 
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scriptlets? In a tag file?

Perhaps you could research more modern ways to look at your environment in JSP 2.0 environments?
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You misunderstood my question. Have you found the way to get the servlet context without the use of scriptlets?
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ok. Thank you Chinmaya.
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic