Ok here is the detail. 1. Yes the name of the servlet is ImageRetriever. The servlet fetches an Image from the database where it is stored as a blob. 2. I have tested the servlet by calling it directly from the browser using the URL http://localhost:8084/GEFT/ImageRetriever?imageId=1. This produces the required image in the browser. 2. Yes i am using the port 8084 for my TomCat server, which is integrated with the netBeans development environment i am using. 4. I have also tested calling this servlet from a .jsp page placed in the root (same directory as the applet) and it successfully connects with the servlet and receives the image data. 5. I port the same code (the one i tested in jsp page) to the applet making only minimal changes like using getCodeBase() to fetch the host name and port and suddenly it connects no more.
Here is a little more detail of the code i am using.
protected Image getImage(String imageId) throws Exception {
URL imageURL = new URL(getCodeBase(), "/GEFT/ImageRetriever");
URLConnection connection = imageURL.openConnection();
// Make sure browser doesn't cache this URL.
connection.setUseCaches(false);
// Tell browser to allow me to send data to server.
connection.setDoOutput(true);
// Grows if necessary
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512);
// Stream that writes into buffer
PrintWriter outstr = new PrintWriter(byteStream, true);
String postData = URLEncoder.encode("imageId=") + URLEncoder.encode(imageId);
// Write POST data into local buffer
outstr.print(postData);
// Flush since above used print, not println
outstr.flush();
// POST requests are required to have Content-Length
String lengthString = String.valueOf(byteStream.size());
connection.setRequestProperty("Content-Length", lengthString);
// Netscape sets the Content-Type to multipart/form-data by default.
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// Write POST data to real output stream
byteStream.writeTo(connection.getOutputStream());
connection.connect();
[b]InputStream input = connection.getInputStream();[/b]
ByteArrayOutputStream output = new ByteArrayOutputStream();
// set read buffer size
byte[] rb = newbyte[1024];
int ch = 0;
// process the data
while ((ch=input.read(rb)) != -1) {
output.write(rb, 0, ch);
}
// transfer to byte buffer
byte[] b = output.toByteArray();
input.close();
output.close();
// load final buffer to image Icon
ImageIcon imgIcon = new ImageIcon(b);
return imgIcon.getImage();
}
The Exception comes in the line
InputStream input = connection.getInputStream();
.
Here is the Exception as it shows in the java console java.io.FileNotFoundException: http://localhost:8084/GEFT/ImageRetriever at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at GEFTMain.getImage(GEFTMain.java:103) at GEFTMain.init(GEFTMain.java:46) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
Initially i thought it had something to do with the placement of the applet. However i have tried placing it in several locations but the error remains the same. I also tried deploying my application to a different server (Resin) instead of the Tomcat that i am using currently. I am using jdk1.4.2