1
我有一個sellhouse.jsp,我試圖從用戶那裏獲取值,即名稱和地址,並將其插入到數據庫中。在將值插入數據庫之前,我試圖調用JavaScript來獲取用戶輸入地址的地理編碼。但是,在我提交表單之前,腳本會被執行。但我希望腳本執行一次,我點擊提交按鈕。除此之外,我希望將地理編碼傳遞給jsp元素。從JSP調用JavaScript
這裏是sellhouse.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="javax.swing.JOptionPane"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sell Page</title>
</head>
<body>
<h1>Hello World!</h1>
<form action="sellHouse.jsp" method="POST">
Name: <input type="text" name="Name" value="" placeholder="please Enter your Name" />
Address1: <input type="text" name="Address1" value="" placeholder="please Enter your Name" />
Address2: <input type="text" name="Address2" value="" placeholder="please Enter your Name" />
City: <input type="text" name="City" value="" placeholder="please Enter your Name" />
State: <input type="text" name="State" value="" placeholder="please Enter your Name" />
Country: <input type="text" name="Country" value="" placeholder="please Enter your Name" />
Zip: <input type="text" name="Zip" value="" placeholder="please Enter your Name" />
<input type="submit" value="submit" name="Submit" />
</form>
<%
try
{
String name = request.getParameter("Name");
String addr1 = request.getParameter("Address1");
String addr2 = request.getParameter("Address2");
String City = request.getParameter("City");
String State = request.getParameter("State");
String country = request.getParameter("Country");
String zip = request.getParameter("Zip");
String geocoderadd = name + addr1 + addr2 + City + State + country + zip;
%>
<script>
function initMap() {
//var map = new google.maps.Map(document.getElementById('map'), {
//zoom: 8,
//center: {lat: -34.397, lng: 150.644}
//});
var geocoder = new google.maps.Geocoder();
document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder);
});
}
function geocodeAddress(geocoder) {
//var address = document.getElementById('address').value;
//var address = request.getParameter("FirstName");
var address = <%=geocoderadd%>;
//alert(address);
geocoder.geocode({'address': address}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
alert("Done");
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
alert(results[0].geometry.location);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDubMBIwomXLLoL2ueDx1tb1ErpsuxnWrE&v=3&callback=initMap" >
</script>
<%
int latitude ;
int longitude ;
if(name!=null){
Class.forName("oracle.jdbc.OracleDriver");
out.println("fkjngskldfng");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "OHMS", "OHMS");
Statement st = con.createStatement();
st.executeUpdate("INSERT INTO SELLHOUSE VALUES('" + name + "','" + addr1 + "','" + addr2 + "','" + City + "','" + State + "','" + country + "','" + zip + "','" + latitude + "','" + longitude + "')");
if (st != null) {
JOptionPane.showMessageDialog(null, "Registered Successfully");
//session=request.getSession();
//session.setAttribute("userName", userName);
response.sendRedirect("index.html");
}
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage());
}
%>
</body>
</html>