首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

一个容易的Servlet+jsp+tomcat代码,无法与服务器交互,求大侠

2012-10-11 
一个简单的Servlet+jsp+tomcat代码,无法与服务器交互,求大侠~代码是照书上打得,零基础学JavaWeb开发, 环境

一个简单的Servlet+jsp+tomcat代码,无法与服务器交互,求大侠~
代码是照书上打得,零基础学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;
  }

}

[color=#FF0000][/color]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[color=#FF0000][/color]
<%@ 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>



[解决办法]
我研究了一下楼主的代码,第一个问题:
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);后在页面上仍然没有显示。不知楼主预期的效果是什么样的?
[解决办法]
我只看了你ajax那块的疑点代码,你是不是要定义一个var XMLHttpReq这个对象呢?我也是菜鸟,后面太长就没看
[解决办法]
js那块我也不太熟,但是我感觉你说的不对,createXMLHttpRequest()只是实例化的你的变量,你先定义一个var XMLHttpReq全局变量试试看,然后下面我记得是onreadystatechange,你写的是in,

热点排行