你好,我得到了这个脚本,但我不知道为什么我总是得到那个 req = XMLHTTPRequest 是未定义的。你能告诉我我犯了什么错误吗?
var req;
function validate() {
//var idField = document.getElementById("name");
var wrt1 = document.getElementById("p1").value;
var wrt2 = document.getElementById("p2").value;
var data = eval(wrt1)+eval(wrt2);
// var data = "name=" + encodeURIComponent(idField.value);
req = new XMLHttpRequest();
// req = new XMLHttpRequest();
var url = "Serwlet";
req.open("POST", url, true);
req.onreadystatechange = inserter;
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send(data);
}
function inserter() {
if (req.readyState == 4) {
if (req.status == 200) {
var msg = req.responseText
if (msg == "") {
//document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+"COS NIE TAK"+"</div>";
document.getElementById("msg1").value = '';
}
else
//document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+wynik+"</div>";
document.getElementById("msg1").value = "IT WORKS";
}
}
}
编辑
好的,我做了一些改进。现在唯一的问题是来自 JavaScript inserter() 的 msg 始终为空;/,不知道为什么,如果您查看 validate 方法,var 数据具有正确的值,则 msg 始终为空;/。
package Pakiet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Serwlet")
public class Serwlet extends HttpServlet {
private static final long serialVersionUID = 1L;
static String KOD =
/*
out.print("<FORM ACTION=\"ExerciseE\" name=\"entryForm\">\n"+
"Name:<INPUT TYPE=\"TEXT\" NAME=\"name\"><BR>\n"+
"<INPUT TYPE=\"BUTTON\" VALUE=\"SUBMIT\"\n" +
" OnClick=\"validate()\">\n</FORM>");
*/
"<html>"+
"<head>"+
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1250\">"+
"<title>Testowanie</title>"+
"<script type=\"text/javascript\" language=\"javascript\" src=\"servletD2Script.js\"></script>"+
"</head>"+
"<body>"+
"<div id=\"msg1\">Pierwsza suma</div>"+
"<div id=\"msg2\">Pierwsza suma</div>"+
"<center><h2>Testowanie parametrów</h2></center>"+
"<hr>"+
"<form method=\"get\" action=\"Serwlet\">"+
"p1<input type=\"text\" size=\"50\" name=\"p1\" onblur=\"validate()\"><br>"+
"p2<input type=\"text\" size=\"50\" name=\"p2\" onblur=\"validate()\"><br>"+
"<br><input type=\"submit\" value=\"Wyślij formularz by GET\">"+
"</form>"+
"</hr>"+
"<hr>"+
"<form method=\"post\" action=\"Serwlet\">"+
"p3<input type=\"text\" size=\"50\" name=\"p3\" onblur=\"funkcja()\"><br>"+
"p4<input type=\"text\" size=\"50\" name=\"p4\" onblur=\"funkcja()\"><br>"+
"<br><input type=\"submit\" value=\"Wyślij formularz by POST\">"+
"</form>"+
"</hr>"+
"</body></html>";
public Serwlet() {
super();
}
static boolean raz = true;
private void serviceRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
if(raz){out.print(Serwlet.KOD);raz = false;}
String name = request.getParameter("name"); // dostaję nazwisko
String org = ""; //jakas wartosc
out.write(org != null ? org : ""); // zwracam organizację lub pusty napis, jesli nie znam nazwiska
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
serviceRequest( request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
serviceRequest( request, response);
}
}
并且脚本//方法 FUNCKJA() 根本不重要
function funkcja(){
var wrt1 = document.getElementById("p1").value;
var wrt2 = document.getElementById("p2").value;
var wynik = eval(wrt1)+eval(wrt2);
document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+wynik+"</div>";
var wrt3 = document.getElementById("p3").value;
var wrt4 = document.getElementById("p4").value;
var wynik2 = eval(wrt3)+eval(wrt4);
document.getElementById("msg2").innerHTML = "<div style=\"color:red\">"+wynik2+"</div>";
}
var req;
function validate() {
//var idField = document.getElementById("name");
var wrt1 = document.getElementById("p1").value;
var wrt2 = document.getElementById("p2").value;
var data = eval(wrt1)+eval(wrt2)+"";
if (typeof XMLHttpRequest != "undefined") {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = "Serwlet";
req.open("POST", url, true);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
alert("data = "+data);
req.send(data);
req.onreadystatechange = inserter;
}
function inserter() {
if (req.readyState == 4) {
if (req.status == 200) {
var msg = req.responseText;
alert("msg = "+msg);
if (msg == "") {
document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+"COS NIE TAK"+"</div>";
//document.getElementById("msg1").value = "blabla";
}
else
document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+msg+"</div>";
//document.getElementById("msg1").value = "COOOO JEST";
}
}
}