• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

useBean-like bean instantiation behavior from a Servlet?

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a more elegant way to instantiate a bean from a servlet than the approach I'm using below:

That piece of servlet code is intended to have the same behavior as the useBean action:

I'm trying to reuse an existing bean instantiation mechanism, but the only one I can find is from the Tomcat runtime (the JspRuntimeLibrary is a class i n jasper-runtime.jar).
The above works, but I'd prefer to not have to package jasper-runtime.jar with my application (nothing against jasper).
[ April 19, 2002: Message edited by: Greg Whelan ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could always set up the bean manually - it just takes a couple of lines of code per parameter after all.
Bill
 
Greg Whelan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Certainly I could, however the same argument could be made for useBean in JSPs... but it's a handy concise construct for mapping from an HTML form to Java bean. Changes to the Java bean do not require corresponding servlet changes (specifically: adding new properties). And I'm hoping to have those same benefits in a servlet.
I'll note that I found it interesting that Tomcat implements useBean using reflection at servlet execution time (thus my ability to utilize JspRuntimeLibrary), but Orion performs reflection of the bean at JSP translation time (thus producing a servlet with code similar to the type you suggest).
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How about providing an extra constructor that takes a HttpServletRequest. The constructor can then inspect the Request for the parameters it requires.
If you don't want to couple a HttpServletRequest to a bean then you could create a helper class to sit between the request and bean objects.
The advantage of these approaches is that you don't have to change and recompile the servet if the bean changes.
Any comments on this approach (good or bad) would be welcomed as we have used it in our projects.
Thanks,
Steve
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like Steve's approach as an alternate way to code initialization in a non-bean style. That way you can switch implementations without changing anything else. Since reflection involves a lot more computing, I wonder if it would make a noticible difference in execution speed?
Bill
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic