• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

methods generating html output in jsp

 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For servlet or simple classes, we can easily modularized our class by using methods.

However, the jsp section I want to cutout (called many times in the main jsp method), outputs html (e.g. tags). It wont compile.

One solution I hate but did was to use jsp fragments.

Jsp Include only allows strings for parameters. I need to pass an object.

Will a method work, if we do servlet-style stuff on the method? Like printing the html output using jsp 'out' implicit object? Or maybe getting the printwriter?
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you investigated tag files?
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could add a method to a JSP, but you really can't reuse a method in a JSP; at least not in other JSPs.

What about developing a custom tag? You create a custom tag, such as

<mytage:doSomething/>

And when the JSP hits that tag, something gets printed out. Essentially, this tag links back to a Java method that can spit out the HTML tags that you desire.

Also, in the code for the custom tag, you have access to things like the HttpSession and ServletContext. If you have an object, you can stuff those objects into the HttpSession, and retrieve it in the custom tag.

And of course, you can write one tag, but use it in all of your JSP files.

Does this sound like a feasable solution? Custom tags were added to the API to address these very types of issues.

Cheers!

-Cameron
 
Bear Bibeault
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kameron McKenzie:
Custom tags were added to the API to address these very types of issues.



Yes, exactly why I mentioned them.
 
reply
    Bookmark Topic Watch Topic
  • New Topic