Safirah
Safirah

Reputation: 375

How to pass multiple inputs to jsp

In my jsp file I generate dynamically multiple input tags, trough a database. I'd like to know the values of each. I've tried doing it in Javascript, but according to some answers in this website this is not possible. Example:

<input type="number" id="age" class="v">
<input type="text" id="name class="v">
...

And on the jsp side I'd like to get:

"age" => 18
"name" => "Joe"

Any ideas on how to achieve this?

Edit

In case you are wondering, my Javascript is fairly simple, I can get all the values I need simply by doing:

var chars = document.getElementsByClassName("v");

EDIT 2 My (simplified) JSP looks something like this:

<%= session.getAttribute("chars")%>
    <form action="hello" method="POST">
    <c:forEach items="${chars}" var="ch">
        <input type="number" id="${ch}" class="v">
    </c:forEach>
    <input type="submit">
    </form>

"chars" is an array that was created through calls to the database, this array is displayed and created dynamically. So what I want to do is pass all those values, like ("age" => 18) to another my hello servlet, so I can work on that info. For example if the value of the inputs is something like this: //ID value "age" => 18 "name" => "Joe"

On hello I should have access to that.

Upvotes: 0

Views: 2323

Answers (2)

coletrain
coletrain

Reputation: 2849

Using pure JavaScript you can get the field values using querySelector which allows you to retrieve properties from multiple elements with the same class identifier

document.querySelector('.v').value;

You will then have access to both field values via js. To read those values using jsp you the JavaScript will need to be included within the same file. You can do something like:

JSP:

You will need to add the HTML Name="myFieldName" to the input fields.

<%= request.getParameter("myFieldName") %>

Upvotes: 1

Sirmyself
Sirmyself

Reputation: 1564

If you are in a form, you could use a POST method to submit the datas of the different inputs inside your form.

Here is a previous post on StackOverlow that might help you get the datas you want :

How can I get form data with JavaScript/jQuery?

Upvotes: 0

Related Questions