手工写ajax
其实ajax就是js+xmlHttpRequest实现的。<script type="text/javascript" charset="GBK">
<!---->
?var xmlHttp=false;
?<!---->
?if(window.ActiveXObject){
?try{
?<!---->
?xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
?}catch(e){
<!---->
?xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
?}
?
?}
<!---->
?if(window.XMLHttpRequest && typeof(xmlHttp)=='underfined'){
?
?try{
<!---->
?xmlHttp=new XMLHttpRequest();
?}catch(e){
?
?alert("当前浏览器版本太低!!!");
?}
?}
?
?function getResponse(){
<!---->
?var url="Hall.do?username="+document.getElementById("username").value;
<!---->
?xmlHttp.onreadystatechange=updatepage;
<!---->
?xmlHttp.open("post",url,true);
<!---->
?xmlHttp.send(null);
?
?}
?
<!---->
?function updatepage(){
?<!---->
?if(xmlHttp.readyState==4&&xmlHttp.Status==200){
?<!---->
?var result=xmlHttp.responseText;
?alert(result);
?}
?
?}
?
???
??
?? </script>
在后台servlet中用
System.out.println("请求成功!!!");
//用Request的方法获得前台传过来的值
??System.out.println(req.getParameter("username").toString());?
//用response的方法输出返回的值
??PrintWriter writer=resp.getWriter();
??writer.write("hallo world");
??writer.flush();
??writer.close();
?
?
?
页:
[1]