CGI Support

The javaxt-server library makes it easy to host CGI applications. In most cases, all you need to do is create a Servlet that extends the CgiServlet and set the path to the CGI executable in the constructor.

Here's an example of a CGI Servlet based on UMN Mapserver. In this example, all you have to do is pass the path to the Mapserver executable and you're off.

package www.kartographia.com;
import javaxt.http.servlet.*;

public class MapServer extends CgiServlet {

    public MapServer(){
        super(new java.io.File("D:\\Software\\GDAL\\FWTools2.2.8\\bin\\mapserv.exe"));
    }
}
Here's a slightly more sophisticated example of a CGI Servlet based on UMN Mapserver. In this example, we are masking the "map" querystring parameter by overriding the getParameters() method. This example is useful because Mapserver expects a file path to a map file. However most admins really don't want to expose any information of the server's file system.

package www.kartographia.com;
import javaxt.http.servlet.*;
import java.util.HashMap;

//******************************************************************************
//**  UMN MapServer
//*****************************************************************************/
/**
 *   CGI Servlet used to generate maps using the UMN Mapserver.
 *
 ******************************************************************************/

public class MapServer extends CgiServlet {

    private final static HashMap<String, String> maps = new HashMap<String, String>();
    private final static String QUERY_STRING = "QUERY_STRING=";
    

  //**************************************************************************
  //** Constructor
  //*************************************************************************/
  /** Creates a new instance of this class.
   */
    public MapServer(){
        super(new java.io.File("D:\\Software\\GDAL\\FWTools2.2.8\\bin\\mapserv.exe"));
        maps.put("world.map", "D:\\Maps\\world.map"); //<--Remember to update the "wms_onlineresource" to point to the right URL!
    }


  //**************************************************************************
  //** getParameters
  //*************************************************************************/
  /** Overrides the native getParameters() method by updating the "map"
   *  parameter found in the requested URL. This allows us to hide the
   *  physical location of the map file.
   */
    protected java.util.ArrayList<String> getParameters(HttpServletRequest request){
        java.util.ArrayList<String> params = super.getParameters(request);

      //Find querystring in the list of CGI parameters
        String queryString = null;
        for (String param : params){
            if (param.startsWith(QUERY_STRING)){
                queryString = param.substring(QUERY_STRING.length());
                break;
            }
        }

      //Set the "map" variable
        javaxt.utils.URL url = new javaxt.utils.URL("http://test?" + queryString);
        url.setParameter("map", maps.get("world.map"));

      //Update the CGI parameters
        params.remove(QUERY_STRING + queryString);
        params.add(QUERY_STRING + url.getQueryString());
        return params;
    }

}