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 ]
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).
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
Author and all-around good cowpoke
Joined: Mar 22, 2000
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