package demo;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AjaxServlet extends HttpServlet {
private ServletConfig servletConfig = null;
public void destroy() {
servletConfig = null;
}
public ServletConfig getServletConfig() {
return (this.servletConfig);
}
public String getServletInfo() {
return (this.getClass().getName());
}
public void init(ServletConfig servletConfig)
throws ServletException {
this.servletConfig = servletConfig;
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws java.io.IOException,
ServletException {
String key = (String)request.getParameter("key");
//get CountryBean object from ServletContext
CountryBean countryBean = (CountryBean)
getServletContext().getAttribute("CountryBean");
//get list of countries
String[] countries = countryBean.getCountries();
//find matches
String matches = getMatches(countries, key);
//print it out to the client
response.setContentType("text/xml");
java.io.PrintWriter out=response.getWriter();
out.print(matches);
out.flush();
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws java.io.IOException,
ServletException {
doGet(request, response);
}
private String getMatches(
String[] countries, String key){
//generate xml response
String cList = "<?xml version=\"1.0\" ";
cList += "encoding=\"UTF-8\" ?>";
cList +="<COUNTRIES>";
int count = 0;
//from countries list find first 5 matches
for(int i=0;i<countries.length;i++){
if(countries[i].toUpperCase().
startsWith(key.toUpperCase())){
cList += "<COUNTRY name=\"" + countries[i] + "\" />";
count++;
if(count == 5) break;
}
}
cList += "<TOTALCOUNT count=\"" + count + "\" />";
cList += "</COUNTRIES>";
return cList;
}
}
CountryBean countryBean = (CountryBean)
getServletContext().getAttribute("CountryBean");
//get list of countries
String[] countries = countryBean.getCountries();
<servlet>
<servlet-name>AjaxServlet</servlet-name>
<servlet-class>demo.AjaxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AjaxServlet</servlet-name>
<url-pattern>/AjaxServlet</url-pattern>
</servlet-mapping>
var req;
//initialize the XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
//check if it is IE
var isIE = (window.navigator.appName.toLowerCase().indexOf("microsoft")>=0);
//implement loadXML method for Mozilla since it is not supported
if(!isIE){
Document.prototype.loadXML = function (s) {
// parse the string to a new doc
var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
// remove all initial children
while (this.hasChildNodes())
this.removeChild(this.lastChild);
// insert and import nodes
for (var i = 0; i < doc2.childNodes.length; i++) {
this.appendChild(this.importNode(doc2.childNodes[i], true));
}
};
}
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript" SRC="js/script.js"></SCRIPT>
<h:inputText id="autoText" onblur="hidePopup()" onfocus="getQuery(this.value)" onkeyup="getQuery(this.value)" value="#SelectedCountryRecord.selectedCountry}" />
<div align="left" class="box" id="autocomplete"
style="WIDTH:170px;visibility:hidden;
BACKGROUND-COLOR:#ccccff">
</div>
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
initPopup(‘_id0:autoText‘,‘a(chǎn)utocomplete‘);
</SCRIPT>
聯(lián)系客服