微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
    }

原文地址:https://www.jb51.cc/ajax/163446.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐