ajax小练习,帮别人修改的一个小aj…

前端之家收集整理的这篇文章主要介绍了ajax小练习,帮别人修改的一个小aj…前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
ajax 小例子做个记号

页面1:
ajax.html

<!DOCTYPEhtml>
<html>
<head>
 <title>AJAX.html</title>
</head>
<script type="text/javascript">

function getResult(){
  var url ="ajaxServlet?action=send&time="+(newDate()).getTime();
  if (window.XMLHttpRequest){
   req = newXMLHttpRequest();
  }else if (window.ActiveXObject)
  {
   req = newActiveXObject("Microsoft.XMLHTTP");
  }
  if(req) 
  {
   req.open("GET",url,true);
   req.onreadystatechange =complete;
   req.send(null);
  }
}

function complete(){
  if (req.readyState == 4)
  {
   if (req.status == 200){
    var type =req.responseXML.getElementsByTagName_r("type_name");
    var str=newArray();
    for(vari=0;i<type.length;i++){
    str[i]=type[i].firstChild.data;
    alert(str[i]);
     document.getElementByIdx_x("ajaxTest").innerHTML=str[i]+"<BR>";
    }
   }
  }
}
</script>
<body >

  <table width="80%" border="0"cellspacing="0" cellpadding="0">
  <tr>
   <td id="td">
    ddddd
   </td>
   <td>
   <input type="button" value="ajaxTest"onclick="getResult()">
   </td>
   <td>
         测试结果:
      <labelid="ajaxTest"></label>
   </td>
  </tr>
  </table>
</body>
</html>


页面2:
web.xml 配置servlet 跳转
 <?xmlversion="1.0" encoding="UTF-8"?>
<web-app version="3.0"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <display-name></display-name>
 
  <servlet>
   <description>This is the descriptionof my J2EEcomponent</description>
   <display-name>This is the displayname of my J2EEcomponent</display-name>
   <servlet-name>ajax</servlet-name>
   <servlet-class>ajaxServlet</servlet-class>
  </servlet>

 <servlet-mapping>
   <servlet-name>ajax</servlet-name>
   <url-pattern>/ajaxServlet</url-pattern>
 </servlet-mapping>
 
 <welcome-file-list>
   <welcome-file>AJAX.html</welcome-file>
 </welcome-file-list>
</web-app>


页面3:
后台对应的servlet 程序:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ajaxServlet extends HttpServlet {

   
    privatestatic final long serialVersionUID = 1L;
   
   
    privatestatic final String CONTENT_TYPE = "text/xml;charset=gb2312";

   
    publicajaxServlet() {
      super();
    }

   
    public voiddestroy() {
      super.destroy(); // Just puts "destroy" string in log
       // Put yourcode here
    }

   
    public voiddoGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
    {
      doPost(request,response);
    }


    public voiddoPost(HttpServletRequest request,HttpServletResponseresponse)
          throwsServletException,IOException {
 
        response.setContentType("text/xml;charset=gb2312");
        PrintWriter out = response.getWriter();
        String action =request.getParameter("action");
        System.out.println("后台执行ajax"+action);
        if (("send").equals(action))
        {
          
         StringBuffer sb = newStringBuffer("");
         sb.append("AA");
         sb.append("BB");
         sb.append("CC");
         sb.append("DD");
         sb.append("");
        out.write(sb.toString());
         out.close();
        }
    }

   
    public voidinit() throws ServletException {
       // Put yourcode here
    }

}
原文链接:https://www.f2er.com/ajax/165742.html

猜你在找的Ajax相关文章