代码是照书上打得,零基础学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>
<%@ 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>
解决方案 »
- 用户长时间无操作后,如何保存当前状态并要求重新输入密码
- JCreator开发web程序编译时提示找不到符号
- MSSQL2K+Struts如何配置数据源?
- 请JSP高手门简单介绍一下自己学习jsp的经过吧!希望对我们菜鸟有所帮助!顶者有分!谢谢~!!
- 我的tomcat启动后为什么访问localhost不行,而127.0.0.1就没有问题?
- 请问一下啊,后台管理里面添加文章的格式
- javascript如何控制输入字数
- html提交到selvlet文件显示中文问题?
- 从页面上传的图片怎么保存在工程目录下呢??
- 懂PAYPAL支付的进来下
- java 关闭进程 求思路
- TOMCAT 5.5 浏览器输入http://localhost:8080/工程名/页面名 报错404
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);后在页面上仍然没有显示。不知楼主预期的效果是什么样的?