Interpreting JSP code

Discussion in 'Java' started by yzzzzz, Apr 19, 2005.

  1. yzzzzz

    yzzzzz Guest

    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
    yzzzzz, Apr 19, 2005
    #1
    1. Advertising

  2. yzzzzz wrote:
    > 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
    Tom Arne Orthe, Apr 19, 2005
    #2
    1. Advertising

  3. yzzzzz

    yzzzzz Guest

    Tom Arne Orthe wrote:
    > 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.
    yzzzzz, Apr 19, 2005
    #3
  4. On Tue, 19 Apr 2005 23:06:04 +0200, yzzzzz wrote:

    > Tom Arne Orthe wrote:
    >
    >> 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).


    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.
    Owen Jacobson, Apr 21, 2005
    #4
  5. Owen Jacobson wrote:
    > 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
    Tom Arne Orthe, Apr 21, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alec S.
    Replies:
    5
    Views:
    659
    Alec S.
    Sep 11, 2004
  2. Replies:
    0
    Views:
    4,325
  3. Chris Saunders

    Interpreting some code

    Chris Saunders, Jul 23, 2003, in forum: C Programming
    Replies:
    5
    Views:
    343
    Dan Pop
    Jul 24, 2003
  4. Chris Saunders

    Interpreting some C code

    Chris Saunders, Feb 6, 2004, in forum: C Programming
    Replies:
    3
    Views:
    554
    Vijay Kumar R Zanvar
    Feb 6, 2004
  5. zolli

    Interpreting mem_map init code

    zolli, Mar 27, 2005, in forum: C Programming
    Replies:
    4
    Views:
    423
    CBFalconer
    Mar 28, 2005
Loading...

Share This Page