The following are my configurations -
Apache JServ 1.05b
Apache Web Server 1.3.6
OS Windows NT Workstation 4.0 SP4
I am trying to send a static html file from within a servlet. For test
purpose I have written a servlet which sends a static file back to the
client. The problem I am having is that only the text goes to the
client and the gifs don't.
If I invoke my html file as follows -
http://myhost:port/myfile.html
I see both text and gifs.
If I involke as -
http://myhost:port/servlets/ViewFile/myfile.html
I see only the text and not the gifs. (I have tried to put a copy of
the gif files both at htdocs dir where the html file is and at servlets
dir too.)
My ViewFile.java is as follows -
-------------------------------------------
public class ViewFile extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// Use a ServletOutputStream since we may pass binary information
ServletOutputStream out = res.getOutputStream();
// Get the file to view
String file = req.getPathTranslated();
// No file, nothing to view
if (file == null) {
out.println("No file to view");
}
// Get and set the type of the file
String contentType = getServletContext().getMimeType(file);
res.setContentType(contentType);
// res.setContentType("image/gif");
// Return the file
try {
returnFile(file,out);
}
catch (FileNotFoundException e) {
out.println("File not found");
}
catch (IOException e) {
out.println("Problem sending file: " + e.getMessage());
}
out.close();
}
private void returnFile(String filename, OutputStream out) throws
FileNotFoundException, IOException
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(filename);
byte[] buf = new byte[4*1024]; //4K buffer
int byteRead;
while((byteRead = fis.read(buf) ) != -1)
{
out.write(buf, 0, byteRead);
}
}
finally
{
if (fis!= null) fis.close();
}
}
}
-------------------------------------
getServletContext().getMimeType(file) returns text/html. I tried to
hard code image/gif, but that did not work.
Any clue how I can send both text and image back to the client from a
servlet ?
Thanks in advance
-Depankar
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
--
--------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
READ THE FAQ!!!! <http://java.apache.org/faq/>
Archives and Other: <http://java.apache.org/main/mail.html/>
Problems?: [EMAIL PROTECTED]