Adding to what Casey said, below, I suggest you also take a look at the Tag
Extension Framework in JSP 1.1. A custom action (tag) gets access to
all servlet objects (request, response, session, context) automatically,
so it can extract all information it needs (parameters, headers, etc.).
It is also invoked automatically, so no scriptlet code is needed to
make it do what you want. Custom actions can of course be combined with
JavaBeans. Here's a simple example:

  <%!-- Capture the user input in a bean --%>
  <jsp:useBean id="user" class="com.mycomp.UserInfoBean" >
    <jsp:setProperty name="user" property="*" />
  </jsp:useBean>

  <%!--
    Validate the bean data with a custom action. If it's not
    valid, forward to an error page, otherwise continue.
  --%>
  <foo:validateUser name="user" forwardOnError="invalid.jsp" />

  <%!-- Save the validated info in a database --%>
  <foo:saveUser name="user" />

Without the custom actions, the bean would typically have to
implement the validation and saving methods as well, and the page
would look something like this:

  <%!-- Capture the user input in a bean --%>
  <jsp:useBean id="user" class="com.mycomp.UserInfoBean" >
    <jsp:setProperty name="user" property="*" />
  </jsp:useBean>

  <%!--
    Validate the bean data. If it's not valid, forward to an
    error page, otherwise continue.
  --%>
  <% if (!user.isValid()) %>
    <jsp:forward page="invalid.jsp" />
  <% } %>

  <%!-- Save the validated info in a database --%>
  <% user.saveData() %>


The difference is not striking in this simple example, but the version
with scriptlets is still more error prone. It's easy to miss one of
the parenthesis in the if statement, or the scriptlet with the closing
bracket, especially if you're a page author not used to programming.
A page authoring tool is also better suited to deal with the custom
action alternative and can help with the syntax, making sure all mandatory
attributes are defined, etc.

Hans

---------
JSP Insider wrote:
>
> Hi Keith
> If you are using the bean approach. Passing data from the page is very
> simple and quick an example:
>
> <jsp:useBean id="ReportBean" scope="request" class="MyPackage.MyBean"/>
> <jsp:setProperty name="ReportBean" property="rowDisplayCount"
> param="sle_size"/>
>
> The nice thing about this is the Param field directly takes the
> output from your query string/ form data being posted to your page
> and send the data straight to your bean property.
>
> In fact if you name your property in the bean to match the form field
> exactly you
> can rewrite the above to look like this
>
> <jsp:useBean id="ReportBean" scope="request" class="MyPackage.MyBean"/>
> <jsp:setProperty name="ReportBean" property="sle_size" />
>
> Also note there is wild card approach so I can rewrite once more as the
> following
>
> <jsp:useBean id="ReportBean" scope="request" class="MyPackage.MyBean"/>
> <jsp:setProperty name="ReportBean" property="*" />
>
> and if you all your form fields name match to your bean property names
> This will pump all of the form/query data straight into your bean.
>
> You can also rewrite these to look like the following
>
> <jsp:useBean id="ReportBean" scope="request" class="MyPackage.MyBean"/>
> <jsp:setProperty name="ReportBean" property="rowDisplayCount"
> param="sle_size"/>
> </jsp:useBean>
>
> The difference in syntax being  that the properties are being sent to the
> bean during
> JSP pages initialization of the bean
>
> Now I prefer using the first approach since I like being able to see
> on my page what�s going in my bean , I find it easier to debug when I can
> see everything happening, the wildcard approach can be dangerous since you
> are assuming everything you need is aligned and this can make debugging
> problems
> harder.
>
> ************************************************************************
>
> Now which approach is better in JSP design. They are both good.
> My preferences are base upon available resources and complexity of the web
> site.
> SO when I am the only Java developer, or when the customer
> is light on the Java approach. I like to use beans and JSP. I can
> make the beans quickly. They are easy to plug in and out
> of your JSP pages. I also prefer this approach on smaller simple sites
> since you won't have a huge number of beans to maintain / juggle
>
> If I have access to several serious Java developers then switching
> to a more centralize servlet system to handle your site will give you
> more options in your site I feel. For extremely Large Sites and More
> complicated
> processing the servlet approach would be better. With the centralize
> servlet approach you can optimize your site to a greater degree and reuse
> more
> elements and have a finer degree of control on how your site is being
> processed.
> Now I must also admit I prefer to program smaller sites SO I currently stick
> to the first approach.
>
> Casey Kochmer
> WWW.JSPInsider.com
> [EMAIL PROTECTED]
>
> >From: keith kwiatek <[EMAIL PROTECTED]>
> >Reply-To: A mailing list about Java Server Pages specification and
> >     reference <[EMAIL PROTECTED]>
> >To: [EMAIL PROTECTED]
> >Subject: Can you access servlet info through jsp bean?
> >Date: Sun, 9 Jul 2000 10:15:53 -0400
> >
> >Hello,
> >
> >Pardon the newbie question, but as I understand it, when designing jsp
> >applications, you have two options:
> >1) call the jsp page directly, and then reference a bean
> >2) call a servlet that instantiates the bean for display in a jsp page.
> >
> >Off-hand I think I like option 1 best, call the jsp page, which references
> >a bean. I like the idea of putting everything in a "bean framework" that
> >can then be used by the HTML people....
> >
> >QUESTION: Using option #1 above, it seems that I must put all the servlet
> >environment stuff into the jsp page (session, get/post).... Is this
> >correct? Can someone show me how to put it into a bean that can then be
> >referenced by the JSP? Is this a good design? If not, what is?
> >
> >Thanks for any and all help,
> >Please post and/or email me [EMAIL PROTECTED]
> >Keith

--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com

===========================================================================
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