The IlvGanttServlet and IlvGanttServletSupport classes
Describes how to create a servlet and how the servlet responds to different requests.
Explains how to create a servlet that can produce an image and send it to the client.
Explains how the servlet can respond to two different types of HTTP requests.
Explains how to create a chart and/or a data model and store them as parameters of a HTTP session.
Creating a servlet
The server side of a
JViews Gantt JavaScript Web application consists in creating a servlet that can produce an image and send it to the client. The
JViews Gantt JavaScript Web application support provides a predefined servlet to achieve this task. The predefined servlet class is named
IlvGanttServlet. This class, which can be found in the package
ilog.views.gantt.servlet, is an abstract subclass of the
HTTPServlet class from the Java™ servlet API.
Using the
IlvGanttServlet class is an easy way to create a servlet, but it has one main drawback. You cannot use it to add support for the
JViews Gantt JavaScript Web application protocol to an existing servlet. This is the purpose of the
IlvGanttServletSupport class. The
IlvGanttServletSupport class implements all the
JViews Gantt JavaScript Web application server-side functionality. In fact, the
IlvGanttServlet class is just a basic wrapper around an instance of
IlvGanttServletSupport. The
doGet method of
IlvGanttServlet simply calls the
handleRequest method of its
IlvGanttServletSupport instance.
In the same way, you can integrate an instance of
IlvGanttServletSupport into your own servlet to handle the requests coming from the Gantt client side. In our Gantt Servlet example, the code of the servlet can be rewritten using the
IlvGanttServletSupport class as follows:
import ilog.views.gantt.*;
import ilog.views.gantt.servlet.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GanttChartServlet extends HttpServlet
{
private IlvGanttServletSupport support;
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
support = new ServletSupport();
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
if (!support.handleRequest(request, response))
throw new ServletException("Unrecognized request");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
class ServletSupport extends IlvGanttServletSupport
{
private IlvHierarchyChart _chart;
public ServletSupport()
{
_chart = createChart();
}
private IlvHierarchyChart createChart()
{
IlvHierarchyChart chart = new IlvGanttChart();
IlvGanttModel ganttModel = new SimpleProjectDataModel();
return chart;
}
public IlvHierarchyChart getChart(HttpServletRequest request,
IlvServletRequestParameters params)
throws ServletException
{
return _chart;
}
}
In this code you have created a new servlet class,
GanttChartServlet, that is derived directly from the
HttpServlet class. The
doGet method passes the requests to an instance of the
IlvGanttServletSupport class for handling.
The servlet parameters
The JViews Gantt servlet support can respond to two different types of HTTP requests, the image request and the capabilities request. The image request returns an image from the Gantt or Schedule chart. The capabilities request returns information to the client, such as the number of rows visible in the chart, the height of the rows, and the minimum and maximum times for horizontally scrolling the Gantt sheet. This information allows the client to know the capabilities of the chart in order to intelligently scroll and manipulate the chart images. When developing the client side of your application, you can use the JavaScript™ scripts provided by the Gantt Web application support. The scripts create the HTTP request for you, so you do not really need to write the HTTP request yourself.
The image request
The image request produces an image from the chart. Here is an example of a request for the image of the table portion of a chart, assuming that myservlet is the name of the servlet:
http://host/servlets/myservlet?request=image
&comp=table
&width=300
&height=250
And here is an example for the image of the Gantt sheet portion of a chart:
http://host/servlets/myservlet?request=image
&comp=sheet
&width=500
&height=250
&startTime=2001,0,1
&endTime=2001,5,30
The Gantt sheet displays the time period from January 1, 2001 to June 30, 2001. A detailed listing of the image request parameters can be found in the
Java API Reference Manual for the
IlvGanttServletSupport class.
The capabilities request
The capabilities request tells the servlet to return the capabilities information about the chart to the client. The capabilities request has the following syntax:
http://host/servlets/myservlet?request=capabilities
&format=(html|octet-stream)
[ &onload= <a string> ]
The format parameter tells the servlet which format should be used to return the capabilities information. Two formats are supported, HTML or Octet stream.
The HTML format is used when the client is a JavaScript client. In this case, the result is an empty HTML page that contains some JavaScript™ code. The JavaScript code is executed on the client side, and some information variables are then available.
The octet-stream format is used when the client is a Java™ applet. In this case, the result is a stream of octets. The data is produced using a java.io.DataOutput and can be read using a java.io.DataInput.
Full details on the capabilities request and the information returned by the server can be found in the
Java API Reference Manual for the
IlvGanttServletSupport class. Details on how the client-side JavaScript components save and use the capabilities information can be found in the
JavaScript Reference Manual for the
IlvGanttComponentView.getCapabilities method.
Multiple sessions
The Gantt Servlet example presented a very simple example that creates a single Gantt chart for the servlet. This means that all calls to the servlet (that is, all clients) are looking at the same chart and the same data model. In some applications, you may want to have a chart and/or a separate data model for each client. In this case, you might use the notion of HTTP sessions. You can then create a chart and/or a data model and store them as parameters of the session.
Here you take our Gantt Servlet example and modify it slightly so that each client has its own chart that is viewing a common data model. This way, each user can toggle rows to expand and collapse without affecting the charts viewed by the other clients. You use an instance of the
IlvGanttSessionAttribute class to store the chart as an attribute of the HTTP session. This class handles the details of properly disposing server-side GUI components when the user session expires. Our updated
IlvGanttServletSupport implementation now looks like this:
class ServletSupport extends IlvGanttServletSupport
{
private IlvGanttModel _model;
private IlvHierarchyChart createChart()
{
synchronized(this) {
if (_model == null)
_model = new SimpleProjectDataModel();
}
IlvHierarchyChart chart = new IlvGanttChart();
chart.setGanttModel(_model);
chart.getGanttSheet().setVerticalGrid(new WeekendGrid());
... more chart customizations ...
return chart;
}
/**
* Returns the chart used for the specified request.
* @param request The current HTTP request.
* @param params The parameters parsed from the request.
*/
public IlvHierarchyChart getChart(HttpServletRequest request,
IlvServletRequestParameters params)
throws ServletException
{
IlvHierarchyChart chart = null;
HttpSession session = request.getSession();
if (session.isNew()) {
chart = createChart();
IlvGanttSessionAttribute chartProxy =
new IlvGanttSessionAttribute(chart);
session.setAttribute("IlvHierarchyChart", chartProxy);
} else {
IlvGanttSessionAttribute chartProxy =
(IlvGanttSessionAttribute)session.getAttribute("IlvHierarchyChart");
if (chartProxy != null)
chart = chartProxy.getChart();
}
if (chart == null)
throw new ServletException("session problem");
return chart;
}
}
NOTE If you store the chart directly as an attribute of the HTTP session, it will not be properly garbage-collected when the session expires. You must wrapper the chart in an instance of IlvGanttSessionAttribute to ensure that the chart is properly disposed of.
Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.