Monday, December 01, 2003

The latest JSP fun

So, this web app that I'm writing needs to pull images out of the database. I was fretting about how to get them out to the browser. Turns out, it's been less than 2 hours of research and coding.

SQL Server's IMAGE type comes out as a byte[], which is perfect, and I can dump that into a model object with no problems. I then ship that over the wire with my web service, and on the web-server tier, I have a simple JSP that generates no HTML at all. My test page looks like this (to fetch photo ID #5):




<%@ page import="org.cpnonline.ws.*,
       java.util.List,
        electric.registry.Registry,
        java.util.Properties,
        java.io.OutputStream" %>

<%
        String baseUrl = "http://www.somesite.org/ws/services/;
        String photoFactoryURL = baseUrl + "photo.wsdl";
        IPhotoFactory photoFactory = null;
        PhysicianPhoto photo = null;

        photoFactory = (IPhotoFactory)Registry.bind( photoFactoryURL, IPhotoFactory.class );

        photo = photoFactory.getPhotoID(new Integer(5));
        // getPhoto() returns byte[].
        response.setContentType("image/jpg");
        response.setContentLength(photo.getPhoto().length);

        OutputStream output = response.getOutputStream();
        output.write(photo.getPhoto());
%>


And that's the whole page. I was kind of surprised to discover that it was that easy, but hey, sometimes Java surprises! Now to add some caching for this, so that we don't have to hit the web service every time...

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home