Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can use JSP to deliver a non-text file?  RSS feed

 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can anyone tell me if it's possible to deliver a non-text file (in this case, a jar file) in a JSP?

Any example code would be much appreciated.

(The reason I ask is because I need some sort of app that can take the 'User-Agent' field from the request, calculate which jar file he needs, and then return it directly.)

Thanks,
James
[ August 12, 2004: Message edited by: Bear Bibeault ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JSP API is designed to send character data. It is supposedly possible by means of sneaky tricks to make a JSP send binary data, but in my opinion that is a really dumb thing to try.
You should use a servlet to send binary data such as a jar file - much cleaner and easier to maintain. I really don't know why some people are so afraid of servlets, they can be quite simple.
Bill
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot simpler than JSP in my experience! But then I suppose that's a server-side programmer's view... :roll:

Jules
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers guys,

So in a servlet, do you know how I would get the HttpServletResponse to deliver the jar or jad file (I'm delivering a j2me midlet) rather than text.

I understand the content-type should be set to 'application/java-archive' for a jar or 'text/vnd.sun.j2me.app-descriptor' for a jad, but how do I get the actual jad/jar to be outputted?

Many thanks,
James
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe it's as simple as just writing the bytes to response's output stream. The browser will then do what it thinks is appropriate for the content type.

Try it.

Jules
[ August 12, 2004: Message edited by: Julian Kennedy ]
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so I think I should be using response.getOutputStream(), but how can I then write my jar file to the output stream? (Sorry if this is an obvious question, but this is new to me.)

Thanks,
James
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming you're familiar with file IO in Java. Check out ByteArrayInputStream. You'll probably want to wrap it in a BufferedInputStream. You can write what you read to the response using:

There's a version of write() that lets you write a buffer's worth if you want to minimise the memory footprint.

Hope that helps.

Jules

P.S. If you're not familiar with file IO then it's time to RTM!
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers. Will hopefully get there from there. Go on, what's RTM ?!
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RTM

Jules
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, Cheers! Learn something everyday...

For anyone that's interested (and considering I've spent all day finding the answer), here's the code that does the trick:



Gorgeous!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!