0

How can I make my own tags use the same ressources(for example xml doc)?

Pseudo code:

JSP:

<readxml:getuser/>
<readxml:getpassword/>

Java:

public class getpassword(or getuser) extends BodyTagSupport
{
   if(doc)
   {
      out.println(doc)
   }
   else
   {
      doc = builder.build(file)
      out.println(doc)
   }
}

Is that even possible?

John Frost
  • 673
  • 1
  • 10
  • 24

2 Answers2

2

You can store temp data in the context of the current request:

public class getpassword(or getuser) extends BodyTagSupport
{
   public int doEndTag() {
      Doc doc = pageContext.getRequest().getAttribute("doc");
      if(doc == null)
      {
         pageContext.getRequest().setAttribute("doc", doc = builder.build(file));
      }
      out.println(doc);
       ...
   }
}

You can also store it into a session or static variable (depends on scope you want).

Eugene Retunsky
  • 13,009
  • 4
  • 52
  • 55
0

Extend your custom tags from a common class and defer the XML loading to it. Since you want to avoid parsing the document repeatedly you will need to make the document variable itself static.

Alternatively to placing the document loading and storage functionality in a common class you may choose to implement it in a standalone utility class that you make reference to from all the relevant tag (support classes).

Perception
  • 79,279
  • 19
  • 185
  • 195
  • In principle you are right, and I tried to do this at first, but the Tag classes are already extended from a common class and I don't want to fiddle that deep into the whole design. – John Frost Apr 12 '12 at 17:40