Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have found other question regarding on this, but still im having a problem,When i check getParamerterValues, its always return null, and i think im having trouble redirecting it to my controller.Bare with me, in new to web app.

arr = [];
$(document).on("click","#idhere",function(){
    $.post("servlet.html","ids="+arr+"",function(response){
    });
});

or is there something like converting array of javascript to JSON of array in JSP so i can pass it to my servlet?

 String arr [] = request.getParameterValues("ids");

if(arr != null){//this line doesnt return true even my array contains item}
share|improve this question
1  
your question title say you want to pass to servlet then why you are writting servlet.html? – SpringLearner Aug 1 '14 at 3:32
    
I agree with the above question. However, I would also pose the question of if 'servlet.html' exists within the same directory. – mwilson Aug 1 '14 at 3:35
    
servlet.html is the name of my java servlet(servlet.java) – bumbumpaw Aug 1 '14 at 3:58
    
Also servlet.java containes code that will redirect to jsp file – bumbumpaw Aug 1 '14 at 3:59
    
show your servlet code. check in browser network tab is your ajax call success or not. – Rembo Aug 1 '14 at 4:21

change

String arr [] = request.getParameterValues("arr");

to

String arr [] = request.getParameterValues("ids");
share|improve this answer
    
strill arr is null when i check on servlet sir – bumbumpaw Aug 1 '14 at 5:32
    
check your browser network tab ids has any data or not? and in your javascript code you have not initialized value to arr = [] at all. check out. – Rembo Aug 1 '14 at 5:34

Its always return null, and i think im having trouble redirecting it to my controller

I couldnt see any code to pass the values to the controller. In your jquery function , you have something like this $.post("servlet.html","ids="+arr+"",function(response){

It is not possible to post a value to the other html file from jsp. if you are trying to pass the values to the servlet.

try something like this ,

$(document).on("click","#idhere",function(){
    $.post("servletName","ids="+arr+"",function(response){
    });
});

Note: servletName refers to the url , mapped in your web.xml for the servlet you are trying to post the data.

Also make sure you jquery function is posting the data correctly to the controller through the browser console .

share|improve this answer
    
this is working "servlet.html" when i used in other case ,only if data is JSON.stringify(array), $.post("servlet.html","ids="+JSON.stringify(array)+"",functi‌​on(response) – bumbumpaw Aug 1 '14 at 5:30
    
sorry couldnt get you , please be more clear – San Krish Aug 1 '14 at 5:32
    
$.post("servlet.html","data i passed here before is working",function(response) so i think this redirecting is not the issue – bumbumpaw Aug 1 '14 at 5:34
    
can you post , what you have inside "servlet.html" ? – San Krish Aug 1 '14 at 5:38
    
sir,wai ill post it later – bumbumpaw Aug 1 '14 at 7:43

As the examples below show, the post method wants an json object as the data parameter.

This should do the job

$.post("servlet.html",{"ids": arr.join()},function(response){
    });

Examples: Example: Request the test.php page, but ignore the return results.

1 $.post( "test.php" ); Example: Request the test.php page and send some additional data along (while still ignoring the return results).

1 $.post( "test.php", { name: "John", time: "2pm" } ); Example: Pass arrays of data to the server (while still ignoring the return results).

1 $.post( "test.php", { 'choices[]': [ "Jon", "Susan" ] } ); Example: Send form data using ajax requests

1 $.post( "test.php", $( "#testform" ).serialize() );

see http://api.jquery.com/jquery.post/

share|improve this answer
    
test.php will work because php is a server side page but servlet.html wont work because it is a static page. – SpringLearner Aug 1 '14 at 3:53
    
what servlet.html is depends on the server. When we are talking about tomcat and servlets servlet.html can an the public access point of the servlet. – Hank Lapidez Aug 1 '14 at 5:55
    
how can you write doPost or doGet methods in html page – SpringLearner Aug 1 '14 at 5:59
    
have a look at this gist.github.com/hlapidez/e4ff6e54fba786d625be – Hank Lapidez Aug 1 '14 at 7:23
    
Are you sure OP has also configured web.xml like this? – SpringLearner Aug 1 '14 at 8:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.