0

I have te following problem: Imagine we have a list of people, and we have a button next to them. When we click the button we need to sent te id of the user(which is a long type) to the servlet. We generate the people list with "for" cycle, but we don't show anywhere this ID, only if the buton on this person is clicked then we send this person's ID.

Here is example:

John -- 23 --- male---(button) Meet (his id is 2)

Marty-- 26 --- female---(button) Meet (her id is 5)

George -- 25 --- male---(button) Meet (his id is 4)

So if we want to meet John, and click the button, the servlet recieves 2 as an id. So how I can do this without having the id anywhere in the jsp code but in the objects I loop trough?

1 Answer 1

1

If you want to simply make a GET request, use a link:

<a href="meet?id=${person.id}">Meet</a>

If you want to post, use a form:

<form action="meet">
    <input type="hidden" name="id" value="${person.id}"/>
    <input type="submit" value="Meet"/>
</form>

If you want to send an AJAX request, then it depends a lot of what you use as JavaScript library/framework, but you can for example store the ID ad a data attribute, and get it from the clicked element in the click handler:

<button type="button" data-id="${id}">Meet</button>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.