RSS
热门关键字:  java  Ajax  JSP  JSF  Struts
当前位置 : 首页>Ajax>列表

基于REST的Web服务及基于Ajax的客户端

来源: 作者: 时间:2007-10-10 点击:

  map.put("00346",emp1);
  map.put("00347",emp2);
  map.put("00348",emp3);
 }

 /* (non-Java-doc)
 * @see javax.servlet.http.HttpServlet#doGet
 (HttpServletRequest arg0, HttpServletResponse arg1)
 */
 protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
 throws ServletException, IOException {
  // TODO Auto-generated method stub
  arg1.setContentType("text/xml");
  PrintWriter out=arg1.getWriter();
  System.out.println(map);
  if(arg0.getPathInfo()!= null){
   String EmpId=arg0.getPathInfo().substring(1,arg0.getPathInfo().length());
   System.out.println(EmpId);

   out.write("<?xml version='1.0' encoding='UTF-8'?>"+"\n");
   out.write("<p:EmpDetail xmlns:p='http://www.employee-details.com'>"+"\n");
   out.write("<Emp-ID>"+EmpId+" </Emp-ID>"+"\n");
   out.write("<Name>"+((Employee)map.get(EmpId)).name+" </Name>"+"\n");
   out.write("<Department>"+((Employee)map.get(EmpId)).dept+" </Department>"+"\n");
   out.write("</p:EmpDetail>"+"\n");
   out.flush();
  }else{
   out.write("<?xml version='1.0' encoding='UTF-8'?>"+"\n");
   out.write("<p:Employees xmlns:p='http://www.employee-details.com'>"+"\n");
   out.write("<Employee id='00345' href='http://localhost:9080/
AJAX_REST_Demo/RESTDemoServlet/employees/00345'/>"+"\n");
   out.write("<Employee id='00346' href='http://localhost:9080/
AJAX_REST_Demo/RESTDemoServlet/employees/00346'/>"+"\n");
   out.write("<Employee id='00347' href='http://localhost:9080/
AJAX_REST_Demo/RESTDemoServlet/employees/00347'/>"+"\n");
   out.write("<Employee id='00348' href='http://localhost:9080/
AJAX_REST_Demo/RESTDemoServlet/employees/00348'/>"+"\n");
   out.write("</p:Employees>");
   out.flush();
  }
 }

 /* (non-Java-doc)
 * @see javax.servlet.http.HttpServlet#doPost
 (HttpServletRequest arg0, HttpServletResponse arg1)
 */
 protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
  // TODO Auto-generated method stub
 }

}

  在下一个部分中,了解如何为这个基于 REST 的 Web 服务编写 Ajax 客户端。

  为基于 REST 的 Web 服务编写 Ajax 客户端

  如前所述,Ajax 表示 Asynchronous JavaScript + XML。它有时也称为 XML HTTP 技术。在 Ajax 中,核心的技术主要是围绕实现与服务器的异步通信,而无需页面刷新。XMLHTTPRequest 对象支持对服务器进行异步 GET、POST、PUT 和 DELETE。这并不向用户显示任何内容,换句话说,不会显示状态消息。您可以为状态更改指定一个处理程序方法,并且当发生如下请求时将通知这个处理程序:

   初始化
   启动
   在返回的过程中
   完全完成

  清单 4 显示了一个基于 Ajax 的 HTML 页面的代码,它可以用作上述基于 REST 的 Web 服务的客户端:

  清单 4. 基于 Ajax 的 HTML 页面的代码

<SCRIPT language="javascript" type="text/javascript">
var req=null;
//This function initializes XHR
function initXHR() {
 if (navigator.appName.indexOf("Microsoft")> -1 ) {
  try{
   req=new ActiveXObject("Microsoft.XMLHTTP");
  }catch(e1){
   alert("failed to create XHR in IE");
  }
 }else{
  try{
   req=new XMLHttpRequest();
  }catch(error){
   alert("failed to create XHR in FireFox");
  }
 }
}
//get an employee detail
function getEmpDetails(Empurl){

共3页: 上一页 [1] 2 [3] 下一页
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
Google Adsense
相关文章