aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes How to get ServletConfig or ServeltContext  object in the Simple Tag handler? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "How to get ServletConfig or ServeltContext  object in the Simple Tag handler?" Watch "How to get ServletConfig or ServeltContext  object in the Simple Tag handler?" New topic
Author

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

Tiffiny Yang
Ranch Hand

Joined: Mar 29, 2006
Posts: 124
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.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
If you are really there why don't you ask pageContext it has answer for all your implicit objects of JSP.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Tiffiny Yang
Ranch Hand

Joined: Mar 29, 2006
Posts: 124
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
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Cast the JspContext to PageContext and call the getServletContext/getServletConfig methods.



SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
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

Joined: Mar 29, 2006
Posts: 124
Thanks for replying.
Yes, cast to PageContext, then I will be able to get all I want.

Thanks
Sri Kan
Ranch Hand

Joined: Feb 20, 2007
Posts: 30
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


Cheers,<br /> <br />SCJP 1.5 (83% Huh !)<br />SCWCD 1.4 (81% that's it ??)
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

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

Joined: Feb 20, 2007
Posts: 30
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get ServletConfig or ServeltContext object in the Simple Tag handler?