Accessing a Proxy CGI file using JavaScript on a JSP, NetBeans and Glassfish
===

I am writing a web application which uses a mixture of JSPs and the OpenLayers JavaScript API. One of the functionalities I am trying to add is an address search geocoding function.

I have discovered that in order to get this to work I need to create a proxy.cgi file within the WEB-INF folder of my glassfish server and access the proxy.cgi file via the OpenLayers javascript. Unfortunately since the file is stored in the WEB-INF directory it is not accessible using the filepath.

Can anyone give me a way around this, my code is pasted below, its probably a bit messy but I am new to all this.

Thanks in advance for your help.

Peter


    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@page import="sitescout.SiteLatLong"%>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Draw-A-Site</title>  
        
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyC23WNIU05mVpo6AXS1xbjELFktzfN6lWg&sensor=false">
        </script>
    <script type="text/javascript" src="http://openlayers.org/api/OpenLayers.js"></script>
    <script src="proj4js-combined.js" type="text/javascript" charset="utf-8"></script>
    
    <script type="text/javascript">
    
    function init() {
        
    OpenLayers.ProxyHost = "http://localhost:8080/SiteScout/cgi/proxy.cgi?url=";
    var apiKey = "AvODYPXEMckQbV_uA0FBbNTuOCu9ZUPTR0ZknUd1XVRZcOMyJpKOzF9fTrv4nZAw";
    
    var map = new OpenLayers.Map("map", {maxExtent: new OpenLayers.Bounds(
                        -128 * 156543.03390625,
                        -128 * 156543.03390625,
                        128 * 156543.03390625,
                        128 * 156543.03390625),
      projection: new OpenLayers.Projection("EPSG:900913"),
      displayProjection: new OpenLayers.Projection("EPSG:4326")
    });
    
    var road = new OpenLayers.Layer.Bing({
    key: apiKey,
    type: "Road"
    });
    
    var aerial = new OpenLayers.Layer.Bing({
    key: apiKey,
    type: "Aerial"
    });
    
    var hybrid = new OpenLayers.Layer.Bing({
    key: apiKey,
    type: "AerialWithLabels",
    name: "Bing Aerial With Labels"
    });
    
    var vector_layer = new OpenLayers.Layer.Vector('Basic Vector Layer');
    
    var natparks = new OpenLayers.Layer.WMS(
    "National Parks",
    "http://localhost:8080/geoserver/wms",
    {layers:'national_park',
    transparent:true,
    projection:("EPSG:900913")},
    {isBaseLayer:false}
    );
    
    map.addLayers([vector_layer,natparks,road,aerial, hybrid]);
    map.addControl(new OpenLayers.Control.LayerSwitcher());
    map.addControl(new OpenLayers.Control.MousePosition());
    map.addControl(new OpenLayers.Control.EditingToolbar(vector_layer));
    
    var proj = new OpenLayers.Projection("EPSG:4326");
    var point = new OpenLayers.LonLat(-4,56);
    map.setCenter(point.transform(proj, map.getProjectionObject()),5);
    }
    
    function getWKT() {
        document.getElementById('polyInput').value=map.layers[0].features[0].geometry.toString();
        }
    
    function submitform() {
                var queryString = document.forms[0].query.value;
                OpenLayers.Request.POST({
                    url: "http://www.openrouteservice.org/php/OpenLSLUS_Geocode.php",
                    scope: this,
                    failure: this.requestFailure,
                    success: this.requestSuccess,
                    headers: {"Content-Type": "application/x-www-form-urlencoded"},
                    data: "FreeFormAdress=" + encodeURIComponent(queryString) + "&MaxResponse=1"
                });
            }
    function requestSuccess(response) {
                var format = new OpenLayers.Format.XLS();
                var output = format.read(response.responseXML);
                if (output.responseLists[0]) {
                    var geometry = output.responseLists[0].features[0].geometry;
                    var proj = new OpenLayers.Projection("EPSG:4326");
                    var foundposition = new OpenLayers.LonLat(geometry.x, geometry.y);
                    map.setCenter(foundposition.transform(proj, map.getProjectionObject()),12);
                } else {
                    alert("Sorry, no address found");
                }
            }
    function requestFailure(response) {
                alert("An error occurred while communicating with the OpenLS service. Please try again.");
            }
    
    </script>
        </head>
    <body onload="init()">
    <h1>Draw - A - Site</h1>
    <h4>Use the Tools on the map to draw your site boundary, then fill in the form at the bottom of the page.</h4>
    <form name="input" action="javascript: submitform();" method="post">
        <label for="query">Search for address:</label> <input type="text" id="query" size=50 name="query"
                                                              value="Rue des Berges 37 Payerne"/>
        <input type="submit" value="Submit"/>
    </form>
    <div id="map" style="width: 1000px; height: 600px"></div>
    <h2>About Your Site</h2>
    <form action="sitedetailstest.jsp" method="POST" onsubmit="getWKT()">
        <table>
            <tr>
            <td align="right">Site Name:</td>
            <td align="left"><input type="text"
                name="siteName" length="30"/></td>
            </tr>
        </table>
     What type of development are you proposing?<br>
    <input type="radio" name="siteType" value="Wind">Wind Energy<br>
    <input type="radio" name="siteType" value="Solar">Solar PV<br>
    <input type="radio" name="siteType" value="Biomass">Biomass Burning<br>
    <input type="radio" name="siteType" value="Ground Source Heat">Ground Source Heat Pumps<br>
    
    <input type="hidden" id="polyInput" name ="sitePoly"> </input>
    
    <p><input type="submit" value="Assess My Site!!"></p>
       
    </form>
    
    </body>
    
    </html>