J
jsguru72
Using some examples I found on the web, I put together the following
servlet to stream an image to a web page.
private void doGet( HttpServletRequest req, HttpServletResponse
res )
throws ServletException, IOException {
ServletContext sc = getServletContext();
String filename = "/public/web/testimage.jpg";
String mimeType = sc.getMimeType(filename);
if (mimeType == null) {
sc.log("Could not get MIME type of "+filename);
res.setStatus
(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
res.setContentType(mimeType);
File file = new File(filename);
res.setContentLength((int)file.length());
FileInputStream in = new FileInputStream(file);
OutputStream out = res.getOutputStream();
System.out.println("File Name: " + filename );
System.out.println("MIME Type: " + mimeType );
System.out.println("File Length: " + (int)file.length());
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
System.out.print(".");
out.write(buf, 0, count);
}
in.close();
out.close();
}
The following lines in my web.xml file are related to this servlet.
<servlet>
<servlet-name>Image Servlet</servlet-name>
<servlet-class>testApp.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Image Servlet</servlet-name>
<url-pattern>/image/*</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>jpg</extension>
<mime-type>image/jpeg</mime-type>
</mime-mapping>
If I use the URL of /testApp/image/1, it processes the servlet fine
and I get the image in my browser.
http://servername:8080/testApp/image/1
However, if I put "/testApp/image/1" as the src in an IMG tag inside
the jspx file, I get nothing.
<img src='/testApp/image/1' border='2' alt='my image'/>
I have the servlet setup to log some information in the catalina.out
log. Here is that output.
File Name: /public/web/testimage.jpg
MIME Type: image/jpeg
File Length: 42946
...........................................
File Name: /public/web/testimage.jpg
MIME Type: image/jpeg
File Length: 42946
...........................................
The first occurence is when I run this servlet directly from the URL.
The second is when I associate it with the IMG tag. Note they are
both identical which would indicate that the servlet is executing
fine, it just seems that the output is getting lost when I try to put
this in the IMG tag.
Any help would be appreciated.
Thanks.
servlet to stream an image to a web page.
private void doGet( HttpServletRequest req, HttpServletResponse
res )
throws ServletException, IOException {
ServletContext sc = getServletContext();
String filename = "/public/web/testimage.jpg";
String mimeType = sc.getMimeType(filename);
if (mimeType == null) {
sc.log("Could not get MIME type of "+filename);
res.setStatus
(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
res.setContentType(mimeType);
File file = new File(filename);
res.setContentLength((int)file.length());
FileInputStream in = new FileInputStream(file);
OutputStream out = res.getOutputStream();
System.out.println("File Name: " + filename );
System.out.println("MIME Type: " + mimeType );
System.out.println("File Length: " + (int)file.length());
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
System.out.print(".");
out.write(buf, 0, count);
}
in.close();
out.close();
}
The following lines in my web.xml file are related to this servlet.
<servlet>
<servlet-name>Image Servlet</servlet-name>
<servlet-class>testApp.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Image Servlet</servlet-name>
<url-pattern>/image/*</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>jpg</extension>
<mime-type>image/jpeg</mime-type>
</mime-mapping>
If I use the URL of /testApp/image/1, it processes the servlet fine
and I get the image in my browser.
http://servername:8080/testApp/image/1
However, if I put "/testApp/image/1" as the src in an IMG tag inside
the jspx file, I get nothing.
<img src='/testApp/image/1' border='2' alt='my image'/>
I have the servlet setup to log some information in the catalina.out
log. Here is that output.
File Name: /public/web/testimage.jpg
MIME Type: image/jpeg
File Length: 42946
...........................................
File Name: /public/web/testimage.jpg
MIME Type: image/jpeg
File Length: 42946
...........................................
The first occurence is when I run this servlet directly from the URL.
The second is when I associate it with the IMG tag. Note they are
both identical which would indicate that the servlet is executing
fine, it just seems that the output is getting lost when I try to put
this in the IMG tag.
Any help would be appreciated.
Thanks.