ajax应用实现续

上一篇讲的是在web前端中在jsp嵌入使用ajax技术实现局部刷新的目的,运用的是windows一个公共xmlhttprequest对象,这一篇讲的是如何实现服务器端servlet
首先,servlet-mapping中可以看到servlet类名应该是GetBusinformation

<servlet-mapping>
    <servlet-name>GetBusinformation</servlet-name>
    <url-pattern>/GetBusinformation</url-pattern>
  </servlet-mapping>

所以创建了一个GetBusinformation类:
public class GetBusinformation extends HttpServlet
在这个类中接收”POST”或”GET”响应:

protected  void doGet(HttpServletRequest request,HttpServletResponse response)
   throws servletexception,IOException{
    processRequest(request,response,"GET");
   //String x=request.getParameter("name1");
   }
   protected  void doPost(HttpServletRequest request,"POST");
   //String b=request.getParameter(name)
   }

响应请求创建XML文件并发送给客户端

protected void processRequest(HttpServletRequest request,HttpServletResponse response,String method)throws servletexception,IOException{
    response.setContentType("text/xml");
    String c=request.getParameter("timeStamp");
    getData();
    String xml;
    xml=createXML();
    response.setContentType("text/xml");
    PrintWriter out=response.getWriter();
    out.write(xml);
    out.close();
   }

至于xml文件时如何创建的可以参考以下:

public String createXML(){
        String xmlFile="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        xmlFile=xmlFile+" <AllBusInfo>\n";
        xmlFile=xmlFile+" <Locates>\n";
        for(int i=0;i<busNumber;i++){
             xmlFile=xmlFile+" <Location>\n";
             xmlFile=xmlFile+" <gpsinfoID>"+locate[i].getGpsinfoID()+"</gpsinfoID>\n";
             xmlFile=xmlFile+" <busID>"+locate[i].getBusID()+"</busID>\n";
             String timeValue=locate[i].getHour()+":"+locate[i].getMinute()+":"+locate[i].getSecond();
             xmlFile=xmlFile+" <time>"+timeValue+"</time>\n";

             xmlFile=xmlFile+" <status>"+locate[i].getStatus()+"</status>\n";


             xmlFile=xmlFile+" <latitude>"+locate[i].getLatitude()+"</latitude>\n";


             xmlFile=xmlFile+" <latitude_sphere>"+locate[i].getLatitude_sphere()+"</latitude_sphere>\n";

             xmlFile=xmlFile+" <longtitude>"+locate[i].getLongtitude()+"</longtitude>\n";

             xmlFile=xmlFile+" <longtitude_sphere>"+locate[i].getLongtitude_sphere()+"</longtitude_sphere>\n";
             xmlFile=xmlFile+" <speed>"+locate[i].getSpeed()+"</speed>\n";
             xmlFile=xmlFile+" <direction>"+locate[i].getDirection()+"</direction>\n";
             String dateValue=locate[i].getYear()+"-"+locate[i].getMonth()+"-"+locate[i].getDay();
             xmlFile=xmlFile+" <date>"+dateValue+"</date>\n";
             xmlFile=xmlFile+" </Location>\n";
        }
        xmlFile=xmlFile+" </Locates>\n";
        xmlFile=xmlFile+" </AllBusInfo>\n";
        return xmlFile;
    }

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...