前台页面通过JS调用Servlet访问到后台类

1.在页面中导入必要的JS

 
 

 

2. buffalo.js 可以上网下载

    xmlcallback.js如下:

    function getDomDocumentPrefix() { if (getDomDocumentPrefix.prefix) return getDomDocumentPrefix.prefix; var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"]; var o; for (var i = 0; i < prefixes.length; i++) { try { // try to create the objects o = new ActiveXObject(prefixes[i] + ".DomDocument"); return getDomDocumentPrefix.prefix = prefixes[i]; } catch (ex) {}; } throw new Error("Could not find an installed XML parser"); } function getXmlHttpPrefix() { if (getXmlHttpPrefix.prefix) return getXmlHttpPrefix.prefix; var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"]; var o; for (var i = 0; i < prefixes.length; i++) { try { // try to create the objects o = new ActiveXObject(prefixes[i] + ".XmlHttp"); return getXmlHttpPrefix.prefix = prefixes[i]; } catch (ex) {}; } throw new Error("Could not find an installed XMLHttp object"); } function XmlHttp() {} XmlHttp.httpPool = []; XmlHttp.httpMaxSize = 3; XmlHttp.poolGetHttp = function() { if (XmlHttp.httpPool.length > 0) { return XmlHttp.httpPool.pop(); } return XmlHttp.create(); } XmlHttp.poolReturnHttp = function(http) { if (XmlHttp.httpPool.size > XmlHttp.httpMaxSize) { delete http; } XmlHttp.httpPool.push(http); } XmlHttp.create = function () { try { // NS & MOZ if (window.XMLHttpRequest) { var req = new XMLHttpRequest(); // some versions of Moz do not support the readyState property // and the onreadystate event so we patch it! if (req.readyState == null) { req.readyState = 1; req.addEventListener("load", function () { req.readyState = 4; if (typeof req.onreadystatechange == "function") req.onreadystatechange(); }, false); } return req; } // IE if (window.ActiveXObject) { return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp"); } } catch (ex) {} // Fail throw new Error("Your browser does not support XmlHttp objects"); }; function XmlDocument() {} XmlDocument.create = function () { try { if (document.implementation && document.implementation.createDocument) { var doc = document.implementation.createDocument("", "", null); if (doc.readyState == null) { doc.readyState = 1; doc.addEventListener("load", function () { doc.readyState = 4; if (typeof doc.onreadystatechange == "function") doc.onreadystatechange(); }, false); } return doc; } if (window.ActiveXObject) return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument"); } catch (ex) {} throw new Error("Your browser does not support XmlDocument objects"); }; if (window.DOMParser && window.XMLSerializer && window.Node && Node.prototype && Node.prototype.__defineGetter__) { Document.prototype.loadXML = function (s) { var doc2 = (new DOMParser()).parseFromString(s, "text/xml"); while (this.hasChildNodes()) this.removeChild(this.lastChild); for (var i = 0; i < doc2.childNodes.length; i++) { this.appendChild(this.importNode(doc2.childNodes[i], true)); } }; Document.prototype.__defineGetter__("xml", function () { return (new XMLSerializer()).serializeToString(this); }); } /* * xmlHttp Pool * * userage: var xmlhttpObj = XmlHttpPool.pick() */ var XmlHttpPoolArr = new Array(); var XmlHttpPoolSize = 100; var XHPCurrentAvailableID = 0; function XmlHttpPool() {} XmlHttpPool.pick = function() { var pos = XHPCurrentAvailableID; XmlHttpPoolArr[pos] = XmlHttp.create(); XHPCurrentAvailableID >= (XmlHttpPoolSize-1) ? 0 : XHPCurrentAvailableID++ return XmlHttpPoolArr[pos]; } function rpcCall(url, method, data, callback, asyn) { var xmlhttp = XmlHttp.create(); xmlhttp.open(method, url, asyn); xmlhttp.send(data); if (!asyn) { // if not asyn callback(xmlhttp.responseText); } else { xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { callback(xmlhttp.responseText); } } } }

 

 

3. 调用的Servlet如下,同时,需要在web.xml中配置对应的参数

       
   
    ajax
    com.longtop.common.BurlapServlet
   

   
    
    ajax
    /ajax
   
   

 

    package com.longtop.common; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import com.caucho.burlap.io.BurlapInput; import com.caucho.burlap.io.BurlapOutput; import com.caucho.burlap.server.BurlapSkeleton; /** * 通过 ajax,在页面回显数据 */ public class BurlapServlet extends HttpServlet{ private static final long serialVersionUID = -5162149775110419399L; public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { //防止 websphere 5.1 下乱码,对jboss没影响 response.setContentType("text/xml; charset=UTF-8"); String serviceBeanId = "sid"; String serviceName = request.getParameter(serviceBeanId); Object serviceInstance = ContextUtil.getBean(serviceName); BurlapSkeleton skeleton = new BurlapSkeleton(serviceInstance); InputStream is = request.getInputStream(); OutputStream os = response.getOutputStream(); BurlapInput in = new BurlapInput(is); BurlapOutput out = new BurlapOutput(os) { public void startReply() throws IOException { print(""); } }; try { skeleton.invoke(in, out); } catch (Throwable e) { // ProLogService.error(this.getClass(), e.getMessage(), e); } } }

 

 

4.  最后在页面中按照如下代码直接调用就好了!(注:comService必须是在Spring中注册好的!)

    //调用spring中的实例来操作 var buffalo = new Buffalo('', true); buffalo.remoteCall("comService.getTestStr", ["linwei"], function(reply){ var result = reply.getResult(); alert("result is " + result); });

 

 

 

 

 

 

你可能感兴趣的:(AJAX知识)