1

I am uploading a file from JSP and processing it in a servlet and returning data to JSP.

JSP file that uploads the file:

$(document).ready(function() {
    $(':file').change(function(){
        var fileObj = this.files[0];
        var form = $('#upload');
        var fd = new FormData();    

        fd.append( 'file', fileObj);
        $.ajax({
            url: form.attr('action'),
            type: form.attr('method'),
            data: fd,
            processData: false,
                contentType: false,
            async: false,
        }).done(function(data){
            alert('ajax complete');
                $('#previewForm').append("<div>" + data + "</div>");
            $('#ldiv').hide();
        }).fail(function() {
            alert("error");
            $('#ldiv').hide();
        });
    }

Servlet file.

That read file which is upload by the jsp suppose upload Excel file. read that file data and convert that data into jsonString. Now I want to send that string as response of ajax call

I need to return more then 1 value

System.out.print("test.xlsx");  //File name 
System.out.print(jsonSting); // jsonSting is variable that is data of excel file which convert in json
System.out.print("chintan");  //other parameter.

So how can i Handel 3 response in JSP while ajax called....?

Joshua
  • 2,932
  • 2
  • 24
  • 40
Chintan
  • 545
  • 1
  • 9
  • 24

3 Answers3

1

Refer Link here In your jsp page

import org.json.simple.JSONObject;


      JSONObject obj = new JSONObject();

      obj.put("fileName", "test.xlsx");
      obj.put("jsonSting", jsonSting);
      obj.put("name", "chintan");

In Your ajax response

var json = $.parseJSON(data);

 $('#results').html('Filename name: ' + json.fileName + '<br />jsonSting: ' + json.jsonSting);
Yatin Mistry
  • 1,246
  • 2
  • 13
  • 35
0

why not wrap the response to a json style string , so you can handle the ajax call result in a json object. in you case, your jsp can response the result like this "{'fileName':'test.xlsx','jsonString':'****','chintan':'***'}"

if you have more than one ,you also can mark as the array within the json object in it

Feng Lin
  • 670
  • 4
  • 8
0

A single request in ajax will get only a single response. You can either create more requests, excellent solution related to this provided here,

OR

Better way you can append the extra parameters into the json string itself and change logic in you JS to extract those two values separately from excel data, while parsing the json string.

Community
  • 1
  • 1
Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28