user1733583
user1733583

Reputation:

Large data from Javascript to servlet

I have very large data like say a 100 lines text file in my <div> tag,

So I want to passtint to my java servlet and I've tried the following methods by getting the <div> value in javascript

Method 1:

var script = ace.edit("AceEditor"); //Getting the editor text value
var myDivText = script.getValue();

Function to add hidden and submit the form

function addHidden(UploadFile, key, value) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = key;
input.value = value;
UploadFile.appendChild(input);
}

Access the function

var UploadFile = document.forms['RunMy'];
addHidden(UploadFile, 'UserMyScript', myDivText);
document.RunMy.submit();

Method 2:

Get Editor text,

var script = ace.edit("AceEditor");
var myDivText = script.getValue();

window.open('http://xxx.xxx.xxx.xxx:8085/FirstServlet/my?mytxt=' + myDivText);

But, I've faced problems with both the methods as the data is being passed through the URL and data being very large here, the URL size is limited.

So, is there any method to pass large text from javascript to servlet?

Base on answer below, I've edited stuff like this

$("#execute1").click(function(){
$.ajax({
    url: '/YourServlet',
    type: 'POST',
    data: { value: $('#AceEditor'.val()) },
});
});

But when I click the execute1 button, the browser console shows

Uncaught TypeError: undefined is not a function MYJsp:144
(anonymous function) MYJsp:144
x.event.dispatch jquery.js:5095
v.handle

Upvotes: 0

Views: 850

Answers (1)

Santhosh
Santhosh

Reputation: 8197

Why dont you try with jquery ajax post,

$.ajax({
    url: '/YourServlet',
    type: 'POST',
    data: { value: $('#AceEditor'.val()) },
    success: function(result) {
        alert('the request was successfully sent to the server');
    }
});

And in your servlet get the values of the string using getParameter()

see here to know more about jquery ajax post

Hope this helps !!

Upvotes: 1

Related Questions