Interpreting JSP code

Y

yzzzzz

Hi,

In my Java webapp (using tomcat/struts), I end up having some generated
JSP code that I would like to send to the browser.

Currently the only way I have found of doing this is writing the code to
a JSP file and redirecting to that file.

I am sure there is a better way of doing this.

Thanks
 
T

Tom Arne Orthe

yzzzzz said:
Hi,

In my Java webapp (using tomcat/struts), I end up having some generated
JSP code that I would like to send to the browser.

Currently the only way I have found of doing this is writing the code to
a JSP file and redirecting to that file.

I am sure there is a better way of doing this.

Thanks

Maybe you already knows this, but here goes:

In fact you don't want to send the JSP code to the browser as it
will not know what to do with it. JSP code needs to first go
through a JSP compiler, then a Java compiler and finally the resulting
class needs to be run by the container which then sends the output
(typically HTML-code) to the browser. This process takes place everytime
the JSP page changes. If the JSP page hasn't changed since the last
request the container will re-use the previously compiled java class
representation of the JSP page, thus saving time not doing the first two
compiling steps.

Using dynamic JSP in such a way as you describe is a horrible idea
from a performance point of view.

The better way would be to ry refactoring your solution to use
taglibs/custom tags instead (difficult to give advice as I don't
know what problem you try to solve with generated JSP code).


Regards,
Tom Arne
 
Y

yzzzzz

Tom said:
Maybe you already knows this, but here goes:

Yes I know what JSP is :)
Using dynamic JSP in such a way as you describe is a horrible idea
from a performance point of view.

The better way would be to ry refactoring your solution to use
taglibs/custom tags instead (difficult to give advice as I don't
know what problem you try to solve with generated JSP code).

Here is the problem:
I have a custom taglib I use to create menus.
For example I can do

<menu:menu>Title
<menu:item>first item</menu:item>
<menu:item>second item is a submenu
<menu:item>subitem 1</menu:item>
<menu:item>subitem 2</menu:item>
<menu:item>subitem 3</menu:item>
</menu:item>
<menu:item>third item</menu:item>
</menu:menu>
With as many levels of submenus as I want. The result uses javascript
and stuff to create a windows style dropdown menu.

Now, I have a database containing persistent objects (using Hibernate)
which have a hierarchy, i.e. each object has a set of children (in fact
it's more complicated than that, but this is enough to solve my problem
here).

So in my Java I do this:

void doMenu(Element e, StringBuffer s) {
s.append("<menu:item>");
s.append(e.getTitle());
for (Element child : e.getChildren()) {
doMenu(child, s);
}
s.append("</menu:item>");
}
then I call
s = new StringBuffer();
doMenu(rootElement, s);
and I add the <menu:menu> at begining and end.

Ok, so now what I *currently* do is write that string to a file, which
is included where needed. This is not best practice (and impossible if I
use a WAR file). I have no idea how I can do this in JSP, as there does
not seem any way of making recursive calls using a taglib.

The ideal thing would be:
<rec:defineTemplate name="mytemplate" var="e">
<menu:item>
${e.title}
<c:forEach items="${e.children}" var="child">
<rec:callTemplate name="mytemplate" value="${child}" />
</c:forEach>
</menu:item>
</rec:defineTemplate>

<menu:menu>title
<rec:callTemplate name="mytemplate" value="${rootElement}" />
</menu:menu>

Do you see what I mean?
I do not believe any such taglib exists (but I may be wrong). Plus, I
think this would completely break my taglib unless I can be sure the
<rec:> taglib is evaluated before any of the <menu:> tags...

If you can see a way of doing this, please help !

Thanks.

Regards.
 
O

Owen Jacobson

Here is the problem:
I have a custom taglib I use to create menus. For example I can do

<menu:menu>Title
<menu:item>first item</menu:item>
<menu:item>second item is a submenu
<menu:item>subitem 1</menu:item>
<menu:item>subitem 2</menu:item>
<menu:item>subitem 3</menu:item>
</menu:item>
<menu:item>third item</menu:item>
</menu:menu>
With as many levels of submenus as I want. The result uses javascript and
stuff to create a windows style dropdown menu.

Now, I have a database containing persistent objects (using Hibernate)
which have a hierarchy, i.e. each object has a set of children (in fact
it's more complicated than that, but this is enough to solve my problem
here).

So in my Java I do this:

void doMenu(Element e, StringBuffer s) {
s.append("<menu:item>");
s.append(e.getTitle());
for (Element child : e.getChildren()) {
doMenu(child, s);
}
s.append("</menu:item>");
}
then I call
s = new StringBuffer();
doMenu(rootElement, s);
and I add the <menu:menu> at begining and end.

Ok, so now what I *currently* do is write that string to a file, which is
included where needed. This is not best practice (and impossible if I use
a WAR file).

Not to mention a horrible idea from a concurrency standpoint. If two
requests that generate different menus occur simultaneously, you may end
up with the menu output incorrect, corrupted, or missing entirely
depending on all sorts of timing factors.

What about creating a tag which does the full menu generation for an
Element? Presumably the menu:menu tag already contains logic for
producing the HTML for a given menu item; perhaps that code could be
refactored into a set of methods that do menu generation which are
available to other classes as well.
 
T

Tom Arne Orthe

Owen said:
On Tue, 19 Apr 2005 23:06:04 +0200, yzzzzz wrote:

[-----8<----]
Ok, so now what I *currently* do is write that string to a file, which is
included where needed. This is not best practice (and impossible if I use
a WAR file).


Not to mention a horrible idea from a concurrency standpoint. If two
requests that generate different menus occur simultaneously, you may end
up with the menu output incorrect, corrupted, or missing entirely
depending on all sorts of timing factors.

What about creating a tag which does the full menu generation for an
Element? Presumably the menu:menu tag already contains logic for
producing the HTML for a given menu item; perhaps that code could be
refactored into a set of methods that do menu generation which are
available to other classes as well.

I have to go with the last writer here. It's obvious that the
custom tag menu you use are only suitable for static menu structures
and you end up trying to pound a square peg through a round hole.

I once saw an article (can't remember where and can't find it on Google)
where someone had their jsp code in a database (structured some way).
They manually called Jasper to compile the jsp and then the java
compiler to compile it into a class. They also had to extend the
classloader of the container they were using so it would load the
resulting class. I would go for creating a new custom tag as suggested
by Owen Jacobson.


Regards,
Tom Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top