如何将表单值从javascript传递到jsp页面
这是我的示例 html 页面,其中包含内联 javascript,它计算地理编码并尝试返回纬度和经度。但我的问题是如何将所有具有从 showlocation 函数返回的纬度和经度的表单值提交到 Submitform.jsp 页面并显示纬度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>
提交表单.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"));
%>
This is my sample html page which contains inline javascript which calculates the geocode and tries to return the latitude and longitude .But my question is how to submit all the form values with latitude and longitude returned from showlocation function to submitform.jsp page and display latitude value in jsp page .
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"));
%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是直接将其附加在 url 中,例如
myJsp.jsp?lat=value&lon=value
GET
,另一种方法是保留隐藏参数并在其中设置 lat 、 lon并
POST
它。one way directly append it in url like
myJsp.jsp?lat=value&lon=value
GET
,another way is to keep a hidden param and set lat , lon in it and
POST
it.