HTML的<head>标签中的JS,
例子:实现异步
<!--
<script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
var center = new GLatLng(39.9493, 116.3975);
map.setCenter(center, 13);
var marker = new GMarker(center, {draggable: true});
GEvent.addListener(marker, "dragstart", function() {
map.closeInfoWindow();
});
GEvent.addListener(marker, "dragend", function() {
// alert(marker.getLatLng().lat());
// alert(marker.getLatLng().lng());
marker.openInfoWindowHtml("弹起来了...");
document.getElementById('lat').value= marker.getLatLng().lat();
document.getElementById('lng').value= marker.getLatLng().lng();
});
map.addOverlay(marker);
}
}
</script>
<script type="text/javascript">
var Try = {
these: function() {
var returnValue;
for (var i = 0, length = arguments.length; i < length; i++) {
var lambda = arguments[i];
try {
returnValue = lambda();
break;
} catch (e) { }
}
return returnValue;
}
};
var Ajax = {
/**
* Creates a new XMLHttpRequest object.
* @alias Ajax.getTransport
* @return {XMLHttpRequest} Returns a new XMLHttpRequest object.
*/
getTransport: function() {
return Try.these(
function() {return new XMLHttpRequest()},
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')}
) || false;
},
activeRequestCount: 0
};
function testSaverMarker(){
var lat = document.getElementById("lat").value;
var lng = document.getElementById("lng").value;
var request = Ajax.getTransport();
request.open("post","/mapserver/savemarker?lat="+lat+"&lng="+lng,true);
request.onreadystatechange = function(){
if(request.readyState == 4){//约定只要有了返回结果,无论何时都可以alert
alert(request.responseText);
var obj = eval('('+request.responseText + ')');
alert(obj.success);
alert(obj.lat);
alert(obj.lng);
document.getElementById('result').innerHTML=request.responseText;
}
}
request.send(null);//我真的要发送到后台了
// document.getElementById('result').innerHTML=request.responseText;
return false;
}
</script>
-->
HTML的<body>标签中的代码
<!--
<body οnlοad="initialize()" οnunlοad="GUnload()" >
<div id="map_canvas" style="width: 500px; height: 300px"></div>
lat: <input type="text" name="lat" id="lat" /> <br/><br/>
lng: <input type="text" name="lng" id="lng" /><br/><br/>
<input name="submit" type="submit" value="OK" οnclick="testSaverMarker()";/>
<div id="result"></div>
</body>
-->
servlet中post方法中的代码回写,其中还用到json格式:
<!--
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("now you are in post..");
String ip=request.getRemoteAddr();
System.out.println("Now the ip is "+ip);
String llq=request.getHeader("user-agent");
System.out.println("Now the user-agent is "+llq);
String thisURI=request.getRequestURI();
System.out.println("Now the URI is "+thisURI);
if(thisURI.equals("/mapserver/savemarker")){
String lat = request.getParameter("lat");
String lng = request.getParameter("lng");
System.out.println("lat="+lat);
System.out.println("lng="+lng);
response.setCharacterEncoding("utf-8");
//response.getWriter().write("保存成功"+lat+":"+lng);
String jsonstr = "{\"success\":true,\"lat\":"+lat+",\"lng\":"+lng+"}";
Map map = new HashMap();
map.put("success", true);
map.put("lat", lat);
map.put("lng", lng);
String jsonText = JSONValue.toJSONString(map);
response.getWriter().write(jsonText);
}
-->