Writing the JWAVE Wrappers
As with any JWAVE application, a JWaveJSPServlet application relies on
JWAVE wrapper functions to perform the data and graphical analysis functions. This section describes a typical wrapper for a JWaveJSP application and discusses three related wrapper utilities.
With JWaveJSPServlet applications, there are three PV‑WAVE functions that you will use in your wrappers. If you examine the loan.pro wrapper, you will see these three routines in use:
PACKIMAGE
PACKTABLE
SETIMAGESIZE
These routines are located in:
(UNIX) RW_DIR/jwave-3_6/lib
(WIN) RW_DIR\jwave-3_6\lib
where RW_DIR is the main Rogue Wave installation directory.
PACKIMAGE Function
This function takes as its parameters the name of an associative array and the name of an image or plot. This routine simply packages the 2D byte array representing the image or plot and three 1D byte arrays representing the RGB color values for the graphic. These variables are stored in an associative array which is returned to the calling procedure. The calling procedure then returns these items to the servlet.
This function has one keyword, Ismap, that lets you specify if the returned image is an image map.
PACKTABLE Function
This function takes string data and converts it into an HTML table format. The HTML table data is converted to a 1D byte array. The array is then stored in an associative array which is returned to the calling procedure. The calling procedure then returns this data to the servlet.
The HTML table data is converted to byte because of a limitation in the size of strings that JWAVE can handle. In the servlet, this byte array is converted back into a string, and it is then forwarded to a JSP page for the client to display.
SETIMAGESIZE Procedure
This procedure sets the size of the image appropriately for the current PV‑WAVE device driver.
Example
This example JWaveJSPServlet demonstration uses the JSP
RW_DIR/classes/jwave_demos/loan.jsp and the wrapper RW_DIR/jwave-3_6/lib/user/loan.pro.
More JSP demonstration files are located in:
(UNIX) RW_DIR/jwave_demos/jsp
(WIN) RW_DIR\jwave_demos\jsp
where RW_DIR is the main Rogue Wave installation directory.
Wrappers for this and other JWaveJSPServlet demonstrations are located in:
(UNIX) RW_DIR/jwave-3_6/lib/user
(WIN) RW_DIR\jwave-3_6\lib\user
where RW_DIR is the main Rogue Wave installation directory.
Parameters are sent by loan.jsp to the wrapper loan.pro using standard HTML inputs and calls to request.getParameter and request.getAttribute, enclosed in scriptlet tags ("<%" and "%>"). The first time loan.jsp is invoked, each call to request.getParameter wrapper is null; this causes default values to be generated for each input field, but the wrapper loan.pro is not yet called. Then, when the Calculate button is pressed, the parameters entered for Loan Amount $, Interest Rate %, and Number of Years are passed to the wrapper, which is defined to be loan.pro in the SUBMIT parameter list.
<BODY BGCOLOR="white">
<H2><CENTER>Loan Analysis using Java Server Pages and JWave</CENTER></H2>
<FORM METHOD=POST ACTION=/jwavejsp/jspdemos>
<TABLE>
<TR><TH>
<TABLE><FONT SIZE=3>
<TR ALIGN=RIGHT><TH>Loan Amount $</TH>
<TH><INPUT NAME=amount TYPE=TEXT VALUE=
<% if ( request.getParameter("wrapper") != null ) { %>
<%= request.getAttribute("AMOUNT") %>
<% } else { %>
100000
<% } %>
SIZE=8></TH></TR>
<TR ALIGN=RIGHT><TH>Interest rate %</TH>
<TH><INPUT NAME=interest TYPE=TEXT VALUE=
<% if ( request.getParameter("wrapper") != null ) { %>
<%= request.getAttribute("INTEREST") %>
<% } else { %>
8.0
<% } %>
SIZE=8></TH></TR>
<TR ALIGN=RIGHT><TH>Number of Years</TH>
<TH><INPUT NAME=years TYPE=TEXT VALUE=
<% if ( request.getParameter("wrapper") != null ) { %>
<%= request.getAttribute("YEARS") %>
<% } else { %>
10
<% } %>
SIZE=8></TH></TR>
<INPUT NAME="jsp" TYPE=HIDDEN VALUE="/jsp/loan.jsp">
<INPUT NAME="wrapper" TYPE=HIDDEN VALUE=loan>
<TR ALIGN=RIGHT><TH></TH>
<TH><INPUT TYPE=SUBMIT VALUE="Calculate"></TH></TR>
</FONT></TABLE>
</TH><TH>
<% if ( request.getParameter("wrapper") != null ) { %>
<%= request.getAttribute("PRINCPLOT") %>
<%= request.getAttribute("INTPLOT") %>
<% } %>
</TH></TR>
</TABLE>
</FORM>
<% if ( request.getParameter("wrapper") != null ) { %>
<TABLE><FONT SIZE=3>
<TR ALIGN=RIGHT><TH>Monthly Payment $</TH>
<TH><%= request.getAttribute("PAYMENT") %></TH></TR>
<TR ALIGN=RIGHT><TH>Interest Cost $</TH>
<TH><%= request.getAttribute("COST") %></TH></TR>
</FONT></TABLE>
<%= request.getAttribute("SCHEDULE") %>
<% } %>
</BODY>
</HTML>
This wrapper uses the getParam function to retrieve input that was sent from the servlet. The parameters are then processed and several results are generated, including a table and two plots. The PackImage and PackTable routines are used to store graphical and tabular results in an associative array, which is then passed back to the servlet when the function returns. The results are shown in
Figure 10-3: Loan Analysis using Java Server Pages and JWAVE.
FUNCTION Loan, client_data
; Get params from HTML FORM
amount = GetParam(client_data, ‘AMOUNT’, /Value)
interest = GetParam(client_data, ‘INTEREST’, /Value)
years = GetParam(client_data, ‘YEARS’, /Value)
; Calculate results
result = CalcLoan(amount, interest, years)
; Convert 2D float array to an HTML table
schedule = result(‘schedule’)
collab = [‘Month’,’Principal Due’,’Principal’,’Interest’, $
‘Interest Cost’,’Total Payments’,’Monthly Payment’]
PackTable, TRANSPOSE(schedule), ‘SCHEDULE’, ret, $
/Right, ColLabels=collab, $
Border=1, Caption=’Loan Schedule’
TEK_COLOR
; Create principal paid plot
SetImageSize, 200, 200
PlotLoan, ‘principal’
PackImage, ret, ‘princplot’
; Create interest cost plot
SetImageSize, 200, 200
PlotLoan, ‘interest’
PackImage, ret, ‘intplot’
; Return original values to be used to fill in form
ret(‘AMOUNT’) = amount
ret(‘INTEREST’) = interest
ret(‘YEARS’) = years
; Return calculated values and loan schedule table
ret(‘PAYMENT’) = result(‘payment’)
ret(‘COST’) = result(‘interest_cost’)
RETURN, ret
END