We help IT Professionals succeed at work.

dots added to string after AJAX call returns

yac678
yac678 asked
on
I am writing code that would enable dealing with mass of data that needs to be sent between client and server. The idea is to devide the data into chunks and send each chunk to the server using AJAX. The server has a Servlet that receives the call and caches the data. Here is a simplified version of the servlet code:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF8");
        PrintWriter out = response.getWriter();
        out.println("NONE");  
    }

and here is a simplified version of the the AJAX callback method:


    function gotTotalChunks()
    {
        if (xmlhttp.readyState==4)
        {
            var responseText = xmlhttp.responseText;
            alert("responseText="+responseText)
            if (responseText!="NONE") {
                alert("non NONE!")
            }
        }
    }

The problem is that instead of receiving "NONE" I receive "NO..." and a "non NONE!" message!
What is going on? Could it be related to Locale (Hebrew)? I am using Google Chrome browser, and Tomcat web server.
Comment
Watch Question

Try below two options one after another (apply second if first do not work)

1) remove, request.setCharacterEncoding("UTF8");
2) try to send some number from servlet as: out.println("123");   , and test in ajax if you are getting 123 or not

Author

Commented:
Neither suggestion worked. For the second, I got "1...". It's like the server or the browser insists on adding "..." at the end no matter what...
That means, something is between your servlet response, and ajax.

Are you able to share your whole code or atleast explain the structure of your program/files ?

Author

Commented:
I have no problem sharing more code. Here is the servlet:

import web.control.LoginHandler;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Jun 10, 2010
 * Time: 6:54:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class CacheRequest extends HttpServlet {
    private static HashMap<String, Object> cache = new HashMap<String, Object>();

    public static Object get(HttpServletRequest request, String var) {
        String userName = LoginHandler.getUserName(request);
        String key = makeKey(userName, var);
        return cache.get(key);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("UTF8");
        PrintWriter out = response.getWriter();
//        String userName = LoginHandler.getUserName(request);
//        String putParams = request.getParameter("put");
//        if (putParams != null) {
//
//            String[] varval = putParams.split(";");
//            for (int i = 0; i < varval.length; i++) {
//                String[] pair = varval[i].split(":");
//                String var = pair[0];
//                String val = pair[1];
//                String key = makeKey(userName, var);
//                cache.put(key, val);
//            }
//            out.println("OK");
//        } else {
//            String getParams = request.getParameter("get");
//            if (getParams != null) {
//                String key = makeKey(userName, getParams);
//                Object value = cache.get(key);
//                if (value != null) {
//                    out.print(value);
//                } else {
                    out.println("123");
//                }
//            } else {
//                out.println("OK");
//            }
//        }
//        out.flush();
    }

    private static String makeKey(String userName, String var) {
        return userName + "." + var;
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
and here are the AJAX-related parts of the JSP:

<html>
<%
    request.setCharacterEncoding("UTF8");
%>
<head>
<script language="javascript">
    function getXmlHttpObject()
    {
        if (window.XMLHttpRequest)
        {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            return new XMLHttpRequest();
        }
        if (window.ActiveXObject)
        {
            // code for IE6, IE5
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
        alert ("Your browser does not support XMLHTTP!");
        return null;
    }

    function gotTotalChunks()
    {
        if (xmlhttp.readyState==4)
        {
            var responseText = xmlhttp.responseText;
            alert("responseText="+responseText)
            if (responseText!="NONE") {
                alert("non NONE!")
//                totalChunks=responseText;
//                getKeywordChunk(0);
            }
        }
    }
    function getKeywordChunk(chunkNumber) {
        xmlhttp = getXmlHttpObject();
        if (xmlhttp==null)
        {
            return;
        }
        var url="CacheRequest?get=chunk."+chunkNumber;
        xmlhttp.onreadystatechange=function(chunkNumber) {
            if (xmlhttp.readyState==4)
            {
                var responseText = xmlhttp.responseText;
                alert("responseText =" + responseText);
                if (responseText != "") {
                    chunks[chunkNumber]= xmlhttp.responseText;
                    if (chunkNumber+1==totalChunks){
                        gotAllKeywordChunks();
                    } else {
                        getKeywordChunk(chunkNumber+1);
                    }
                }
            }
        }
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);
    }
    function gotAllKeywordChunks() {
        var allKeywords;
        for (var i=0; i<totalChunks; i++) {
            allKeywords += chunks[i] + "\\n";
        }
        document.getElementById("keywords").value = allKeywords;
    }


    function sendKeywordChunk(chunkNumber) {
        xmlhttp = getXmlHttpObject();
        if (xmlhttp==null)
        {
            return;
        }
        var url="CacheRequest?put=";
        if (chunkNumber==0) {
            url+="totalChunks:"+totalChunks+";";
        }
        url+=("chunkNum:"+chunkNumber+";chunk:"+decode(chunks[chunkNumber]));
        xmlhttp.onreadystatechange=function(chunkNumber) {
            if (xmlhttp.readyState==4)
            {
                if (chunkNumber<totalChunks){
                    sendKeywordChunk(chunkNumber + 1);
                } else {
                    window.location = target;
                }
            }
        };
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);

    }

    var chunks = new Array();
    var totalChunks = 0;
    var target;
    var xmlhttp;

    function retrieveKeywords() {
        //            alert("retrieveKeywords");
        xmlhttp = getXmlHttpObject();
        if (xmlhttp==null)
        {
            return;
        }
        var url="CacheRequest?get=totalChunks";
        xmlhttp.onreadystatechange=gotTotalChunks;
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);


        //            if (keywords != "") {
        //                document.getElementById("keywords").value = keywords;
        //            }
    }
</script>
<link rel="stylesheet" type="text/css" href="app.css" />
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
</head>
<body onload="retrieveKeywords()">
<FORM>
                <textarea name="keywords" id="keywords" rows="25" cols="25" dir="rtl"></textarea>
</FORM>
</body>
</html>
As per your above sent code:
1) the web page is calling (GET) to servlet via js function retrieveKeywords()
2) In servlet, doGet() is calling doPost()
3) I can see all the codes in doPost() are commented.

Please share work same working code, so that i can analyze.

Author

Commented:
Not all lines in doPost() are commented - two lines remain uncommented:
PrintWriter out = response.getWriter();
and
out.println("123");
Sorry for the confusion.
have you tried in any other browser?

Author

Commented:
I just tried IE 6 and it works fine! That is, I see "123". So is it a bug in Chrome?
yea, sure...but don't know why :(
Commented:
OK, I figured it out. The problem was caused because I used out.println("123") as opposed to out.print("123"). The newline was added, causing Chrome to display it as "..." hiding some of the previous contents. After I changed to out.print("123"), everything works fine.