Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Custom tag generating JSP code?....

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

Is it at all possible for a custom tag to generate JSP code and have it evaluated by the JSP page?

I tried to do the following as a test:
I wrote a class that extends TagSupport and outputs a very simple jsp expression like as a String. Then I wrote another class that extends BodyTagSupport, set its <body-content> to accept JSP, and made it simply print the results of its body evaluation.
The code that does the work is as follows:

BodyContent bodyContent = getBodyContent();
JspWriter out = bodyContent.getEnclosingWriter();
String bodyString = bodyContent.getString();
if(bodyString!=null) out.print(bodyString);


Then I placed the first tag between the starting and ending tags of the second one and ran my page. The result was a bit frustrating, it was just "<%=new java.util.Date() %>", that is, the JSP expression did not get evaluated but it was just printed as is...

Anything that I did wrong?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is it at all possible for a custom tag to generate JSP code and have it evaluated by the JSP page?


No
Think about when JSPs are compiled into servlets, what tags turn into in the compiled servlet, and when tag code runs.
Bill
 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it always seemed to me that custom tags are evaluated first, then the results of the evaluation are included in the page code and then it is again evaluated as a whole, but in my case the output of a custom tag are NOT evaluated even though they represent the body of another tag which MUST accept JSP code and MUST be evaluated....

I'm still a little confused...
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, it always seemed to me that custom tags are evaluated first, then the results of the evaluation are included in the page code and then it is again evaluated as a whole


Well, thats the source of your problem. Why don't you examine the Java code created by the JSP compiler to see what tags really turn into.
Bill
 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, Bill, thanks, will do...

P.S. There's such a bloody mess in it...
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic