aspose file tools*
The moose likes Servlets and the fly likes useBean-like bean instantiation behavior from a Servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "useBean-like bean instantiation behavior from a Servlet?" Watch "useBean-like bean instantiation behavior from a Servlet?" New topic
Author

useBean-like bean instantiation behavior from a Servlet?

Greg Whelan
Ranch Hand

Joined: May 18, 2000
Posts: 52
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 ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
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

Joined: May 18, 2000
Posts: 52
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).
Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
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
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: useBean-like bean instantiation behavior from a Servlet?