Web Development with JavaServer Pages

Скачать в pdf «Web Development with JavaServer Pages»


<% GameGrid grid = GameGrid.getGameGrid();


Recognizer r1 = new Recognizer(new Coordinates(grid, 0, 0));


Recognizer r2 = new Recognizer(new Coordinates(grid, 100, 100)); r1.findProgram(«Flynn»); r2.findProgram(«Flynn»); %>


This scriptlet fetches one object via a class method, which it then uses to instantiate two new objects. Methods are then called on these objects to initiate some computation.


Note that a page’s scriptlets will be run for each request received by the page. For the previous example, this means that two instances of the Recognizer class are created every time the JSP page containing this scriptlet is requested. Furthermore, any variables introduced in a scriptlet are available for use in subsequent scriptlets and expressions on the same page (subject to variable scoping rules). The foregoing scriptlet, for example, could be followed by an expression such as the following:


<%= r1.statusReport() %>


This expression would then insert the results of the statusReport() method call for instance r1 into the page’s output. Later scriptlets or expressions could make additional references (such as method calls, or inclusion in argument lists) to this instance and the r2 instance, as well the grid object.


If you wish to control the scoping of a variable introduced by a scriptlet, you can take advantage of JSP’s support for leaving code blocks open across multiple script-lets. Consider, for example, the following JSP page which reproduces the above scriptlet, with one small but important modification:


<html>


<body>


<h1>Intruder Alert</h1>


<p>Unauthorized entry, dispatching recognizers…</p>


<% GameGrid grid = GameGrid.getGameGrid();

Скачать в pdf «Web Development with JavaServer Pages»