Java Reference
In-Depth Information
which will do, in effect, the following:
<% myvar.setTotal("1234"); %>
So this would hardly seem worth it, but there are other syntax constructs
that make this much more powerful. Remember that we're working with Web-
based stuff, with a JSP that will be invoked via a URL. That URL may have
parameters on it, and we can map those parameters onto a bean's proper-
ties—that is, connect the parameters to setters for a given bean. We replace the
value attribute with a parameter attribute, for example:
<jsp:setProperty name="myvar" property="total" parameter="newtot" />
which works the same as:
<% myvar.setTotal ( request.getParameter("newtot") ); %>
We can take that one step further and map all the parameters that arrive
in the URL to setters in one step:
<jsp:setProperty name="myvar" parameter="*" />
So if you design your JSP and your HTML well, you can get a lot done
automatically for you. One other thing going on behind the scenes that we've
glossed over is the type of the argument to the setter. The parameters all
come in as String s. However, if your setter's type is a Java primitive, it will
automatically convert to that type for you, instead of just passing you String s.
One final twist on using beans is the duration of the bean and its values.
If you don't specify otherwise (and we have yet to show you syntax to do other-
wise) your bean will be around for the duration of the request, at which time
it will be available to be garbage-collected. Any values in the bean will not be
there on the next visit to that URL (i.e., the next call to that servlet).
Here is the syntax to make that bean last longer:
<jsp:useBean id="myvar" class="net.multitool.servlet.AccountBean"
scope="session" />
which will make it stay for the duration of the session. You may remember (or
you can flip back and look up) how we created and used session variables in
the servlet. The same mechanism is at work here, but behind the scenes. You
Search WWH ::




Custom Search