Traffic Manager Extensions to the Servlet API
The HTTP Servlet specification states that the doGet() and doPost() methods are passed in two arguments, an HttpServletRequest object (commonly named req) and an HttpServletResponse object (commonly named res).
The Traffic Manager’s implementation passes in two subclassed objects of type ZXTMHttpServletRequest and ZXTMHttpServletResponse.
These implementations have several additional fields and methods used to access the additional capabilities in the Java Extensions API. To use these fields and methods, cast the req and res objects to the Traffic Manager subtype, as shown below:
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import com.zeus.ZXTMServlet.*;
public class MyServlet extends HttpServlet
{
public void doGet( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException
{
ZXTMHttpServletRequest zreq = (ZXTMHttpServletRequest) req;
ZXTMHttpServletResponse zres = (ZXTMHttpServletResponse) res;
// Base Servlet API does not need to provide the ability to
// query the content type of a response
if( zres.getContentType() == null ||
!zres.getContentType().equalsIgnoreCase("text/html") )
{
return; // We’re not interested in non-html data
}
// Count how many times a html page has been sent
// with SNMP counter 1
zreq.incrementCounter( 1 );
BufferedReader in = zres.getReader();
PrintWriter out = zres.getWriter();
String current = null;
while( ( current = in.readLine() ) != null ) {
if( current.indexOf( "<title>" ) != -1 ) {
current = "<title>My New Title</title>";
}
out.println( current );
}
}
}
Note the following points about this example:
•This example increments a counter every time a request for an HTML page is made.
The counter value can be viewed through the Traffic Manager’s SNMP interface. The Servlet also alters the content of the HTML page, changing its title to “My New Title”.
•The example highlights one of the main differences between standard Servlets and Java Extensions. Specifically, Java Extensions have the ability to manipulate data received from other sources (in this case a Web server); whereas a normal Servlet is designed to only produce data.
Since the Java Extension alters data in a response, run the Java Extension from a TrafficScript response.
The online help contains Javadoc-style documentation for the Traffic Manager’s extensions to the Servlet API. To access online help, use one of the following methods:
•Access the Traffic Manager Admin UI, and press the Help button.
•Click the Manuals button on the on the Tool bar.
•Select the Java API Documentation link.
Modifying Responses and Writing Data From a Java Extension
It is possible to run several Java Extensions or TrafficScript rules to process a response before the response is written back to the client.
However, once a Java Extension begins modifying the response data (for example, using the PrintWriter.println() function), data is streamed back to the client. At this point, the HTTP headers are flushed to the client and you cannot make any more modifications to the HTTP headers.
Only one Java Extension may modify a response. You cannot modify response data using several Java Extensions in succession. Do not run any Java Extensions once the response data is written to the client.