dav191
dav191

Reputation: 75

How to send input value as request parameter of a link to a servlet?

I have a jsp page that loads content from the database according to what the user needs to see (Loading book information). But i also need the user to enter to enter the quantity at the same jsp page.

I used JSTL tags to retrieve information from the database and display it using the following code.

 <c:forEach var="book" items="${listBooks.rows}">
    <tr>
      <td><c:out value="${book.bookID}"/></td>
      <td><c:out value="${book.bookName}" /></td>
      <td><c:out value="${book.bookType}" /></td>
      <td><c:out value="${book.Price}" /></td>

This information is set to c:param so i can use it in the servlet.

I have a HTML text for the user to enter the quantity. My question is how can i send this as a parameter to the servlet since the request object is not retrieving the quantity parameter (I'm guessing this is because it's not inside a form that directs to the specific servlet (AddBookServlet).

My code

<form>
<td>Quantity:  <input type="text" name="quantity" value="" style="height:30px; width: 45px"/>

</td>
 </form>
 <td><a style="color: blue;" class="myButton1"
       href=<c:url value="/AddBookServlet">

       <c:param name="id" value="${book.bookID}"/>
       <c:param name="name" value="${book.bookName}"/>
       <c:param name="type" value="${book.type}"/>
       <c:param name="amount" value="${book.price}"/>

       <c:param name="seat" value="${param.quantity}"  /><%-- not sure if this is the way to do it--%>

</c:url> > &nbsp;&nbsp;&nbsp; Add book</a> 

Upvotes: 1

Views: 3237

Answers (2)

Angelo Oparah
Angelo Oparah

Reputation: 673

Never seen such approach before: why having a form without an action, method and submit button?

If you use it the way it's intended to be used you should be fine.

You can pre-populate the form with values retrieved from the database and leave the quantity input field blank, then retrieve the quantity attribute as you would normally do

<c:forEach var="book" items="${listBooks.rows}">
    <form action = "/AddBookServlet" method = "post">

        <td>Book name: <input type="text" name = "bname" value="${book.bookName}"/></td>
        <!-- put the rest of the fields here !-->

        <td>Quantity:  <input type="text" name="quantity" value="" style="height:30px; width: 45px"/></td>

        <input type="submit" value="Add Book"/>

    </form>
</c:forEach>

Now if you want to prevent the user from editing input fields apart from quantity, which I think is what you were trying to achieve you can add the readonly HTML attribute (or set the input type to hidden if for some reason you don't want the field to be displayed to the user)

<td>Book name: <input type="text" name = "bname" value="${book.bookName}" readonly="readonly"> </input></td>

Then in the doPost method of the "/AddBookServlet" you should be able to retrieve each field calling the request.getParameter("nameOfParameter")

Upvotes: 2

Peter
Peter

Reputation: 333

You are over-complicating it I think. Forget about <c:param> here.

For the <form>, you should define action="YourServletName" and method="GET" or method="POST", then in the servlet's doGet() or doPost() you can read the parameter like this:

String quantity = request.getParameter("quantity");

Then of course you'll cast it to some kind of numeric type.

Upvotes: 0

Related Questions