Спасибо, решена одна проблема, это следующая.Как новичок в этом, я все еще получаю ошибку, и простое приложение не работает.Я получаю INVALID_STATE_ERR: DOM Exception 11 при выполнении req.open («Get», url, true);Команда в JavaScript находится в следующем файле index.html.Простое приложение не работает.Я получаю это в Chrome в режиме отладки, но приложение также не работает на IE8 или FF3.Есть идеи?
с использованием Eclipse для J2EE с Java6, Ajax, на WindowsVista.
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<script type="text/javascript">
var req;
function focusIn() {
//This is how you comment in javascript portion of code
//I will now demonstrate an alert function that calls a messagebox to the field, very useful for debugging
//this displays in yoru browser
alert('Hey dad this is an alert, this function was called by the onload message of the Body');
//There is even a cooler way, for instance say you wanted to display values
var two = 2;
var one = 1;
var result = two + one;
//Display your variable result
alert(result);
document.getElementById("key").focus();
}
function convertToDecimal(){
var key = document.getElementById("key");
var keypressed = document.getElementById("keypressed");
keypressed.value = key.value;
// onClick="alert('You clicked the button')"
var url = "/AjaxResponseServlet?key=" + escape(key.value);
if (window.XMLHttpRequest){
req = new XMLHttpRequest();
}
else if (window.ActivateXObject){
req = new ActiveXObject("Microsoft.XMLHTTP")
}
req.open("Get",url,true);
req.onreadystatechange = callback;
req.send(null);
}
function callback() {
if (req.readyState==4) {
if (req.status == 200){
var decimal = document.getElementById("decimal");
decimal.value = req.responseText;
}
}
clear();
}
function clear() {
var key = document.getElementById("key");
key.value="";
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ajax on Java Chapter 2</title>
</head>
<body onload="focusIn();" >
<!-- this is how you comment in HTML Body Portion of the code -->
<h1> AJAX CHARACTER DECODER </h1>
<h2> Press a key to find its value. </h2>
<table>
<tr>
<td>
Enter Key Here --
<input type="text" id="key" name="key" onkeyup="convertToDecimal();" />
</td>
</tr>
</table>
<br />
<table>
<tr>
<td colspan="5" style="border-bottom:solid black 1px;">
Key Pressed:
<input type="text" readonly="readonly" id="keypressed" />
</td>
</tr>
<tr>
<td> Decimal </td>
</tr>
<tr>
<td>
<input type="text" readonly="readonly" id="decimal" />
</td>
</tr>
</table>
<!-- this is how you comment in HTML Body Portion of the code -->
<h1> AJAX CHARACTER DECODER </h1>
<h2> Press a key to find its value. </h2>
<table>
<tr>
<td>
Enter Key Here --
<input type="text" id="key" name="key" onkeyup="convertToDecimal();" />
</td>
</tr>
</table>
<br />
<table>
<tr>
<td colspan="5" style="border-bottom:solid black 1px;">
Key Pressed:
<input type="text" readonly="readonly" id="keypressed" />
</td>
</tr>
<tr>
<td> Decimal </td>
</tr>
<tr>
<td>
<input type="text" readonly="readonly" id="decimal" />
</td>
</tr>
</table>
Мой текущий файл web.xml:
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5"> Ajax2 index.html index.htm index.jsp default.html default.htmdefault.jsp AjaxResponseServlet AjaxResponseServlet com.example.servlets.AjaxResponseServlet AjaxResponseServlet / AjaxResponseServlet JAMES JAMES com.example.servlets.JAMES введите здесь:
Мой сервлет * * */ * * Берет символ, преобразует его в десятичное и отправляет обратно * значение в ответе.* / // package com.oreilly.ajax.servlet;// вызывает ошибку, поэтому закомментирован
import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
открытый класс AjaxResponseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String key = req.getParameter("key");
if (key != null) {
// extract the first character from key
int keyInt = key.charAt(0);
String decimalString = Integer.toString(keyInt);
// setup the response
res.setContentType("text/xml");
res.setHeader("Cache-Control", "no-cache");
// write out the response string
res.getWriter().write(decimalString);
}
else {
// If key comes back as a null, return a question mark.
res.setContentType("text/xml");
res.setHeader("Cache-Control", "no-cache");
res.getWriter().write("?");
}
}
}