Could you show me an example of using doPut() method?

Garth Somerville

[Short answer: no modern browser uses HTTP PUT for anything meaningful, so doPut() is never used, except for a custom protocol. If you want to upload a file, see How do I upload a file to my servlet? -Alex]

A simple example would be a source code repository application consisting of a Java Applet on the client and a Servlet on the server -- it would be entirely appropriate to upload new files into the system by having the applet send an HTTP PUT request, which would be processed by the servlet's doPut() method.

I am guessing there is more to this question. It is important to understand that in the HTTP 1.1 protocol, each request indicates a resource (the request path) and an action (the method) to be taken. In HTTP/1.1 the methods supported are OPTIONS, GET, POST, HEAD, PUT, DELETE, and TRACE. Each of these is clearly defined and differentiated in the HTTP/1.1 RFC. In particular, POST and PUT do not mean the same thing.

POST means to apply the content accompanying the request to the entity in the request URI, usually in the sense that the entity processes the content and returns some data as the result. Servlets and CGI scripts fit this description.

PUT means very simply that the accompanying content *is* the entity described by the request URI, and the server is being asked to store it as such. So the following conversation makes sense when PUT is used:

->GET /file.dat HTTP/1.1

<-HTTP/1.1 404 Not Found

->PUT /file.dat HTTP/1.1
Content-Length: 6
Content-Type: text/plain

Hello!

<-HTTP/1.1 200 OK

->GET /file.dat HTTP/1.1

<-HTTP/1.1 200 OK
Content-Length: 6
Content-Type: text/plain

Hello!

The above exchange makes no sense if POST is used instead of PUT.

However, when it comes to servlet programming, the distinctions among HTTP methods have been largely lost, in particular the difference between GET and POST. When it comes to servlets, it is essentially up to the author to define what his doPost() or doPut() means but it probably makes sense to keep in mind what these mean in HTTP and try to use them accordingly.

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact