- Server-Side VoiceXML Development with Java Servlets and PHP
- Setting Up a Servlet Environment
- PHP Scripting
Setting Up a Servlet Environment
The following software is required:
You'll need a JDK to compile your servlets (and to execute them through the servlet engine if you don't have an existing Java Runtime Environment installed). For the examples in this article, we'll be using the Sun JDK. After running through the installation program and selecting the installation directory, you'll need to modify your environment to update your PATH and set CLASSPATH and JAVA_HOME. Here's a sample batch file that can be run from the command line to do this:
env.bat REM jdkinstalldir is where you installed the jdk, i.e., c:\jdk1.3.1 set JAVA_HOME=<jdkinstalldir> set PATH=%JAVA_HOME%\bin;%PATH% set CLASSPATH=.
The important thing to remember in setting your CLASSPATH is that it shouldn't point to a previous JDK installation's classes.zip file. These settings can be made manually by opening a command console to the DOS prompt and entering each command individually. They can also be made to apply to every DOS window you open by permanently modifying your environment. To do this, choose Start, Settings, Control Panel and select System. Select the Environment tab (NT) or Advanced tab (2000) and modify or enter the environment settings in env.bat. Note that as you add more components, you'll need to modify env.bat to reflect their paths.
A JRun developer's edition is available from Macromedia without charge. To install JRun, download and execute jrun-31-win-us.exe. This leads you through a standard Windows installation where you can modify default installation directories and program groups. You'll also be asked for the location of your Java installation and a server port number. When the installation is complete, access http://localhost:8100 (assuming that you accepted the default port number during installation). Your installation was successful if you see Index of / and two horizontal lines.
As mentioned earlier, JRun and similar servlet engines can be added to web server software in production environments, though we're not doing so here for the sake of simplicity.
Simple Example
Now that you have a server configured to support Java servlets, we'll walk through a simple example to test your configuration. First, we'll look at the "Hello World" application to be viewed from a web browser, then we'll make some minor modifications for the voice web.
To begin, create a file HelloWorld.java with the following code:
HelloWorld.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet( HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); // Output HTML out.println("<html><head><title>Hello world!"); out.println("</title></head><body>"); out.println("<h1>Hello world!</h1>"); out.println("<p>This is written from a servlet.</p>"); out.println("</body></html>"); } }
Now compile this into a class file, by entering the following at a command prompt:
javac HelloWorld.java
Put the resulting class file in a directory where JRun can serve it, <jruninstalldir>\servers\default\default-app\WEB-INF\classes. Now you should be able to access the page from your web browser using the URL http://localhost:8100/servlet/HelloWorld, again assuming that you accepted the default server ports.
Now modify the example slightly to output VoiceXML rather than HTML:
HelloVoiceWorld.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloVoiceWorld extends HttpServlet { public void doGet( HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/xml"); PrintWriter out = res.getWriter(); out.println("<?xml version=\"1.0\"?> " ); out.println("<vxml version=\"1.0\">"); out.println(" <form>"); out.println(" <block>"); out.println(" Hello voice world!"); out.println(" Spoken from a servlet."); out.println(" </block>"); out.println(" </form>"); out.println("</vxml>"); } }
The preceding two servlets do very little, but there are a couple of things to notice about the code. First, the call to HttpServletResponse.setContentType is changed to reflect the different type of output returned. For some VoiceXML gateways this may need to be "text.vxml".
To run this example, you have three options:
Using one of the VSPs, provision your application using its online tools linking to a web server you can configure and control.
Do the same with your ISP account, if your service provider supports servlet development. If not, investigate some of the free Java servlet hosts that are financed by advertising, such as WebAppCabaret. If you follow this approach, make sure that you understand how banner advertisements may be inserted in the output of your servlets.
Use a desktop simulator until you're ready to deploy an application.
Finally, don't forget your web browser. Part of developing voice applications is knowing which applications are right for voice, and debugging software is not one of them. Follow an incremental approach by testing servlets with a browser before attempting to use a VSP. You can get a lot of dynamic VoiceXML debugging done by accessing your servlets in a web browser and viewing the source produced. Just point your web browser at the servlet, supply any parameters, and view the output (which can be pasted into a static VoiceXML file for testing with the phone in a pinch).