加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax应用实现(续)

发布时间:2020-12-16 02:01:20 所属栏目:百科 来源:网络整理
导读:上一篇讲的是在web前端中在jsp嵌入使用ajax技术实现局部刷新的目的,运用的是windows一个公共xmlhttprequest对象,这一篇讲的是如何实现服务器端servlet 首先,servlet-mapping中可以看到servlet类名应该是GetBusInformation servlet-mapping servlet-name G

上一篇讲的是在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;
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读