Creating a WebFigure on a JSP Page
There are several ways to use WebFigures on a Web page with Java:
Return a special HTML string from the servlet which
embeds the WebFigure into a page.
Using the custom WebFigure tag library directly from
the JSP, have the servlet bind the WebFigure and redirect it to the
JSP.
Generate a string from the middle tier that can be
dumped directly onto a Web page at the front end, embedding all of
the WebFigure functionality and the correct callbacks directly into
the front-end page.
In each case, the WebFigure object is stored in the
Web server's session. The Java script that executes the client calls
back to the server for updates and, using the cached WebFigure, new
updates are sent back to the client.
Using an HTML String
This example is very similar to what you can find in Deploying a Java Component Over the Web, but this
example also uses the DAO. The middle tier code in Hosting the DAO with a Servlet is repeated
here:
HttpSession session = request.getSession();
WebFigure userPlot =
(WebFigure)session.getAttribute("UserPlot");
// if this is the first time doGet has been called for
// this session,
// create the plot and WebFigure object
if (userPlot== null)
{
userPlot = examples.getWebFigureFromMWJavaObjectRef();
// store the figure in the session context
session.setAttribute("UserPlot", userPlot);
// bind the figure's lifetime to the session
session.setAttribute(
"UserPlotBinder",
new MWHttpSessionBinder(userPlot));
}
Using the WebFigure Tag Library
To use the WebFigure object directly from a JSP page, reference the webfigures tag
library. This creates a WebFigure object with your object's parameters.
The UserPlot is the name of the Web object that
you placed in the cache by the middle tier.
Note
Host the middle tier and the JSP on the same server. |
<%@ taglib prefix="wf" uri="/WEB-INF/webfigures.tld" %>
< wf:web-figure
name="UserPlot"
scope="session"
root="WebFigures"
width="100%"
height="100%"/>
When using this approach, the only other code needed on the
servlet is a redirect back to the JSP that the above code resides
on. In this example, this code is contained in response.jsp.
The servlet code would look like this:
RequestDispatcher dispatcher =
request.getRequestDispatcher("/response.jsp");
dispatcher.forward(request, response);
Using Embedded HTML
This option is convenient since all of the "heavy lifting"
is done on the server and only a string is sent to the front end.
In this example, notice how the servlet is merely referenced and dumps
the contents into a Web page frame.
Tip
This technique can be used regardless of the transfer protocol
or location of the front end or back end. Since a simple string is
being sent, you can code the front-end in a number of ways, adapting
nicely to a SOAP transfer, for example. |
<iframe
src ="http://localhost:8080/Examples/ExamplesServlet?
function=webFigureEmbedded"
width="590"
height="480">
</iframe>
To generate this string, run code similar to this on the servlet:
// The argument to the WebFigureHtmlGenerator
// constructor is the URL where the
// WebFigures servlet is mapped (relative to the Web a
// pplication and Servlet context)
WebFigureHtmlGenerator wfHtmlGenerator =
new WebFigureHtmlGenerator("WebFigures",
getServletContext());
String embeddedString;
try
{
//This generates a string that can be sent to the
// response that represents the WebFigure.
embeddedString =
wfHtmlGenerator.getFigureEmbedString(
userPlot,
"UserPlot",
"session",
null,
null,
null));
}
catch(MWException mwe)
{
throw new Exception();
}
response.getOutputStream().println(embeddedString);
 | Working with the Front-End Layer | | Creating a WebFigure on an ASPX Page |  |
Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
Get the Interactive Kit