We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x
Private

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

Medium Priority
61 Views
Last Modified: 2020-06-15
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

Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
"/src/com/webApp/RegisAction.java"
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?

Author

Commented:
@ girionis I am trying to send the objects from jsp to servlet.in the Ajax call.
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Author

Commented:
@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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
,here I am using .serializeArray() in jquery)
Why? Do you know what that does?

Author

Commented:
@ 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.

Author

Commented:
@ cehj please suggest the right approach.
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
@ 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?

Author

Commented:
@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.
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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.

Author

Commented:
@ 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
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
OK. Do you understand the Ajax code you posted?
CERTIFIED EXPERT
Top Expert 2016

Commented:
It might help if you posted a screenshot of the form itself, with (if necessary) comments about how it's supposed to work

Author

Commented:
@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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Author

Commented:
@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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Again, you're not telling me what it's meant to do

Author

Commented:
@ 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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
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 ..."

Author

Commented:
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.
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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.

Author

Commented:
@girionis ok, thanks
rrzstudent
CERTIFIED EXPERT

Commented:
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.
rrzstudent
CERTIFIED EXPERT

Commented:
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.
rrzstudent
CERTIFIED EXPERT

Commented:
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.
student
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT
Top Expert 2016

Commented:
What happened to the justification? ;)
rrzstudent
CERTIFIED EXPERT

Commented:
@CEHJ ,  I just couldn't  let that bad code stand.
CERTIFIED EXPERT
Top Expert 2016

Commented:
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.
rrzstudent
CERTIFIED EXPERT

Commented:
In an earlier question,  I  posted that getParameter would work.  I just wanted  to  correct my mistake.

Author

Commented:
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. 
CERTIFIED EXPERT
Top Expert 2016

Commented:
MUCH better. Let us all think about that
rrzstudent
CERTIFIED EXPERT

Commented:
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 ?
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Author

Commented:
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.
rrzstudent
CERTIFIED EXPERT

Commented:
What  will the client do after he reads the message?
CERTIFIED EXPERT
Top Expert 2016

Commented:
OK, so the goal is to enable the user to determine which certificates are expired?

Author

Commented:
Client will upload the PDF for signing.
@ cehj yes

rrzstudent
CERTIFIED EXPERT

Commented:
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?

Author

Commented:
@rrz will give the link,yes in the current page only link will be provided if the chosen certificate is valid.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Client will upload the PDF for signing.
What's that about? What's the purpose of the pdf?

Author

Commented:
Upload PDF For digital signing.pdf some official document,will go to the server for verification.
CERTIFIED EXPERT
Top Expert 2016

Commented:
OK - so what's the relation between the certificates and their status and this 'official document'?

Author

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Wouldn't it be better to provide the following bits of user functionality first?

a. show me all the valid certificates

Author

Commented:

Yes ok, then from valid certificate s user will choose one and proceed forward right.
CERTIFIED EXPERT
Top Expert 2016

Commented:
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?

Author

Commented:
No PDF link will be only provided once the user is registered.registerd with certificate details.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Oh OK. So the user clicks the radio button, choosing the cert and that then goes to a registration page?
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.