Ram Anantha wrote:

> Hi,
>
> I have a bunch of servlets which essentially do the following (usually in
> the doPost method)
> - parse the input parameters
> - based on the values of the parameters, get appropriate data from the
> database
> - output the html along with the database data back to the browser
>
> I would like to use JSP to do the above.
>
> What's the best way to do it? If I have to convert this into a bean, how can
> I do it?
>

My suggestion would be to keep the servlet as the receiver of the form input, with
the following logic:
* Use the request URI or input parameters to decide
  which action class you should execute (this lets
  you use a single servlet for the app).
* Call a perform() method on an instance of that
  action class to perform the required database
  lookups and so on (adding new actions is configured
  with servlet initialization parameters).
* The action stores the results it has gathered as
  either request attributes (if they apply only to
  this request) or session attributes (if they need
  to be saved for this user across requests)
* The action uses RequestDispatcher.forward()
  to forward control to the appropriate JSP page
  for displaying the results.

The JSP page then sees the results as beans with a scope of "request" or "session"
(depending on where the action class put them).

Among the advantages of this approach:
* Complete separation of business logic (in the
  action classes and beans) and display logic
  (in the JSP page).
* Ability to provide more than one presentation
  of the same underlying logic without rewriting
  the logic.
* Ability to assign JSP page creation tasks to
  people who don't know Java, and servlet/action/bean
  creation tasks to people who don't know HTML.

This design model was called "Model 2" in the 0.92 version of the JSP spec, and is
quite popular for designing scalable, maintainable, web-based applications.  It
also conforms to the commonly used Model-View-Controller design pattern
popularized in interactive applications, with the action classes and beans
representing the Model, the JSP pages comprising the View, and the servlet acting
as the Controller.

The Java2 Enterprise Edition (J2EE) section of JavaSoft's web site
(http://java.sun.com/j2ee) contains a "Best Practices" manual, and sample
application, that utilize this approach, although IMHO they make a bad mistake by
using a JSP page as the "controller" rather than a servlet.

>
> Thanks for the help.
>
> Ram
>

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to