here's an example that uses a bean to create a table of prime numbers. if there is a parameter named numdigits in the request data, it is passed into the bean's numdigits property. likewise for numprimes.
jspprimes.jsp
to download the jsp source, right click on the source code link. you can also download the source code for thenumberedprimes bean referenced by the jsp:usebean element. browse the source code directory for other java classes used by numberedprimes. the best way to try it out on-line is to start with the html page that acts as a front end to it. <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>reusing javabeans in jsp</title>
<link rel=stylesheet
href="my-style-sheet.css"
type="text/css">
</head>
<body>
<center>
<table border=5>
<tr><th class="title">
reusing javabeans in jsp</table>
</center>
<p>
<jsp:usebean id="primetable" class="hall.numberedprimes" />
<jsp:setproperty name="primetable" property="numdigits" />
<jsp:setproperty name="primetable" property="numprimes" />
some <jsp:getproperty name="primetable" property="numdigits" />
digit primes:
<jsp:getproperty name="primetable" property="numberedlist" />
</body>
</html>
here's a typical result:
8.5 the jsp:getproperty action
this element retrieves the value of a bean property, converts it to a string, and inserts it into the output. the two required attributes are name, the name of a bean previously referenced via jsp:usebean, and property, the property whose value should be inserted. here's an example; for more examples, see sections 8.2 and 8.4. <jsp:usebean id="itembean" ... />
...
<ul>
<li>number of items:
<jsp:getproperty name="itembean" property="numitems" />
<li>cost of each:
<jsp:getproperty name="itembean" property="unitcost" />
</ul>
8.6 the jsp:forward action
this action lets you forward the request to another page. it has a single attribute, page, which should consist of a relative url. this could be a static value, or could be computed at request time, as in the two examples below. <jsp:forward page="/utils/errorreporter.jsp" /> <jsp:forward page="<%= somejavaexpression %>" />
8.7 the jsp:plugin action
this action lets you insert the browser-specific object or embed element needed to specify that the browser run an applet using the java plugin.
9. comments and character quoting conventions
there are a small number of special constructs you can use in various cases to insert comments or characters that would otherwise be treated specially. here's a summary:| syntax | purpose |
|---|---|
<%-- comment --%> | a jsp comment. ignored by jsp-to-scriptlet translator. any embedded jsp scripting elements, directives, or actions are ignored. |
<!-- comment --> | an html comment. passed through to resultant html. any embedded jsp scripting elements, directives, or actions are executed normally. |
<\% | used in template text (static html) where you really want "<%".
|
%\> | used in scripting elements where you really want "%>".
|
\' | a single quote in an attribute that uses single quotes. remember, however, that you can use either single or double quotes, and the other type of quote will then be a regular character. |
\" | a double quote in an attribute that uses double quotes. remember, however, that you can use either single or double quotes, and the other type of quote will then be a regular character. |
%\> | %> in an attribute.
|
<\% | <% in an attribute. |