2010-11-11 158 views
1

這是我的示例html頁面,其中包含內嵌javascript,它計算地理編碼並嘗試返回經度和緯度。但我的問題是如何提交所有表單值與緯度和經度從showlocation函數返回到submitform.jsp頁面,並在jsp頁面顯示緯度值。如何將表單值從javascript傳遞給jsp頁面

的Index.html直接

<html> 
    <head> 
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA5yM-fP6TPUheIXxW3TxpVRRgJAR_YXLeqIBw9Qs7Dzlh-4wFexRUVFiN8RbSageQjhAggT3jom" 
    type="text/javascript"></script> 
<script type="text/javascript" 
    src="http://maps.google.com/maps/api/js?sensor=false"> 
</script> 

    <script type="text/javascript"> 
    geocoder = new GClientGeocoder(); 
    function showlocation() { 
      var clientaddress = document.getElementById('addstreetname').value; 
      geocoder.getLocations(clientaddress, function(response) { 
       if (!response || response.Status.code != 200) { 
        alert("Sorry, we were unable to geocode the first address"); 

       } 
       else { 
        var lat1 = response.Placemark[0].Point.coordinates[1]; 
        var lng1 = response.Placemark[0].Point.coordinates[0]; 

        return {latitude: lat1, longitude: lng1}; 
     } 
      }); 
    </script> 
    </head> 
    <body> 
    <form id="addresslistingform" action="submitform.jsp" onsubmit="returnshowlocation();"> 
       Company_Name:<br/> 
         <input size="30" type="text" id="businessname" name="businessname"/><br/> 
        Zipcode:<br/> 
         <input size="30" type="text" id="zipcode" name="zipcode1"/><br/> 
        Street No:<br/> 
         <input size="30" type="text" id="addstreetno" name="streetno"/><br/> 
        Street Name:<br/> 
         <input size="30" type="text" id="addstreetname" name="streetname"/><br/> 
        State:<br/>     
         <select name="select2" id="select2" class="required" name="state" > 
         <option value="">Select Your State</option> 
         <option value="1"> karnataka</option> 
         <option value="2"> Kerala</option> 
         <option value="3"> Andhra Pradesh</option> 
         </select> 
    </form> 
    </body> 
    </html> 

submitform.jsp

<%@ page language="java" import="java.sql.*" %> 

    <% 
      String driver = "com.mysql.jdbc.Driver"; 
       Class.forName(driver).newInstance(); 
      Connection con=null; 
      ResultSet rst=null; 
      Statement stmt=null; 

     try{ 
      String url = "jdbc:mysql://localhost:3306/"; 
       String dbName = "db"; 
       String driverName = "com.mysql.jdbc.Driver"; 
       String userName = ""; 
       String password = ""; 


      con = DriverManager.getConnection(url+dbName, userName, password); 
      stmt=con.createStatement(); 
     }catch(Exception e){ 
       System.out.println(e.getMessage()); 
       } 


    String Company_Name=request.getParameter("businessname"); 
    String Zipcode =request.getParameter("zipcode1"); 
    String Street_Number=request.getParameter("streetno"); 
    String Street_Name=request.getParameter("streetname"); 
    String State= request.getParameter("state");  
    String Latitude=request.getParameter("Latitude"); 
    out.println("latitude is :"+request.getParameter("Latitude")); 

      %> 

回答

1

一種方式追加它的URL像myJsp.jsp?lat=value&lon=valueGET

另一種方法是保持一個隱藏的PARAM和緯度設置,lon在它和POST它。

相關問題