Title: Message
Yes, you're right.
I've just described the common case of using beans from tag hanler. These beans can be commonly be used both by JSPs and tag hanlers.
BTW, The code, which instaciates bean in tag handler, is the same, what JSP Compiler produces whet it comes across:
<jsp:useBean id='clock' scope='page' class='dates.JspCalendar' type="dates.JspCalendar" />
 
If you are sure, that JSP doesn't declare a bean, you certainty may instanciate bean by using:
 
JspCalendar clock =new JspCalendar();
 
whitout trying to find it in pageContext.
 
If you are sure, that this bean will not be used lately in JSP, you can omit putting it into pageContext.
 
If you are interesting in how JSP compiler treats JSP directives, you can study produced by JSP compiler java-code, which can by found
in tomcar/work directory.
 
Regards, Yuri
 
----- Original Message -----
From: Purav
Sent: Wednesday, December 26, 2001 12:18 PM
Subject: Re: Accessing Beans from TagLibs

HI,
 
why do you have to write
JspCalendar clock = (JspCalendar)   pageContext.getAttribute("clock", PageContext.PAGE_SCOPE);
in your tag handler to access the bean.
cant i just write
JspCalendar clock =new JspCalendar()
 
Also, the bean may not be used in the JSP Page.. so pageContext.getAttribute will not work
 
The next statement
clock = (JspCalendar) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "dates.JspCalendar");
Again,
cant i just write
JspCalendar clock =new JspCalendar()
 
Please Help
 
Regards
Purav

 
 
-----Original Message-----
From: Yuri Grument [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, December 25, 2001 1:26 PM
Subject: Re: Accessing Beans from TagLibs

Hi, Puray
 
Tomcat includes all classes located in web-inf/classes and all jars located in web-inf/lib into CLASSPATH available to your tag handler.
So, you ccan just add
 
import somepackage.SomeBean;
 
into your tag handler.
 
For examle, in JSP you declare bean as
 
<jsp:useBean id='clock' scope='page' class='dates.JspCalendar' type="dates.JspCalendar" />
 
In tag handler the code will be next:
 
import dates.JspCalendar;
 
 
 
JspCalendar clock = (JspCalendar)   pageContext.getAttribute("clock", PageContext.PAGE_SCOPE);
 if ( clock == null ) {
  try {
      clock = (JspCalendar) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "dates.JspCalendar");
  } catch (ClassNotFoundException exc) {
          throw new InstantiationException(exc.getMessage());
  } catch (Exception exc) {
           throw new ServletException (" Cannot create bean of class "+"dates.JspCalendar", exc);
  }
  pageContext.setAttribute("clock", clock, PageContext.PAGE_SCOPE);
}
 Regards,
  Yuri
----- Original Message -----
From: Purav
Sent: Tuesday, December 25, 2001 9:12 AM
Subject: Accessing Beans from TagLibs

Hello,

 

I am using Tomcat 4.

I want to instantiate my Beans in my taglibs.

The Beans are located in web-inf/classes directory and taglibs are located in web-inf/lib directory

In JSP page I can use the jsp:useBean tag, but in my Taglib , how do I instantiate the bean. They are in a different directory�

Please Help

 

 

Purav

 

Reply via email to