ajax应用实现(续)

1 篇文章 0 订阅

上一篇讲的是在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,HttpServletResponse response)
   throws ServletException,IOException{
    processRequest(request, response, "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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值