0

I'm looking for any way to run PUT and DELETE methods for Java Servlets. In general, I have a simple application with web control (WAR) connected with EJB project that controls a simple list of libraries. For each of CRUD actions I would like to use different different method:

  • Creating by POST,
  • Reading by GET,
  • Update by PUT,
  • Delete by DELETE

However create I make by simple form with post method, read (list) is executed by simple entering GET in the URL address (without form), but I have no idea how could I use put and delete for their purposes.

Unfortunately online resources I found didn't answer that question, but I'm just curious how I could use id.

I assume removing by e.g. /Servlet/:libraryId (without any body), update by the same URL schema (with updated schema). doPut() and doDelete() should just run proper actions in the EJB.

OldShaterhan
  • 175
  • 1
  • 2
  • 12
  • I'm sure you know, but just incase, modern browsers are not able to send any requests to a server but GET or POST methods by a normal html form. See [Why a browser only supports GET and POST HTTP methods?](https://stackoverflow.com/q/33074877/42962) for more information. You could submit both PUT and DELETE statements using curl, wget, or maybe even Postman. – hooknc Nov 26 '19 at 21:12
  • Well I just read it earlier, but what I'm interested in it, because I work on some project as frontent (Angular) and there we can run if I'm not wrong HttpClient.delete(), which runs delete method and I just wonder if I can use it somehow (even assuming writing additional method on click that just will fire delete/put method with proper body). – OldShaterhan Nov 26 '19 at 21:41
  • @gurioso, yeah a bit changed version of him, I started using it this version years ago and it stays as pall of really small creativity ;) – OldShaterhan Nov 26 '19 at 22:34
  • @gurioso, sure I'll click it, if only my doubts will be fully clarified ;) – OldShaterhan Nov 26 '19 at 23:03

1 Answers1

1

There's no way on the HTML level to create a DELETE request in a form submit (it's always GET or POST, except you start to write your own browser that handles the kind of non standard HTML you proposed), all you can do is to submit a value (taken from a hidden field, radio button, whatever) that expresses which action should be taken in the doPost() or doGet() handler on the server.

But then there's nothing special about DELETE on the http protocol level. The whole request is just a sequence of bytes, where the first few define the method (PUT, GET, DELETE, PATCH...), followed by space etc.

As far as your server is concerned you could even use web sockets to accept connections and create responses to self defined fantasy methods. If you're using servlets, you have doPut(), doGet() and doDelete() available anyway. To handle other http methods you'd have to override the service() method.

On the client side Angular's HttpClient allows for all three methods that you need, you could use jquery's ajax, or there's again the web socket approach on the client too, as long as the browser plays along.

If it is a Java client, again there are web sockets (but you'd want to send some valid http request instead of "Hello", as done in the example¹.), HttpUrlConnection and the Apache HttpClient, at a first glance.

As for testing, there are browser extensions available that let you compose requests other than PUT and GET, SoapUi as a tool is very popular, IntelliJ even has a http client, that allows you to type the complete request as plain text.

¹ If you have a hand crafted server that agrees to such a "Hello" protocol, that's fine too. Its just not http.


Since there was quite some confusion in the question, as it turned out, I'd like to promote the idea some more to write your own Http client/server with webSockts. You have ready made examples for the socket stuff, so that's easy.

But it's more interesting then, that you're at the raw tcp/ip level that http is built on, all this framework-redirection (servlets, whatever) drops away and you're left with a single text basically (well, a byte stream), that's the http request. All the magic is gone, things are obvious and simple, just as specified.

Curiosa Globunznik
  • 3,129
  • 1
  • 16
  • 24
  • Well I know how to do that in Angular, maybe I wasn't clear, but I'm just trying to use similar solution but using Java Web Application, wondering if I can run from HTML or anywhere just that delete ;) About SoapUi - I can use Postman (which I use sometimes) for that purpose, but I'm still trying to find the way to execute DELETE on the same servlet on the java code level (even if I would use `doPost` from form, which will have hidden input field with action and then use something like HttpDelete and execution of the proper servlet method. – OldShaterhan Nov 26 '19 at 22:38
  • I'm really beginner in Java, I need to prepare program for my studies (expectation is for delete/update by post but I'm wondering: how to do it by put and delete + unfortunately - Netbeans) and that's partially curiosity of how to use something, etc. :D – OldShaterhan Nov 26 '19 at 22:42
  • Coming back to first comment: Is there any chance (I can't check it right now), that e.g. if there's no way to run DELETE from form (what's okay for me) to use form with POST action, that will just on `doPost()` check action given in hidden input and then fire HttpDelete() from `doPost()` on the same servlet? I know that's senseless and I could use just i.e. `doDelete()` from `doPost()` - but again - curiosity? – OldShaterhan Nov 26 '19 at 22:50
  • I will consider it, but unfortunately last days I'm not sure if I have enough time for something like this, however I was trying to touch some websockets few months ago for some project purposes (that was TypeScript). – OldShaterhan Nov 26 '19 at 22:52
  • What I meant was something like this (a bit simplified, but I think you'll get it): (html) https://pastebin.com/yCpghztU (Servlet.java - pseudocode) https://pastebin.com/jTBFT9Un <- and this way POST would fire DELETE? – OldShaterhan Nov 26 '19 at 23:01
  • At this moment I don't understand that scale, I'll read 'bout it when I'll get some free time (websockets I think would be a bit too large for such simply project, but I plan to develop myself in the Java technologies a bit) – OldShaterhan Nov 26 '19 at 23:09