Link to home
Start Free TrialLog in
Avatar of smriti priyanka
smriti priyanka

asked on

how to retreive objects in servlet which is set in the jsp page through jquery ajax call.

how to get the response of the object in the servlet whose object is set in the jsp page through ajax call.I am attaching the code snippet of the ajax call in jsp.

My question is:-what to write in post method of the servlet to retreive the object which is set in the jsp in through the ajax.

how to retreive contents from myForm.serializeArray() in the servlet.



	<script>
		document.addEventListener("click", function(event) {
			alert("inside ajax call");
			var target = event.target;
			if (target.classList.contains("submit-btn")) {
				event.preventDefault();
				alert("after event prevent Default");
				//var isValid = validate();
				//if (true) {
					alert($(this).closest("form"));
					var myForm = $(this).closest("form");
					alert("my Form values is"+myForm.serializeArray());
					$.post("/src/com/webApp/RegisAction.java", myForm.serializeArray(),
							function(dataBackFromServlet) {
								alert("certificate sent to server");
								// maybe you want to process data Back From Servlet once
								// the Ajax call is done instead this stupid alert below
								alert(dataBackFromServlet.toString());
							});
			//	}
			}
		});
	</script>

Open in new window

Avatar of girionis
girionis
Flag of Greece image

"/src/com/webApp/RegisAction.java"

Open in new window

The above is wrong. The POST should be in the servlet's listening endpoint, not in the file.

Can you tell us what you're trying to do?
Avatar of smriti priyanka
smriti priyanka

ASKER

@ girionis I am trying to send the objects from jsp to servlet.in the Ajax call.
I think you might be a little confused about Ajax/Javascript. You must realize that this technology is client side, so the processing is done in your browser and then posted to the servlet
@cehj ,yes The objects posted to servlet needs to be retreived at the servlet side,I want those objects to retreived and proceed forward.

but I don't know how to retreive it.I.e I have learnt earlier that it could be retreived using request.getParameter("hereinputtype name") by using this we were able to retreive the input type value in the servlet.

In the above mentioned jquery post mentioned(whose code snippet i have attached above,here I am using .serializeArray() in jquery),here i also want input type values which is set in jsp ,in the servlet.My question is here i am not knowing how to retreive it in the servlet.

Please help what to write to get the inputtype(i.e in this case hidden values) values in servlet.
,here I am using .serializeArray() in jquery)
Why? Do you know what that does?
@ cehj creates an array of object.i am guessing it.Please suggest if not how to write Ajax call and retreive in servlet.what to write then.
@ cehj please suggest the right approach.
@ girionis I am trying to send the objects from jsp to servlet.in the Ajax call.
Why not simply send it from JSP to a servlet? Why the Ajax call? Why don't you just submit the form?
@girionis becoz there not one submit button,there are more then one submit button, more than one submit button is there ,user selects one then the data needs to be submitted.
I don't understand what you mean. The Ajax code you have submits a form (in a weird way), you can do the same with JSP.
@ girionis on choice of user form needs to be submitted,directly not.
I.e There are not one submit button,there are multiple submit buttons
OK. Do you understand the Ajax code you posted?
It might help if you posted a screenshot of the form itself, with (if necessary) comments about how it's supposed to work
@girionis its new to me,but some understanding i had gained about not very clear.so,I request you if u can give me a proper understanding.Thanks

@cehj,Please find the screenshot.on click of submit button may be not submit button,suppose radio button I have to pass the details from jsp to the servlet ,which is shown in the jsp page i have attached.User generated image
submit button may be not submit button,suppose radio button
Whether that's a submit button or radio buttons with a submit button, that just looks like a normal form to me. So i'm wondering why you're using Ajax at all?

You have also not said what your goal is with the screenshot you posted. You must design software based on your goal, not on the steps you imagine (quite possibly wrongly) you need to achieve that goal.
http://technojeeves.com/joomla/index.php/free/117-smart-questions
@cehj Requirement may change it's radio button ,then we have to call Ajax .please suggest me how to call through Ajax. Through submit button I know.but I want to know through ajax.so, please help me.
Again, you're not telling me what it's meant to do
@ cehj I want to pass the values of the form through Ajax call.so, please suggest how to pass the values through Ajax call in servlet.
Sorry - if you're not going to answer the question, i'm not going to bother any more. You're simply ignoring what i'm telling you. Your answer (i shall give you one more chance only to give it) should be something like "The form shows which certificates we want to process so that we can foo and bar ..."
Sorry @ cehj ,I am not ignoring your question,what question please tell,I am not getting your question properly. Please tell what you wanted to ask.
If you don't (or only half) understand how Ajax calls/form submission works, I suggest you take a step back and go through some JSP/Servlet tutorials before you delve further into this.

You're asking for help about things you don't understand, this is only going to make it worse.
@girionis ok, thanks
I have read all your previous questions.  girionis and CEHJ ask important questions to you here.
People use AJAX when they want the page to communicate with the server without navigating to a different page or without refreshing the current one.
You never told us what you plan to do after making the AJAX call and updating your database.  
If you want the client to stay on the page he is currently on, then AJAX makes sense . If not, then you should submit the form in the standard way and have your Servlet forward the request to where ever you want your client to go after your database has been updated or just have the Servlet  send a response.
I am sorry.  In your previous question, I  assumed that the jQuery code that  leakim971 posted would  submit all your  parameters separately.  
But, I was wrong.  See
https://api.jquery.com/serializeArray/ 
which states
The .serializeArray() method creates a JavaScript array of objects, ready to be encoded as a JSON string.
 
But that is not what you want to use.  
I don't use  jQuery so I didn't even look it.
Please justify  your use of  jQuery.  If you do so, then we will have to rewrite your  jQuery.  Or figure out how to deal with  the array that is being sent to your Servlet by your current jQuery code.
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What happened to the justification? ;)
@CEHJ ,  I just couldn't  let that bad code stand.
Let's forget you just did that for the moment, and i'll repeat:

Please justify  your use of  jQuery.  If you do so, then we will rewrite your  jQuery.
In an earlier question,  I  posted that getParameter would work.  I just wanted  to  correct my mistake.
I have a web page with list of certificates and its details,with one submit button each.

Now ,user will choose one certificate,and on click of one submit button that is chosen by the user ,the details of the certificate needs to be send to the servlet. 
MUCH better. Let us all think about that
The question remains in my mind: why do you think you need AJAX?
What do you want to happen after making the AJAX call and updating your database?
What do you want your client to see afterwards ?
Better, but you're still thinking in terms of the technology rather than user functionality. e.g. the certificates are already on the server - that's where they've come from, so what you're saying needs to be done is not correct.

What do you want your client to see afterwards ?
Is what i want to know too. Always talk first about user requirements and functionality
After the submit button is clicked by the user the data goes to the database and on the same jsp page some message appears.i.e user certificate is valid or not.based on check done.
What  will the client do after he reads the message?
OK, so the goal is to enable the user to determine which certificates are expired?
Client will upload the PDF for signing.
@ cehj yes

Client will upload the PDF for signing.
How will he do that ?
Where  is the link to the PDF?
Are all the links to all the PDFs for all the certs on the current page?
@rrz will give the link,yes in the current page only link will be provided if the chosen certificate is valid.
Client will upload the PDF for signing.
What's that about? What's the purpose of the pdf?
Upload PDF For digital signing.pdf some official document,will go to the server for verification.
OK - so what's the relation between the certificates and their status and this 'official document'?
Will verify in the system whether certificate is valid or not ,if valid user will be allowed to upload the PDF and if PDF is valid ,user will be allowed to digital sign the pdf.I have already mentioned,in the above comments.
Wouldn't it be better to provide the following bits of user functionality first?

a. show me all the valid certificates

Yes ok, then from valid certificate s user will choose one and proceed forward right.
Yes. So as a pre-requirement, you need to tell the servlet or jsp (probably better a servlet) to get you only valid certs.
You need to get it to show you radio buttons so one of those can be selected. Together with that, on the same page, you could have a file chooser for the pdf could you not?
No PDF link will be only provided once the user is registered.registerd with certificate details.
Oh OK. So the user clicks the radio button, choosing the cert and that then goes to a registration page?