< Zurück | Inhalt | Weiter >

19.3.6 JavaBeans in JSP

For those who really want to avoid doing any Java coding inside of a JSP, there is additional syntax that will provide for a lot of capability but without having to explicitly write any Java statements. Instead, you write a lot of arcane JSP directives, as we’ll show you in just a bit. Is this any better? In some ways yes, but in other ways, no, it’s just different syntax.

What we’ll be able to do with this additional syntax is:

1. Instantiate a Java class and specify how long it should be kept around

2. Get values from this class

3. Set values in this class


1. The newer XML-style syntax would be useful if your JSP are generated by an XSLT stylesheet or are validated against a DTD, both topics being beyond the scope of our discussion.

The syntax looks like this:

<jsp:useBean id="myvar" class="net.multitool.servlet.AccountBean" />

which will create a variable called myvar as an instance of the AccountBean

class found in the net.multitool.servlet package. Think of this as:

<%! import net.multitool.servlet.AccountBean; %>

<% AccountBean myval = new AccountBean(); %>

So can AccountBean be any class? Well, sort of. It can be any class that you want, as long as it is a bean. It doesn’t have to end in “Bean”, but it does have to be a class which has:

• A null constructor (you may have noticed there is no syntax to support arguments to the constructor on the useBean statement).

• No public instance variables.

• Getters and setters for instance variables.

• Getters and setters named according to a standard: getTotal() or isTotal() and setTotal() for a variable called total (isTotal() would be used if we had a boolean getter, that is, if the getter returned a boolean; otherwise it would expect getTotal() as the getter’s name).

Otherwise, its a normal class. These restrictions mean that you can call the class a “JavaBean” or just “bean,” and there is additional JSP syntax to manipu- late the class. Specifically:

<jsp:getProperty name="myvar" property="total" />

will do, in effect, the following:

<%= myvar.getTotal() %>


<% out.print(myvar.getTotal()); %>

Similarly, we can set a value in the JSP with this syntax:

<jsp:setProperty name="myvar" property="total" value="1234" />

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 Strings. However, if your setter’s type is a Java primitive, it will automatically convert to that type for you, instead of just passing you Strings. 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

only use the specific syntax in the useBean tag, and it does the rest (getting and storing) for you.