Tell me how the servlet determines what exactly the DoDelete request came in, if only GET / POST methods can be specified on the form? For example, if I filled in the form that I needed to delete and clicked on the button, the request went to the server and reached the servlet, and now how to understand the servlet what exactly the DoDelete method should be called and not the doPost?
- No If a non-DELETE request arrives, doDelete will not be called. - Sergey Gornostaev
- You won’t believe it, but the server receives your HTTP request and processes it (including determining what kind of request it is: GET, POST, DELETE, etc.) and calls the appropriate method in the servlet. - not a Programmer
- No need to smack. An example is possible? The method attribute accepts only get or post. - Vardan Matevosyan
- And who prevents to make a post request to the address "/ someurl / object / delete"? - Riĥard Brugekĥaim
- @Ri a sense then in the doDelete method, if I can make a separate servlet call it delete and override the doPost method there? - Vardan Matevosyan
2 answers
Gentlemen, that's the thing, it turns out that the form tag sends only GET and POST values to the html tag, it would seem that there are other methods of the HTTP protocol, and so it turns out there is an opportunity to send all other methods like DELETE, PUT, HEAD, and so on by sending them via javascript. Like this. And a lot of information on the Internet only does what confuses. Thank you all for discussing this topic.
The HTTP specification defines the structure of HTTP requests and responses. According to it, the HTTP request has the following structure:
- The request line (contains method, URL, protocol version);
- The request headers;
- An empty line (indicates that the request headers have ended);
- Message body (optional).
Example HTTP GET request:
GET https://www.w3.org http / 1.1
Host: www.w3.org
Accept: text / html, application / xhtml + xml, application / xml; q = 0.9, image / webp, image / apng, / ; q = 0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: ru, en-US; q = 0.9, en; q = 0.8, uk; q = 0.7
Cache-Control: max-age = 0
Connection: keep-alive
User-Agent: Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 65.0.3325.181 Safari / 537.36
The first line contains the name of the method (GET, POST, DELETE, HEAD, PUT, ...). This is how a web server (yes, yes, not a servlet, but a web server) determines which request came from the user. Next, it converts the HTTP request to the HttpServletRequest, forms the HttpServletResponse, and passes the parameters to the appropriate servlet. A suitable servlet server finds the URL-y, which you defined in the servlet-mapping -> url-pattern in the deployment descriptor web.xml .
In the book Head First Servlets and JSP, this is all described in detail.
- Very interesting, but it is the service () method that determines which method to call the servlet, according to the method in the request line. That is exactly what is written in the book Head First Servlets and JSP. And what server to determine? What request, yes in the request line from the user, and so everything determines which method, it forms two objects based on this data and passes on. And this is not the answer to my question. I myself wrote the answer above. - Vardan Matevosyan