• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about sending a jar file to browser

 
Yuan Ye
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks in advance.
 
Siddhartha Bhattacharya
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getInputStream() which returns a ServletInputStream and getOutputStream() which returns ServletOutputStream can be used to work with binary data for request/response respectively.
so u can read binary data with GET
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That example shows what the HttpServlet doGet method does after it gets the text request from the browser. You're confusing the http get method and what it can do and the HttpServlet doGet method and what it can do. The http get method has to send text. The doGet method in the servlet can do anything it wants, including writing out the jar to the response.
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Yuan Ye
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for replying. Now I am quite clearer about the issure. Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic