| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2824 人关注过本帖
标题:AJAX和Servlet连不上
只看楼主 加入收藏
fujinliang
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-3-30
结帖率:0
收藏
 问题点数:0 回复次数:1 
AJAX和Servlet连不上
sendRequest.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>send.html</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
<script type="text/javascript">
var xmlhttp;
  function createXMLHttpRequest()
  {
     if(window.ActiveXObject)
     {
      if(navigator.userAgent.toLowerCase().indexOf('msie 5')!=-1)
       {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
       else
       {
        xmlhttp=new ActiveXObject("Msxml2.XMLHttp");
        }
      }
      else if(window.XMLHttpRequest)
      {
       xmlhttp=new XMLHttpRequest();
       }
      else
      {
      alert("创建XMLHTTPRequest失败");
       }
  }
 function createQueryString(){
 var text1=document.getElementById("text1").value;
 var text2=document.getElementById("text2").value;
 var text3=document.getElementById("text3").value;
 var queryString="text1=" +text1+"&text2="+text2+"&text3="+text3;
 return queryString;
 }
 function sendRequest()
 {
   createXMLHttpRequest();
   var queryString="send?"+createQueryString();
   xmlhttp.onreadystatechange=process;
   xmlhttp.open("GET",queryString,true);
   xmlhttp.send(null);
   
   }
 function process()
 {
 if(xmlhttp.readyState == 4){
 if(xmlhttp.status == 200){

   var responseDiv=document.getElementById("serverResponse");
   if(responseDiv.hasChildNodes()){
   responseDiv.removeChild(responseDiv.childNodes[0]);
   }
   responseDiv.innerHTML=xmlhttp.responseText;
   }
   }
   }
   function init()
   {
   document.getElementById("text1").value="";
   document.getElementById("text2").value="";
   document.getElementById("text3").value="";
   
   }
  </script>
  <body onload="init();">
  <strong>请在下面的文本框中输入您的兴趣爱好:</strong><br><strong></strong><strong><br></strong><form  action="#" name="form1"><p>&nbsp;爱好1:
                <input type="text"  size="20" width="20" id="text1"/>
            </p><p>&nbsp;爱好2: <input width="20" type="text"  size="20" id="text2"/></p>
            <p>&nbsp;爱好3: <input width="20" type="text"  size="20" id="text3"></p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="button" value="发送" name="button1" onclick="sendRequest();">
            &nbsp;&nbsp;</form><br>
    <div id="serverResponse"></div>
    </body>
</html>
send.java:
package test;

import
import
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class send extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public send() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/xml;charset=gb2312");
        String text1=new String(request.getParameter("text1").getBytes("8859_1"));
        String text2=new String(request.getParameter("text2").getBytes("8859_1"));
        String text3=new String(request.getParameter("text3").getBytes("8859_1"));
        String row1="";
        String row2="";
        String row3="";
        if(!text1.equals(""))
        {
            row1="<tr><td>1</td><td>"+text1+"</td></tr>";
        }
        if(!text2.equals(""))
        {
            row2="<tr><td>2</td><td>"+text2+"</td></tr>";
        }
        if(!text3.equals(""))
        {
            row1="<tr><td>3</td><td>"+text3+"</td></tr>";
        }
        String responseText="<table border=\"1"+"\">"+row1+row2+row3+"</table>";
        PrintWriter out = response.getWriter();
        out.println(responseText);
        out.flush();
        out.close();
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}


高手看哈怎么联不上啊,,,没反应???????
搜索更多相关主题的帖子: AJAX Servlet 
2008-10-17 00:04
恋轩念伊人
Rank: 5Rank: 5
来 自:湖南衡阳
等 级:贵宾
威 望:19
帖 子:643
专家分:0
注 册:2008-2-23
收藏
得分:0 
首先你的代码有很多问题,
第二你不能说连接不上就把问题推给我们,你的在什么地方出现什么错误写上来,或者慢慢调试看下

我不是一个随便的人,我随便起来不是人我哦。信不?
2008-10-20 20:04
快速回复:AJAX和Servlet连不上
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022825 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved