1

I am making an ajax call in my javascript submit function. In this ajax call, I am passing an array(globalSelection) as data to the servlet. This array consists elements of function textSelection which is also pasted below.

globalSelection =[];

function submit() {

    console.log("globalSelection start")
    console.log(globalSelection)
    console.log("globalSelection end")

        $.ajax({
            async : false,
            type : "POST",
            url : 'http://example.com:8080/myApp/DataServlet',
            data: {globalSelection:globalSelection},
            success : function(data) {
                alert(data)
            },
            error : function(data, status, er) {
                alert("error: " + data + " status: " + status + " er:" + er);
            }
        }); 

}

function textSelection(range, anchorNode, focusNode) {
    this.range = range;
    this.type = 3;
    this.rCollection = [];
    this.textContent = encodeURI(range.toString());
    this.anchorNode = anchorNode;
    this.focusNode = focusNode;
    this.selectionId = getRandom();
    this.yPOS = getYPOS();

    this.getTagName = function(range) {
        var el = range.startContainer.parentNode;
        return el;
    }
    this.getTagIndex = function(el) {
        var index = $(el.tagName).index(el);
        return index;
    }

    this.simpleText = function(node, range) {
        if (!node)
            var entry = this.createEntry(this.anchorNode, this.range);
        else
            var entry = this.createEntry(node, range);
        this.rCollection.push(entry);
        this.highlight(this.rCollection[0].range);
        this.crossIndexCalc();
        textSelection._t_list.push(this);
        pushto_G_FactualEntry(this);
    }

    this.compositeText = function() {
        this.findSelectionDirection();
        var flag = this.splitRanges(this.anchorNode, this.focusNode,
                this.range.startOffset, this.range.endOffset);
        if (flag == 0) {
            for (j in this.rCollection) {
                this.highlight(this.rCollection[j].range);
            }
        }
        this.crossIndexCalc();
        textSelection._t_list.push(this);
        pushto_G_FactualEntry(this);
    }

}

I am ading the screen of my browser console below, which prints the globalSelection(array).

enter image description here

In my servlet I am getting this array as follows

String[] arrays = request.getParameterValues("globalSelection[]");
System.out.println(arrays);

Here I am getting null value for arrays.

If I put globalSelection as follows in submit function for simple test to servlet, I am able to get the arrays.

var globalSelection = ["lynk_url", "jsonBody", "lynk_dummy1", "lynk_dummy2", "lynk_name", "lynk_desc", "lynk_flag"];

Why my actual globalSelection is shows null in servlet, what I am doing wrong here.

  • What is the expected type on your Java server? my guess is that you are trying to get String[] but are actually sending textSelection[]. What fields from textSelection do you want to receive in the backend? Are you using any framework for the backend? because sending an array of objects is not that simple (because you have to reconstruct the object in the backend). – tudor.gergely Apr 16 '16 at 09:15
  • @tudor.gergely I am using servlets and JSP only, not any frameworks.. I need all the fields from this array to my servlet, then I will do some json conversion then stored in to DB... –  Apr 16 '16 at 09:27

2 Answers2

0

Try with : String[] arrays = request.getParameterValues("globalSelection"); System.out.println(arrays);

Because the parameter submitted with name "globalSelection" only not "[]" symbol.

Pankaj Verma
  • 191
  • 10
0

I see your problem and I have a simple solution.

I recommend in that case that you convert the array as a string in JS:

JSON.stringify(globalSelection) 

and then reconstructing the object on the backend using some sort of library for JSON conversion like: https://code.google.com/archive/p/json-simple/

You could then do something like this:

JSONArray globalSelection = (JSONArray) new JSONParser().parse(request.getParameter("globalSelection"));
    Iterator i = globalSelection.iterator();

    while (i.hasNext()) {
        JSONObject selection = (JSONObject) i.next();
        String type = (String)selection.get("type");
        System.out.println(type);
    }

This will parse your array and print the selection type. Try it, hope it helps.

tudor.gergely
  • 4,800
  • 1
  • 16
  • 22
  • I tried this actually, but Safari is not accepts this JSON.stringify(globalSelection) ... Pls see my question http://stackoverflow.com/questions/36640582/how-to-make-ajax-call-with-array-and-string-parameters/. I couldn't found a way to get work this in Safari. So I am looking for send this as a array to backend. –  Apr 18 '16 at 05:18