Chapter 8. A Servlet with Input

A Servlet with Input

Starting with the example from the previous lesson, let's extend it to handle and react to user input.

Here's the file layout for this lesson:


servlet-input
|-- org
|   `-- tvt
|       `-- examples
|           `-- ServletInput.java
|-- package-in.xml
`-- resources
    |-- icon.gif
    |-- services.xml
    |-- servlet-input.xoe-config
    `-- shortcuts.xml

The package-in.xml file is similar to before, with just a different package name:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE package PUBLIC "-//TVT//DTD XOEPKG 1.0//EN" "http://www.xoe.org/DTD/xoe-package.dtd">

<package name="servlet-input" version="1.0" task='true'>
  <info>
    <name>Servlet input example</name>
    <description>A simple package demonstrating servlets that handle input.</description>
    <maintainer>
      <name>Jim Pick</name>
      <email>jim@transvirtual.com</email>
    </maintainer>
    <copyright>
      <year>2001</year>
      <holder>Transvirtual Technologies Inc.</holder>
    </copyright>
    <licence type="open" common="GPL" />
  </info>

  <requires>
    <dep name='xhtml' predepends='true' ns='http://www.xoe.org/installer/base/package'/>
    <dep name='xoe-servlet' predepends='true' ns='http://www.xoe.org/installer/base/package'/>
  </requires>

</package>

Here's the services.xml file (just name changes):


<?xml version="1.0" encoding="UTF-8"?>
<!-- $tvt: servletinput.xml,v 1.1 2001/12/18 00:22:15 alex Exp $ -->
<services>
  <service class='org.tvt.examples.ServletInput' config='servlet-input.xoe-config'/>
</services>

Here's the servlet-input.xoe-config file, which contains the configuration options for the service:


<?xml version="1.0"?>
<xoe-config xmlns="http://xoe.org/ns/2001"
            name="Servlet Input Example" 
            icon="icon.gif"
	    class="org.tvt.examples.ServletInput" />

The shortcuts.xml file looks like:


<?xml version="1.0" encoding="UTF-8"?>
<shortcuts>
  <lnk file="servlet-input.xoe-config" />
</shortcuts>

In this example, there is no index.xhtml file containing static XHTML. Instead, it is all generated dynamically by the Java code.

Here's the code from ServletInput.java:


/*
 * Copyright (c) 2001 Transvirtual Technologies, Inc.  
 * All rights reserved. See the file "COPYING" for details.
 */ 
package org.tvt.examples;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.xoe.core.dom.DOMlet;
import org.xoe.core.dom.XDocument;
import org.xoe.servlet.XoeServlet;


public class ServletInput
  extends XoeServlet
{
  private String color = "#ffffff";

  public void doGet (HttpServletRequest req, HttpServletResponse res) 
    throws ServletException, IOException
  {
    String path = req.getPathInfo ();

    if ("set".equals (path))
      {
        color = req.getParameter ("color");
      }  
    display (res);
  }

  private void display (HttpServletResponse res)
    throws IOException
  {
    String view = "<?xml version=\"1.0\"?>\n" +
      "<!DOCTYPE html PUBLIC \"-//TVT//DTD XOEVIEW 1.0//EN\" " +
      "\"http://www.xoe.org/DTD/xoe-view.dtd\">\n" + 
      "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
      "  <body bgcolor=\"" + 
      color +
      "\">\n" +
      "    <p><b>Change Color</b></p>\n" +
      "    <p>Choose color and press set</p>\n" +
      "    <form method=\"GET\" action=\"set\" >\n" +
      "      <input type=\"radio\" value=\"#ff0000\" name=\"color\" />Red<br/> " +
      "      <input type=\"radio\" value=\"#00ff00\" name=\"color\" />Green<br/> " +
      "      <input type=\"radio\" value=\"#0000ff\" name=\"color\" />Blue<br/> " +
      "      <input type=\"radio\" value=\"#ffffff\" name=\"color\" />White<br/> " +
      "      <input type=\"submit\" value=\"Set Color\" />\n" +
      "    </form>\n" +
      "  </body>\n" + 
      "</html>\n";

    res.setContentType ("text/html");
    XDocument xdoc = DOMlet.create (view.toCharArray (), "index.xhtml");
    res.getWriter ().print (xdoc);
  }  
}