ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/program/bsfscene.htm

317 lines
14 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Scenario: Create a Bean Scripting Framework application</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h3><a name="bsfscene"></a>Scenario: Create a Bean Scripting Framework application</h3>
<p>Programming skills in JavaScript code are more prevalent than programming skills using JavaServer Pages (JSP) tags. Using the Bean Scripting Framework, JavaScript programmers can gradually introduce JSP tags in their JavaScript applications without completely rewriting the source code. The BSF method not only reduces the potential of programming errors, but also provides a painless way to learn a new technology.</p>
<p>This scenario illustrates how to implement a BSF application using JavaScript within JSP tags.</p>
<p><strong>Develop the BSF application</strong></p>
<p>At ABC elementary school, John Doe teaches third grade mathematics. He wants to help his students memorize their multiplication tables and thinks a small Web-based quiz could help meet his objective. However, John Doe only knows JavaScript.</p>
<p>Using the Bean Scripting Framework to help leverage his JavaScript skills, John Doe creates two JSP files, multiplication_test.jsp and multiplication_scoring.jsp.</p>
<p>In the multiplication_test.jsp file, John Doe uses both client-side and server-side JavaScript code to generate a test of 100 random multiplication questions, displayed using a three minute timer. He then writes the multiplication_scoring.jsp file to read the data submitted by the multiplication_test.jsp file and to generate the scoring results.</p>
<p>John Doe creates these two files:</p>
<p><strong>multiplication_test.jsp</strong></p>
<pre> &lt;html&gt;
&lt;head&gt;
&lt;title&gt;Multiplication Practice Test&lt;/title&gt;
&lt;script language=&quot;javascript&quot;&gt;
var countMin=3;
var countSec=0;
function updateDisplay (min, sec) {
var disp;
if (min &lt;= 9) disp = &quot; 0&quot;;
else disp = &quot; &quot;;
disp += (min + &quot;:&quot;);
if (sec &lt;= 9) disp += (&quot;0&quot; + sec);
else disp += sec;
return(disp);
}
function countDown() {
countSec--;
if (countSec == -1) {
countSec = 59;
countMin--;
}
document.multtest.counter.value = updateDisplay(countMin, countSec);
if((countMin == 0) &amp;&amp;(countSec == 0)) document.multtest.submit();
else var down = setTimeout(&quot;countDown();&quot;, 1000);
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body bgcolor=&quot;#ffffff&quot; onLoad=&quot;countDown();&quot;&gt;
&lt;%@ page language=&quot;javascript&quot; %&gt;
&lt;h1&gt;Three Minute Multiplication Drill&lt;/h1&gt;
&lt;hr&gt;
&lt;h2&gt;Remember: this is an opportunity to excel!&lt;/h2&gt;
&lt;p&gt;
&lt;form method=&quot;POST&quot; name=&quot;multtest&quot; action=&quot;multiplication_scoring.jsp&quot;&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Time left:
&lt;input type=&quot;text&quot; name=&quot;counter&quot; size=&quot;9&quot; value=&quot;03:00&quot; readonly&gt;
&lt;/h3&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit for scoring!&quot;&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;table border=&quot;1&quot;&gt;
&lt;%
var newrow = 0;
var q_num = 0;
function addQuestion(num1, num2) {
if (newrow == 0) out.println(&quot;&lt;tr&gt;&quot;);
out.println(&quot;&lt;td&gt;&quot;);
out.println(num1 + &quot; x &quot; + num2 + &quot; = &quot;);
out.println(&quot;&lt;/td&gt;&lt;td&gt;&quot;);
out.print(&quot;&lt;input name=\&quot;&quot; + q_num + &quot;|&quot; + num1 + &quot;:&quot; + num2 + &quot;\&quot; &quot;);
out.println(&quot;type=\&quot;text\&quot; size=\&quot;10\&quot;&gt;&quot;);
out.println(&quot;&lt;/td&gt;&quot;);
if (newrow == 3) {
out.println(&quot;&lt;/tr&gt;&quot;);
newrow = 0;
}
else newrow++;
q_num++;
}
for (var i = 0; i &lt; 100; i++) {
var rand1 = Math.ceil(Math.random() * 12);
var rand2 = Math.ceil(Math.random() * 12);
addQuestion(rand1, rand2);
}
%&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p><strong>multiplication_scoring.jsp</strong></p>
<pre> &lt;html&gt;
&lt;head&gt;
&lt;title&gt;Multiplication Practice Test Results&lt;/title&gt;
&lt;/head&gt;
&lt;body bgcolor=&quot;#ffffff&quot;&gt;
&lt;%@ page language=&quot;javascript&quot; %&gt;
&lt;h1&gt;Multiplication Drill Score&lt;/h1&gt;
&lt;hr&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;table border=&quot;1&quot;&gt;
&lt;tr&gt;&lt;th&gt;Problem&lt;/th&gt;&lt;th&gt;Correct Answer&lt;/th&gt;&lt;th&gt;Your Answer&lt;/th&gt;&lt;/tr&gt;
&lt;%
var total_score = 0;
function score (current, pos1, pos2) {
var multiplier = current.substring(pos1 + 1, pos2);
var multiplicand = current.substring(pos2 + 1, current.length());
var your_product = request.getParameterValues(current)[0];
var true_product = multiplier * multiplicand;
out.println(&quot;&lt;tr&gt;&quot;);
out.println(&quot;&lt;td&gt;&quot; + multiplier + &quot; x &quot; + multiplicand + &quot; = &lt;/td&gt;&quot;);
out.println(&quot;&lt;td&gt;&quot; + true_product + &quot;&lt;/td&gt;&quot;);
if (your_product == true_product) {
total_score++;
out.print(&quot;&lt;td bgcolor=\&quot;\#00ff00\&quot;&gt;&quot;);
}
else {
out.print(&quot;&lt;td bgcolor=\&quot;\#ff0000\&quot;&gt;&quot;);
}
out.println(your_product + &quot;&lt;/td&gt;&quot;);
out.println(&quot;&lt;/tr&gt;&quot;);
}
var equations = request.getParameterNames();
while(equations.hasMoreElements()) {
var currElt = equations.nextElement();
var splitPos1 = currElt.indexOf(&quot;|&quot;);
var splitPos2 = currElt.indexOf(&quot;:&quot;);
if (splitPos1 &gt;=0 &amp;&amp; splitPos2 &gt;= 0) score(currElt, splitPos1, splitPos2);
}
%&gt;
&lt;/table&gt;
&lt;h2&gt;Total Score: &lt;%= total_score %&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;a href=&quot;multiplication_test.jsp&quot;&gt;Try again?&lt;/a&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>Perform these steps to see how John Doe uses BSF to implement JavaScript in a JSP application:</p>
<ol>
<li><p><strong>Give your files a .jsp extension.</strong></p></li>
<li><p><strong>Use server-side JavaScript code in your application.</strong>
<br>The multiplication_test.jsp file incorporates both client-side and server-side JavaScript. Server-side JavaScript is similar to client-side JavaScript; the primary difference consists of using a different set of objects. Whereas client-side Javascript programmers invoke document and window objects, server-side JavaScript programmers, using the Bean Scripting Framework, invoke a set of objects provided by the JSP technology. Also, client-side scripts are enclosed in &lt;script&gt; tags, but server-side scripts use JSP scriptlet and expression tags. </p>
<p>Examine the following blocks of code:</p>
<pre> &lt;script language=&quot;javascript&quot;&gt;
var countMin=3;
var countSec=0;
function updateDisplay (min, sec) {
var disp;
if (min &lt;= 9) disp = &quot; 0&quot;;
else disp = &quot; &quot;;
disp += (min + &quot;:&quot;);
if (sec &lt;= 9) disp += (&quot;0&quot; + sec);
else disp += sec;
return(disp);
}
function countDown() {
countSec--;
if (countSec == -1) {
countSec = 59;
countMin--;
}
document.multtest.counter.value = updateDisplay(countMin, countSec);
if((countMin == 0) &amp;&amp; (countSec == 0)) document.multtest.submit();
else var down = setTimeout(&quot;countDown();&quot;, 1000);
}
&lt;/script&gt;
...
&lt;body bgcolor=&quot;#ffffff&quot; onLoad=&quot;countDown();&quot;&gt;
...
&lt;form method=&quot;POST&quot; name=&quot;multtest&quot; action=&quot;multiplication_scoring.jsp&quot;&gt;
...
&lt;input type=&quot;text&quot; name=&quot;counter&quot; size=&quot;9&quot; value=&quot;03:00&quot; readonly&gt;
...</pre>
<p>The JavaScript code contained in the &lt;script&gt; block implements a timer set within the &lt;input&gt; field named counter. The onLoad event handler in the &lt;body&gt; tag causes the browser to load and execute the code when the page is loaded.</p>
<p>The document.multtest.submit() statement causes the form named multtest to be submitted when the timer expires.</p></li>
<li><p><strong>Identify the code to the BSF function.</strong>
<br>This code example, from the multiplication_test.jsp file, displays the use of a JSP directive. This directive tells the WebSphere Express BSF function that this file is using the JavaScript language, and that the JavaScript code is enclosed by the &lt;% ... %&gt; scriptlet tags. The out implicit JSP object in this code example, creates the body section of a table from 100 randomly generated questions.</p>
<pre> ...
&lt;%@ page language=&quot;javascript&quot; %&gt;
...
&lt;%
var newrow = 0;
var q_num = 0;
function addQuestion(num1, num2) {
if (newrow == 0) out.println(&quot;&lt;tr&gt;&quot;);
out.println(&quot;&lt;td&gt;&quot;);
out.println(num1 + &quot; x &quot; + num2 + &quot; = &quot;);
out.println(&quot;&lt;/td&gt;&lt;td&gt;&quot;);
out.print(&quot;&lt;input name=\&quot;&quot; + q_num + &quot;|&quot; + num1 + &quot;:&quot; + num2 + &quot;\&quot; &quot;);
out.println(&quot;type=\&quot;text\&quot; size=\&quot;10\&quot;&gt;&quot;);
out.println(&quot;&lt;/td&gt;&quot;);
if (newrow == 3) {
out.println(&quot;&lt;/tr&gt;&quot;);
newrow = 0;
}
else newrow++;
q_num++;
}
for (var i = 0; i &lt; 100; i++) {
var rand1 = Math.ceil(Math.random() * 12);
var rand2 = Math.ceil(Math.random() * 12);
addQuestion(rand1, rand2);
}
%&gt;
...</pre></li>
<li><p><strong>Read the results.</strong>
<br>To score the results of the practice drill, John Doe uses the request implicit JSP object in the multiplication_scoring.jsp file to obtain the POST data created within the &lt;form&gt; tags in the multiplication_test.jsp file.</p>
<p>The multiplication_scoring.jsp file uses the POST data to build an output file containing the original question, the student's answer, and the correct answer, and then prints the text in a table format using the out implicit object.</p>
<p>The following code example from the multiplication_scoring.jsp file illustrates the use of the request and out JSP objects:</p>
<pre> ...
&lt;%@ page language=&quot;javascript&quot; %&gt;
...
&lt;%
var total_score = 0;
function score (current, pos1, pos2) {
var multiplier = current.substring(pos1 + 1, pos2);
var multiplicand = current.substring(pos2 + 1, current.length());
var your_product = request.getParameterValues(current)[0];
var true_product = multiplier * multiplicand;
out.println(&quot;&lt;tr&gt;&quot;);
out.println(&quot;&lt;td&gt;&quot; + multiplier + &quot; x &quot; + multiplicand + &quot; = &lt;/td&gt;&quot;);
out.println(&quot;&lt;td&gt;&quot; + true_product + &quot;&lt;/td&gt;&quot;);
if (your_product == true_product) {
total_score++;
out.print(&quot;&lt;td bgcolor=\&quot;\#00ff00\&quot;&gt;&quot;);
}
else {
out.print(&quot;&lt;td bgcolor=\&quot;\#ff0000\&quot;&gt;&quot;);
}
out.println(your_product + &quot;&lt;/td&gt;&quot;);
out.println(&quot;&lt;/tr&gt;&quot;);
}
var equations = request.getParameterNames();
while(equations.hasMoreElements()) {
var currElt = equations.nextElement();
var splitPos1 = currElt.indexOf(&quot;|&quot;);
var splitPos2 = currElt.indexOf(&quot;:&quot;);
if (splitPos1 &gt;=0 &amp;&amp; splitPos2 &gt;= 0) score(currElt, splitPos1, splitPos2);
}
%&gt;
...
&lt;h2&gt;Total Score: &lt;%= total_score %&gt;&lt;/h2&gt;
...</pre>
<p><strong>Note:</strong> Although using separate scriptlet blocks of code for different portions of a conditional expression is common in JSP files implemented in Java, it is invalid for JSP files implemented using JavaScript through the Bean Scripting Framework. The JavaScript code must be entirely contained within the scriptlet tags.</p>
<p>The following code example illustrates invalid usage:</p>
<pre> &lt;% if (pass == 0) %&gt;
&lt;i&gt;pass is true&lt;/i&gt;
&lt;% else %&gt;
&lt;i&gt;pass is not true&lt;/i&gt;</pre></li>
</ol>
<p><strong>Deploy the BSF application</strong></p>
<p>You assemble and deploy BSF applications in the same manner as JSP applications.</p>
<p>Deploy the BSF code examples in WebSphere Express to view this applications processing and output. Use these quick steps to deploy the application.</p>
<p><strong>Note:</strong> The intent of these &quot;quick steps&quot; is to provide you with instant application output. However, the supported method for deployment is the same as for standard JSP files.</p>
<p>Perform these steps:</p>
<ol>
<li><p>Add your BSF files to your application using the WebSphere Development Studio Client. Copy your JSP files to your application directory.</p></li>
<li><p>Start the application server.</p></li>
<li><p>Open a browser and request your BSF application. Use the following URL to request your application:</p>
<pre>http://<em>your.server.name</em>:<em>port</em>/<em>jspFileName</em>.jsp</pre>
<p>where <em>your.server.name</em> is the host name of your server, <em>port</em> is the port number, and <em>jspFileName</em> is the name of your JSP file.</p></li>
</ol>
</body>
</html>