-
1. Re: displaying image from a database in jsp
tbfmicke Sep 4, 2002 7:17 PM (in response to zarni)In the src part of the tag, refer to a servlet that take a parameter telling what image you want.
The servlet should then get the image from the db and stream it to its output.
Regards. -
2. Re: displaying image from a database in jsp
zarni Sep 11, 2002 11:04 AM (in response to zarni)thanks for the reply,
i figured this would be the case,
>In the src part of the tag, refer to a servlet that take >a parameter telling what image you want.
>The servlet should then get the image from the db and >stream it to its output.
do you have such a servlet, or a simple example, i just don't won't to write it my self...:-)
please...
regards, zarni -
3. Re: displaying image from a database in jsp
coldbeans Sep 12, 2002 3:27 PM (in response to zarni)check out DB taglib from Coldtags suite
http://www.servletsuite.com/jsp.htm
You can do it right with JSP. -
4. Re: displaying image from a database in jsp
zarni Oct 9, 2002 12:30 PM (in response to zarni)I did it my self, becouse nobody helped me ...
:-)
here's my servlet for displaying images of any kind:
u just supply it a filename on the server...
package galaktimat.ekatalog.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ImgServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Get the absolute path of the image
ServletContext sc = getServletContext();
String filename = req.getParameter("fajl");
//String fajl = sc.getAttribute("itemImage");
//String filename = sc.getRealPath("fajl");
if (filename!=null) {
System.out.println("[ImgServlet] procesiram rekvest!");
// Get the MIME type of the image
/*String mimeType = sc.getMimeType(filename);
if (mimeType == null) {
sc.log("Could not get MIME type of "+ filename);
System.out.println("[ImgServlet] Could not get MIME type of "+filename);
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}*/
// Set content type
String extension = null;
extension = filename.substring(filename.lastIndexOf("."));
if ( extension!=null ){
resp.setContentType("image/" + extension);
// Set content size
File file = new File(filename);
resp.setContentLength((int)file.length());
// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = resp.getOutputStream();
// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
System.out.println("[ImgServlet] slika je bila izpisana!");
}}
}
}
to use it write this in your jsp:
<img src="/imgservlet?fajl=name-of-file" border=0 alt="picture" name="image">
of course after u set up your servlet mapping in web.xml:
<servlet-name>imgservlet</servlet-name> <servlet-class>galaktimat.ekatalog.servlet.ImgServlet</servlet-class>
<load-on-startup>2</load-on-startup>
<servlet-mapping>
<servlet-name>imgservlet</servlet-name>
<url-pattern>/imgservlet</url-pattern>
</servlet-mapping>
thanks for help anyway-...
zarni