As follows:
request.jsp
Copy Code code as follows:
<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 "
pageencoding= "Iso-8859-1"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<script language= "JavaScript" ><!--
function Getxmlhttpobject () {
var xmlHttp = null;
try{
XmlHttp = new XMLHttpRequest ();
}catch (e) {
try{
XmlHttp = new ActiveXObject ("Msxml2.xmlhttp");
}catch (e) {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function ShowMsg (str) {
XmlHttp = Getxmlhttpobject ();
if (xmlHttp = = null) {
Alert ("You browser don ' t support the Ajax");
Return
}
var url = "Response.jsp";
url = url + "? q=" + str;
url = url + "&sid =" + Math.random ();
Xmlhttp.onreadystatechange = statechanged;
Xmlhttp.open ("Get", url, True);
Xmlhttp.send (NULL);
}
function statechanged ()
{
if (xmlhttp.readystate==4)
{
document.getElementById ("City"). Value = Xmlhttp.responsetext;
}
}
--></script>
<body>
<form name= "Form1" action= "" method= "POST" >
<label >city code:</label>
<input type= "text" name= "code" onblur = "showmsg (this.value)"/>
<br></br>
<label>city name:</label>
<input type= "text" name= "City" id= "City" ></input>
</form>
</body>
response.jsp
Copy Code code as follows:
<%@ page language= "java" contenttype= "Text/plain; Charset=utf-8 "
pageencoding= "UTF-8"%>
<%@ page import= "com.lwf.eus.util.*,java.util.*,com.lwf.eus.entity.*,com.lwf.eus.bean.*"%>
<%
String code = request.getparameter ("q");
SYSTEM.OUT.PRINTLN (code);
if (Code.equals ("140"))
Out.print ("Shanghai");
else if (code.equals ("150"))
Out.print ("Beijing");
else if (code.equals ("160"))
Out.print ("Tianjin");
Else
Out.print ("Unknown Place");
%>
Note here that because the returned results are displayed in the text box, there are no