Boa tarde pessoal. Seguinte estou estudando olivro de AJAX com Java da ALTA BOOKS O’REILLY Steven Douglas Olson. Mas logo de cara ja tive um problema e não estou achando uma solução. No primeiro exercicio que é pra converter o valor de uma tecla para Decimal, fiz tudo conferme o livro mas não esta imprimindo o valor decimal na tela. Vou colocar os codigos aqui pra ver se algum Ajax Man pode me dar uma força.
Minha jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Code Converter</title>
<link rel="stylesheet" type="text/css" href="../styles/style.css">
<script language="JavaScript" src="../scripts/ajax.js"></script>
</head>
<body onload="focusIn()">
<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" onkeyup="convertToDecimal()" /></td>
</tr>
</table>
<br clear="all" />
<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 id="decimal" /></td>
</tr>
</table>
</body>
</html>
Meu *.js
var req;
function convertToDecimal(){
var key = document.getElementById('key');
var keypressed = document.getElementById('keypressed');
keypressed.value = key.value;
var url = "/JavaComAjax/response?key=" + escape(key.value);
if(window.XMLHttpRequest){
req = new XMLHttpRequest();
}
else if(window.ActiveXObject){
req = new ActiveXObject('Microsoft.XMLHTTP');
}
req.open('Get', url, true);
req.onreadystatechance = callback;
req.send(null);
}
function callback() {
alert('teste')
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 = '';
}
function focusIn() {
document.getElementById('key').focus();
}
Meu web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>JavaComAjax</display-name>
<servlet>
<servlet-name>AjaxResponseServlet</servlet-name>
<servlet-class>br.com.ajax.actions.AjaxResponseServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AjaxResponseServlet</servlet-name>
<url-pattern>/response</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Meu Servlet
package br.com.ajax.actions;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AjaxResponseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String key = req.getParameter("key");
if(key != null && key.length() > 0) {
int keyChar = key.charAt(0);
String decimalString = Integer.toString(keyChar);
resp.setContentType("text/xml");
resp.setHeader("Cache-Control", "no-cache");
resp.getWriter().write(decimalString);
}
else {
resp.setContentType("text/xml");
resp.setHeader("Cache-Control", "no-cache");
resp.getWriter().write("?");
}
}
}
Usando o Firebug, ‘plugin para debugar no Firefox’ notei que nunca entra na funcion callback(), ai então é onde acho que esta o problema. Mas infelismente não tenho idéia de como resolver isso.
Desde já muito obrigado a todos.