Originally posted by Yuan Ye:
Hi, I was trying to learn an example from Deshmukh's book on page 41. The example use a doGet() method to send a JAR file back to the browser. To my understanding, doGet() method can only send text file, not binary, am I correct? And what's more, since a href can only use GET method, does that mean we normally can't use a href to upload or download a binary file since it is only supported by POST method.
There appear to be two issues that
I think you are confusing or at least mixing together.
First, (as Siddhartha pointed out) a servlet may return
any content type from a GET request (or POST request, for that matter). Simply, use the setContentType method to set the MIME type of the content being returned. If that content type happens to be binary data, then use the getOutputStream method on the response object to get access to a binary stream.
Second, you also mention
uploading files. You cannot use a GET request (such as an A-HREF tag) to perform file upload; period, end of story. You must use a FORM tag in conjunction with an INPUT tag of type FILE inside of the FORM tags. The method attribute of the FORM tag must be POST and the enctype attribute must be 'multipart/form-data'. You will also need additional support on the server side to process a file upload. Check out the
Jakarta Commons FileUpload project for more details.
Cheers,
Bryan