注册 登录
编程论坛 jQuery论坛

AJAX和Servlet连不上

fujinliang 发布于 2008-10-17 00:04, 2824 次点击
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
    }

}


高手看哈怎么联不上啊,,,没反应???????
1 回复
#2
恋轩念伊人2008-10-20 20:04
首先你的代码有很多问题,
第二你不能说连接不上就把问题推给我们,你的在什么地方出现什么错误写上来,或者慢慢调试看下
1