简体   繁体   中英

How to call doDelete in Servlet from jsp?

I am trying to write a simple client for rest api service which creates and deletes users.

I can call doGet and doPost from a form in the manner below:

<form action="SServlet" method ="get">
    <input type="submit" value="GET"/>
</form>

But when I do the same to call doDelete, it doesnt get called. Does something more need to be done for doDelete and doPost since they dont need to be inherently implemented from HttpServlet?

<form action="SServlet" method ="delete">
    Id: <input type="text" name="id"/>
    <input type="submit" value="DELETE"/>
</form>

You can't, only POST and GET as HTML form method

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form

<form action="SServlet" method ="post">
    <input type="hidden" name="action" value="delete"/>

    Id: <input type="text" name="id"/>
    <input type="submit" value="DELETE"/>
</form>

Either do POST (or GET), add a (hidden parameter) and at their handling do:

if ("delete".equals(request.getParameter("action"))) {
    ...
}

(Theoretically also the submit value might be used, but that is shown text, likely to be internationalized.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM