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

人盡茶涼 提交于 2019-12-25 02:13:24

问题


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.


回答1:


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=...




回答2:


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>


来源:https://stackoverflow.com/questions/20936738/unable-to-retrieve-the-passed-value-from-jsp-to-controller-using-url-via-a-butto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!