13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

436Chapter 19JSP: Servlets Turned Inside Outwhich will do, in effect, the following:So this would hardly seem worth it, but there are other syntax c<strong>on</strong>structsthat make this much more powerful. Remember that we’re working with Webbasedstuff, with a JSP that will be invoked via a URL. That URL may haveparameters <strong>on</strong> it, and we can map those parameters <strong>on</strong>to a bean’s properties—thatis, c<strong>on</strong>nect the parameters to setters for a given bean. We replace thevalue attribute with a parameter attribute, for example:which works the same as:We can take that <strong>on</strong>e step further and map all the parameters that arrivein the URL to setters in <strong>on</strong>e step:So if you design your JSP and your HTML well, you can get a lot d<strong>on</strong>eautomatically for you. One other thing going <strong>on</strong> behind the scenes that we’veglossed over is the type of the argument to the setter. The parameters allcome in as Strings. However, if your setter’s type is a Java primitive, it willautomatically c<strong>on</strong>vert to that type for you, instead of just passing you Strings.One final twist <strong>on</strong> using beans is the durati<strong>on</strong> of the bean and its values.If you d<strong>on</strong>’t specify otherwise (and we have yet to show you syntax to do otherwise)your bean will be around for the durati<strong>on</strong> of the request, at which timeit will be available to be garbage-collected. Any values in the bean will not bethere <strong>on</strong> the next visit to that URL (i.e., the next call to that servlet).Here is the syntax to make that bean last l<strong>on</strong>ger:which will make it stay for the durati<strong>on</strong> of the sessi<strong>on</strong>. You may remember (oryou can flip back and look up) how we created and used sessi<strong>on</strong> variables inthe servlet. The same mechanism is at work here, but behind the scenes. You

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!