Here is a question which has been puzzling me for a while. In the admin
section of our webapp, the customer can upload images to be shown in pages
on the site. These images (so far all uploads are images) are stored as a
file on the server and retrieved for display by putting a servlet url in the
img tag. This works fine most of the time. However, on pages where there are
two such images, occasionally (once every 10-20 loads) both images are the
same. At first I thought that because the retrieval servlet is running
concurrently with itself, some data was being shared between threads and
therefore mixing up which image to retrieve (we had this problem before with
class variables in servlets). But try as I might I can't see anywhere that
this situation could occur. 
Another fact that is probably worth mentioning is that this happens only on
the deployment server which has two processors.... if anyone can be bothered
looking at the attached code and offering some suggestions I'd be grateful.

Redhat 7.1 (dual processors), Java 1.4.0-beta3, Tomcat 4.0.1, mm.mysql

ChrisC


//////////// in class GenericFileRetrievalServlet (extends HttpServlet)

public void service(HttpServletRequest request, HttpServletResponse response) throws 
IOException {

        if (request.getParameter("fileNo") == null) {
                
response.sendRedirect(Util.getContextPropertiesString("ContextSpecific-NoSuchDownloadPage"));
                return;
        }

        try {
                int fileNo = Integer.parseInt(Util.getCurrentParam("fileNo", request));
                GenericFileBean gfb = (GenericFileBean)Registry.getItem(new 
GenericFileBean(), fileNo);
                if (gfb == null) {
                        response.sendError(response.SC_NOT_FOUND, 
"GenericFileRetrieval failed for file id " + fileNo);
                        return;
                }
                FileInputStream fis = new FileInputStream(gfb.getDiskLocation());
                byte[] ba = new byte[fis.available()];
                fis.read(ba);

                response.setContentType(gfb.getContentType()); 
                response.setHeader("Content-Disposition","inline; 
filename="+gfb.getFilename()+";");
                ServletOutputStream out = response.getOutputStream();
                out.write(ba, 0, ba.length);
                out.flush();

        } catch (Exception e) {
                
response.sendRedirect(Util.getContextPropertiesString("ContextSpecific-NoSuchDownloadPage"));
        }
}


//////////// in class Registry

public static RegistryItem getItem (RegistryItem item, int id) throws 
RegistryAdminException {
        return getItem (item, "id", ""+id);
}
public static RegistryItem getItem (RegistryItem item, String column, String value) 
throws RegistryAdminException {        
        Connection con = Database.getConnection ();
        Statement statement = null;
        ResultSet rs = null;
        try {
            statement = con.createStatement();
            rs = statement.executeQuery (item.getQueryString(column, value));
            if ( rs.next () ) {
                item.setProperties (rs);
            } else {
                item = null;
            }
        } catch (SQLException e) {
            throw new RegistryAdminException (e.getMessage());
        } finally {
            Database.closeConnection(rs, statement, con);
        }
        return item;
}

//////////// in the JSP page

...
<img src="/ds/servlet/ds.servlets.GenericFileRetrievalServlet?fileNo=3&bogus=<%= 
System.currentTimeMillis() %>
<img src="/ds/servlet/ds.servlets.GenericFileRetrievalServlet?fileNo=5&bogus=<%= 
System.currentTimeMillis() %>
...





































--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to