代码是照书上打得,零基础学JavaWeb开发, 环境是MyEclipse+tomcat6.0,jdk是1.7的,firefox和ie都能从服务器获取页面,但是按了按钮,或者这里的在文本框里输入什么的都没有反应,tomcat的log也没有任何反应,求指教~!~Suggest.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
    + request.getServerName() + ":" + request.getServerPort()
    + path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>My JSP 'Suggest.jsp' starting page</title>
<script language="javascript">

     function createXMLHttpRequest(){
     if(window.XMLHttpRequest){
     XMLHttpReq = new XMLHttpRequest();
     }else{
     if(window.ActiveXObject){
     try{
     XMLHttpReq = new ActiveXObject("Msxm12.XMLHTTP");
     }catch(e){
     try{
     XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
     }catch(e){}
     }
     }
     }
     }
    
     function handleResponse(){
     if(XMLHttpReq.readyState==4){
     if(XMLHttpReq.status ==200){
     var out = "";
     var res = XMLHttpReq.responseXML;
     var items = res.getElementsByTagName("item");
     for(var i=0;i<items.length;i++){
     addRow(items(i).firstChild.nodeValue);
     }
     setDivStyle();
     }
     }
     }
    
     function clearTable(){
     var content = document.getElementById("content");
     while(chontent.childNodes.length>0){
     content.removeChild(content.childNodes[0]);
     }
     }
    
     function addRow(item){
     var content = document.getElementById("content");
     var row = document.createElement("tr");
     var cell = document.createElement("td");
     cell.onmouseover = function(){this.style.background = "blue"};
     cell.onmouseout = function(){this.style.background = "#f5f5f1"};
     cell.onclick = function(){
     document.getElementById("key").value = this.innerHtml;
     document.getElementById("suggest").style.visibility = "hidden"};
     row.appendChile(cell);
     content.appendChild(row);
     }
    
     function sendRequest(url){
     createXMLHttpRequest();
     XMLHttpReq.open("GET",url,true);
     XMLHttpReq.inreadystatechange = handleResponse;
     XMLHttpReq.send(null);
     }
    
     function suggest(){
     var key = document.getElementById("key").value;
     sendRequest("Suggest?key="+key);
     }
    
     function setDivStyle(){
     var suggest = document.getElementById("suggest");
     suggest.style.border = "black 1px solid";
     suggest.style.left = 62;
     suggest.style.top = 50;
     suggest.style.width = 150;
     suggest.style.backgroundColor = "#f5f5f1";
     document.getElementById("suggest").style.visibility = "visible";
     }
    
    </script>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<font size="1"> 输入提示实例<br> 请输入 : <input type="text"
id="key" name="key" onkeyup="suggest()" />
<div id="suggest" style="position: absolute">
<table>
<tbody id="content"></tbody>
</table>
</div> </font>
</body>
</html>Servlet  Suggest.java
package servlets;import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Suggest extends HttpServlet {    private ArrayList lib = new ArrayList();    @Override
    public void init() throws ServletException {
super.init();
lib.add("a");
lib.add("able");
lib.add("access");
lib.add("advance");
    }    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
response.setContentType("text/xml; charest=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
String output = "";
String key = request.getParameter("key");
ArrayList matchList = getMatchString(key); if (!matchList.isEmpty()) {
    output += "<response>";
    for (int i = 0; i < matchList.size(); i++) {
String match = matchList.get(i).toString();
output += "<item>" + match + "</item>";
    }
    output += "</response>";
}
out.println(output);
out.close();
    }    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
doPost(request, response);
    }    public ArrayList getMatchString(String key) {
ArrayList result = new ArrayList();
if (!lib.isEmpty()) {
    for (int i = 0; i < lib.size(); i++) {
String str = lib.get(i).toString();
if (str.startsWith(key)) {
    result.add(str);
}
    }
    // return result;
}
return result;
    }}web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
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_2_5.xsd">

<servlet>
<servlet-name>Suggest</servlet-name>
<servlet-class>servlets.Suggest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Suggest</servlet-name>
<url-pattern>/Suggest</url-pattern>
</servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    This is my JSP page. <br>
        <a href="Suggest.jsp">link</a>
  </body>
</html>

解决方案 »

  1.   

    我研究了一下楼主的代码,第一个问题:
    ArrayList matchList = getMatchString(key);
    这行代码,尽管key的值已经获取到了,但是通过以上代码仍然无法把key的值放入matchList中。因此在if (!matchList.isEmpty())的判断中就跳出循环了。
    我简单了做了下修改:
    ArrayList matchList = new ArrayList();
    matchList.add(key);
    这样matchList能获取到页面key的值。然后outprint的值为:
    <response><item>(key的值)</item></response>
    但是out.println(output);后在页面上仍然没有显示。不知楼主预期的效果是什么样的?
      

  2.   

    这个代码其实是做一个输入提示和自动完成的。就好像百度,google的搜索框的效果。现在仅仅预设了几个参数放进了ArrayList lib; 中。  这个应该不是代码问题,我怀疑是什么配置问题, 因为之前写了个Ajax异步输入验证的也没有反应。   我怀疑是web。xml没配好,但经验不足,不知道缺了什么?感谢你的回答~
      

  3.   

    我只看了你ajax那块的疑点代码,你是不是要定义一个var XMLHttpReq这个对象呢?我也是菜鸟,后面太长就没看
      

  4.   

    那段 XMLHttpReq 在 function createXMLHttpRequest()这个函数里就定了,根据书上看的结果,感觉,在js里定在函数里的变量可以当全局变量来用,我猜全局可能是指当前页面吧
      

  5.   

    js那块我也不太熟,但是我感觉你说的不对,createXMLHttpRequest()只是实例化的你的变量,你先定义一个var XMLHttpReq全局变量试试看,然后下面我记得是onreadystatechange,你写的是in,
      

  6.   

    嗯 ,的确是onreadystatechange,还有,我在js最开始定义了个 var XMLHttpReq=null;当全局变量,还是不行,我觉得,如果是调用函数错误的话应该tomcat会报错。但是tomcat没反应,(但我已经把项目上载到tomcat了,不然看不到页面),还是很怪。我还是直接用struts吧,底层的实现交给框架能有反应。。额
      

  7.   

    javascript  函数没有执行!
      

  8.   

    确实有执行,已经 alert过了