Kevin,

I'm not sure of the performance costs of reflection, but I do have two
methods
I use in my servlets to perform the kind of task you envision.  I've
included them
below.

Performance wise, the cost is probably greater at the servlet level, rather
than
the JSP since the JSP engine can do all the reflection at JSP compile time,
spitting
out, if needed, the 32 lines which a servlet writer would have to do
manually.  (I
don't believe all the engines actually do this, but it is a possibility.)

-AMT

<CODE>

  /**
   * Set a particular property of a bean with the value of a parameter
   * from a servlet request.  The property is set if, and only if, the bean
has
   * such a property, and the request has a value corresponding to the
parameter
   *
   * @param request the request to process
   * @param name the bean which must have a property set
   * @param property the bean property to set
   * @param param the request parameter containing the value to be used
   */
  public void setProperty(ServletRequest request,
                          Object name,
                          String property,
                          String param) {
    Method[] theMethods = name.getClass().getMethods();

    // Capitalize first letter of the property
    String properProperty =   property.subString(0,0).upperCase()
                + property.subString(1)

    String value = request.getParameter(param);
    if ((value == null) || value.equals("")) return; //No such property or
blank

    for (int i=0; i<theMethods.length(); i++) {
      if (theMethods[i].getName().equalsIgnoreCase("set" + property)) {
        Object[] valueArray = {value};
        try {
          theMethods[i].invoke(name, valueArray);
        } catch (IllegalAccessException e) {

          // This should never happen because all beans with which this
          // method is called should belong to us.

          throw new RuntimeException("An IllegalAccessException occurred "
            + "when trying to invoke method set" + properProperty + " "
            + "in bean " + name.getClass().getName() + "!");

        } catch (InvocationTargetException e) {

          // According to the bean spec, setter methods should throw only
          // runtime exceptions.  Therefore, any exceptions wrapped by
          // the InvocationTargetException will be a runtime exception.

          throw new RuntimeException("A RuntimeException occurred "
            + "when trying to invoke method set" + properProperty + " "
            + "in bean " + name.getClass().getName() + ".  This "
            + "exception is described as: "
            + e.getTargetException().toString());

        }
      }
    }
  }

  /**
   * Set properties of a bean based on parameter values in a request.  It is
   * assumed that property names match parameter values.
   *
   * @param request the request to process
   * @param name the bean which must have a property set
   * @param property name of the property to be set with a value
corresponding
   *        to the parameter of the same name within the request.  As a
special
   *        case, if the property name supplied is &quot;*&quot; all request
   *        parameters will be process and a corresponding bean property
   *        will be set if it exists.
   */
  public void setProperty(ServletRequest request, Object name,
              String property) {
    if (property.equals("*") {
      Enumeration pNames = request.getParameterNames();
      while(pNames.hasMoreElements()) {
        String nextParam = pNames.nextElement();
        setProperty(request, name, nextParam, nextParam);
      }
    } else {
      setProperty(request, name, property, property);
    }
  }

</CODE>

> -----Original Message-----
> From: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of Kevin Duffey
> Sent: Tuesday, April 11, 2000 8:34 AM
> To: [EMAIL PROTECTED]
> Subject: Re: More on Model 2/little confused
>
>
> Hi again,
>
>
> I am curious if there is a better way to get many form/pages worth of
> request parameters in a better fashion than each action having from 1 to
> infinte number of
>
> bean.setXXX( request.getParameter("XXX") );
>
> On my Enrollment page I had 32 lines of this, which isn't a big deal..but
> its killing for a loop of some kind! I am not familiar with how to do
> reflection, but it would be great if I could implement a way to
> do what the
> Model 1 JSP/JavaBean does, by taking the request parameter, and
> applying it
> to a "set" string and get the method to call. Can anyone explain how to do
> this? I will dig up some dirt on it..but if anyone has done this using
> reflection the way JSP engines do it, that would be great.
>
> On that note, is using reflection a lot slower than the multiple
> request.getParameter() calls? I don't want to eat up cpu just to
> save a few
> lines of code..not to mention most java developers, especially beginners,
> may not know reflection upon seeing it in use, thus its a bit harder to
> understand. Oh..I would ideally want a single method that all
> action classes
> can use, possibly a static one, to turn the request parameters
> into a bunch
> of setXXX calls and actually call the setXXX methods of each
> action classes
> bean instance.
>
> Thanks.
>
> ==================================================================
> =========
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to