Rishabh
Rishabh

Reputation: 3852

unable to retrieve the passed value from jsp to controller using url via a button

I am assigning href to a button and passing a value to the controller

  1. JSP page

    <td><a href="viewController?book_id=<%=vo.getBookId()%>"><input type="button" name="remove" value="DELETE"/></a>
    </td>
    
  2. In controller, I am trying to get the value when the button is clicked using:

    if(request.getParameter("remove") !=null)
    {
        int cart_id=(Integer)request.getSession(false).getAttribute("cartid");
        b_id = (String) request.getParameter("book_id");
        int bid=Integer.parseInt(b_id);
        System.out.println(bid);
    }
    

This prints a null value though I have passed book_id value in the URL. I want to know how can I get the value passed in the URL via the button.

Upvotes: 0

Views: 1132

Answers (2)

ADS
ADS

Reputation: 718

When submit button is pressed, the entire form will be sent. You can select where data will be sent by using attribute action:

<form action="demo_form.jsp"> 
  <!--inputs here-->
  <input type="submit">Send me now</input>
</form>

In that case form will be sent to demo_form.jsp. In HTML5 you can use formaction attribute if you want use different servlets for different buttons

Any way, you shouldn't use links for sending forms.

It is possible to use links without button:

<a href="viewController?book_id=<%=vo.getBookId()%>">Remove</a>

Upvotes: 2

Peter Quiring
Peter Quiring

Reputation: 1706

You can't combine [a] tag with an [input] tag like that. Try using a form instead with hidden inputs:

<form action=viewController>
<input type=hidden name=remove value=delete>
<input type=hidden name=book_id value=<%=vo.getBookId()%>>
<input type=submit value='Delete'>
</form>

The resulting url will be : viewController?remove=delete&book_id=...

Upvotes: 3

Related Questions