• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get ServletConfig or ServeltContext object in the Simple Tag handler?

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I get ServeltContext or ServletConfig object in the simple tag handler?

public class SimpleMathTag extends SimpleTagSupport
{
public void doTag() throws JspException, IOException
{
//get ServletConfig and ServeltContext
}
}
Thanks in advance.
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are really there why don't you ask pageContext it has answer for all your implicit objects of JSP.
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how?
Can you provide the code?

JspContext is the only implicit object used by simple tag handler, PageContext is not.

PageContext is one of the implicit objects used by classic tag handler.


So , I'd like to know how I am going to get that.

Thanks
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cast the JspContext to PageContext and call the getServletContext/getServletConfig methods.

 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tiffiny,

Please visit the Custom tag's life style(cycle).

get introduced to pageContext in the cycle.

TagSupport.java
---------------
public void setPageContext(PageContext pageContext) {
this.pageContext = pageContext;
}
[ April 20, 2007: Message edited by: Srinivasan thoyyeti ]
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying.
Yes, cast to PageContext, then I will be able to get all I want.

Thanks
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JspContext does not provide access to ServletContext because Simple tag support is not bound to servlets. If you want to write a tag that needs some dependency on servlets I suggest to use classic tags.

There may be a question on this concept in the exam
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just wanted to add something tho what has been said so far.

Here's what the spec says in section JSP.7.1.5 about simple tags:


In addition to being simpler to work with, Simple Tag Extensions do not directly rely on any servlet APIs, which allows for potential future integration with other technologies. This is facilitated by the JspContext class, which PageContext now extends. JspContext provides generic services such as storing the JspWriter and keeping track of scoped attributes, whereas PageContext has functionality specific to serving JSPs in the context of servlets. Whereas the Tag interface relies on PageContext, SimpleTag only relies on JspContext.



However, when taking a look into SimpleTagSupport's API pages, this is what they say about the setJspContext method:


public void setJspContext(JspContext pc)
Stores the provided JSP context in the private jspContext field. Subclasses can access the JspContext via getJspContext().
Parameters:
pc - the page context for this invocation



I tried to output the result of getJspContext() instanceof PageContext inside a class extending SimpleTagSupport, and the result was that the Object returned was in fact a PageContext instance.

In my opinion, what the spec says in section JSP.7.1.5 is the same as with filters' doFilter method receiving ServletRequest and ServletResponse as parameters (instead of HttpServletRequest and HttpServletResponse): it was defined that way to allow future use of SimpleTag (or Filter) outside the context of a web application, but in the context of a web application I think we may assume it is safe to cast getJspContext()'s result into PageContext, as it is safe to cast the ServletRequest and ServletResponse (received in doFilter) to a HttpServletRequest and HttpServletResponse.

Any ideas? Am I going to far in my assumptions?
[ April 24, 2007: Message edited by: Sergio Tridente ]
 
Sri Kan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

in the context of a web application I think we may assume it is safe to cast getJspContext()'s result into PageContext, as it is safe to cast the ServletRequest and ServletResponse (received in doFilter) to a HttpServletRequest and HttpServletResponse



Being safe is different from being appropriate (good coding practice). If you happen to be using some non-servlet dependent framework/API in future your code may require lot of changes.
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic