Olá pessoal, estou com o seguinte problema:
possuo um form em um JSP, com dois botões: Click e Clear. Ao acioná-los, invoco métodos em um arquivo JS para efetuar umas validações.
Caso eu acione o botão Click com um valor preenchido no input text do form, uma servlet será invocada para que possa pegar o valor entrado no input text e exibi-lo em um alert javascript.
A questão é que no momento de fazer a seguinte passagem na minha servlet:
String valor = req.getParameter("inputbox");
não está trazendo o valor e assim, no alert é exibido nulo.
Será algum problema ao invocar a minha servlet?
Seguem os códigos:
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>JSPJS</display-name>
<servlet>
<servlet-name>JSAjax</servlet-name>
<servlet-class>br.js.servlets.TesteJS</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JSAjax</servlet-name>
<url-pattern>/ajax</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>JSteste.jsp</welcome-file>
</welcome-file-list>
</web-app>
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>
<script type="text/javascript" src="js/field_validate.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP</title>
</head>
<body>
<form name="myform" method="post">
Enter something in the box: <BR>
<input type="text" name="inputbox" value=""/><P>
<input type="button" name="button1" value="Click" onclick="testResults(this.form)">
<input type="button" name="button2" value="Clear" onclick="cleanResults(this.form)">
</form>
</body>
</html>
JavaScript
var req;
var isIE;
function initRequest(url) {
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
isIE = true;
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
req = new ActiveXObject("Msxml2.XMLHTTP");
}
}
function testResults(form) {
var TestVar = form.inputbox.value;
if( TestVar == "" ) {
alert("Deve-se digitar algo!");
} else {
var url = "ajax";
initRequest(url);
req.onreadystatechange = processRequest;
req.open("GET", url, true);
req.send(null);
}
}
function cleanResults( form ) {
var TestVar = form.inputbox.value;
if( TestVar == "" ) {
alert("Nada para limpar!");
} else {
form.inputbox.value = "";
}
}
function processRequest() {
if (req.readyState == 4) {
if (req.status == 200) {
var texto = req.responseText;
alert(texto);
}
}
}
Servlet
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String valor = req.getParameter("inputbox");
resp.setContentType("text/xml;charset=UTF-8");
resp.setHeader("Cache-Control", "no-cache");
resp.getWriter().write( "Você digitou: " + valor );
}
Qualquer dica ajudará bastante.
Abs.