Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do I send "unlimited" text in a parameter using "POST" in Ajax

843842Feb 25 2010 — edited Feb 26 2010
I'm trying to send large amounts of text data to the server using POST rather than GET, using Ajax
However, I am only able to send a max of 9.76 kb

I start with a jsp page, which calls a javascript method that utilises Ajax, which sends the text in a parameter in a XMLHttpRequest object
to a servlet. The text is derived from options values in a multi select combo box.

Here's my code:

Code snippet from .jsp page:
        <table>
	   <form name="form1" id="form1" method="POST">
	      <tr>
	         <td>
		   <select name="sel" id="sel" multiple="multiple" size="0"></select>
		</td>
		<td>
		   <input type="button" id="sendOpts" name="sendOpts" value="Send Options" onClick="jsObj.sendOpts()">
		</td>
	     </tr>
	  </form>
       </table>
Code snippet from jsObj.js file:
//=================================================
// jsObj object
//=================================================
jsObj = new jsObj();

function jsObj() {

	this.sendOpts = function() {

		if(!confirm('Send Option Values?'))
			return;
			
		if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		} else { // code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xmlhttp.onreadystatechange=function() {
			if(xmlhttp.readyState==4) { // 4 = The request is complete
				if (xmlhttp.status==200 || window.location.href.indexOf("http")==-1) {
					alert(xmlhttp.responseText); // When I copy and paste all the text from this alert to text editor and save the file, the largest the file can be is 9.76 kb.
				}
			}
		}
		
		var url = 'testSize'; // Servlet that simply retrieves the sent String in a parameter and sends it back to this method
		var sel = document.getElementById('sel');
		var str = sel.name + '=';
		var delim = ':';
		
		for(var i = 0; i < sel.length; i++) {
			str += encodeURIComponent(sel.options.value + delim); // 'encodeURIComponent' encodes any special characters within the parameter values
}

xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // This is needed for any POST request made via Ajax
xmlhttp.send(str); // send the parameter and it's value to the servlet
}
}
Code snippet from 'testSize' servlet:
package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;

public class TestSize extends HttpServlet {

public synchronized void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,IOException {
PrintWriter out = null;
try {
response.setContentType("text/html");
out = response.getWriter();
String str = request.getParameter("sel");
str = str.substring(0, str.length()-1); // cut off last delimiter
out.println(str);
} catch (Exception e) {

}
}

public synchronized void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Any help greatly appreciated.

Edited by: Irish_Fred on Feb 25, 2010 5:14 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 26 2010
Added on Feb 25 2010
5 comments
1,292 views