ajax应用实现(续)

前端之家收集整理的这篇文章主要介绍了ajax应用实现(续)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

@H_502_4@<servlet-mapping> <servlet-name>GetBusInformation</servlet-name> <url-pattern>/GetBusInformation</url-pattern> </servlet-mapping>

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

@H_502_4@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文件并发送给客户端

@H_502_4@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文件时如何创建的可以参考以下:

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

猜你在找的Ajax相关文章