As follows:
Request. jsp
Copy codeThe Code is 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">
<Html>
<Head>
<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>
</Head>
<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>
</Html>
Response. jsp
Copy codeThe Code is 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 ");
%>
Note that the returned results must be displayed in the text box. Therefore. jsp does not contain tags such as