ResponseText即使应该设置JavaScript也为null

时间:2012-05-19 08:00:41

标签: java javascript ajax servlets

我遇到了一个大问题,我的代码中没有发现任何错误。该程序应该从字段p1p2添加两个值,并将它们上传到站点而不重新加载整个页面。我是用简单的javascript做的,但我想用AJAX。一切都已经完成,但某处有小错误。

我看一下validate()insterter(),警报有适当的值

 alert("data = "+data);
 req.send(data);

但来自insterter的消息只是空的。我不知道为什么。请帮忙。我想更正此代码。

var msg = req.responseText;
        alert("msg = "+msg);

为什么msg为空的原因?


@WebServlet("/Serwlet")
public class Serwlet extends HttpServlet {
private static final long serialVersionUID = 1L;
static String KOD =

"<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);
}

}

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\">"+"MSG WAS EMPTY"+"</div>";
            //document.getElementById("msg1").value = "blabla";
        }
        else
            document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+msg+"</div>";
            //document.getElementById("msg1").value = "COOOO JEST";
    }
}
}

这部分代码总是被调用MSG是“”

if (msg == "") {
        document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+"MSG WAS EMPTY"+"</div>";
        //document.getElementById("msg1").value = "blabla";
    }

1 个答案:

答案 0 :(得分:0)

raz似乎只有一次,所以下次进入servlet时,你不要输入if语句而不打印Serwlet.KOD

然后我不明白:

  String org = ""; //jakas wartosc 
  out.write(org != null ?  org : "")

它总是打印一个空字符串,它没用。

使用serviceRequest结束out.close();方法也是一种很好的做法。