On Thursday 01 July 2004 19:06, lee hwaying wrote: > hi. Hi,
first of all, let's get clear about the focus of JSP in the overall aspect of Java web technology. JSP is based upon the Servlet API and provides an easy means to generating HTML or other text output in the first degree. Though you *can* use JSPs to deliver binary contents (such as images), that's nothing recommendable. For streamed content, Servlets are much easier to handle and straightfoward. It has never been a good idea just to put <% and %> tags into a JSP and then code the whole program inside these. Then, you have to hold two things apart: the HTML page itself which is a text document and can well be rendered by using JSPs. Note that images are not 'embedded' in the page, but only *referred* to by <img src=...> tags. Delivering the real content is where Servlets come in. For delivering images stored in a database (don't know much about MySQL, as we're on Oracle, but in the end that doesn't matter much), I would write a simple Servlet that takes the byte array retrieved from the db (or a stream or whatever) and delivers it via HTTP. Note that HTTP is different from HTML.. Some sample code to illustrate this: package de.christianbollmeyer.struts.qna.servlets; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import de.christianbollmeyer.qna.Constants; import de.christianbollmeyer.qna.persistence.PersistenceFacade; import de.christianbollmeyer.util.ServletUtilities; public class ImageServlet extends HttpServlet { private static final String CONTENT_TYPE = "image/jpeg"; private static final int BUF_SIZE = 1024; public void init(ServletConfig config) throws ServletException { super.init(config); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String user = request.getParameter(Constants.SC_PARAM_ID); int userId = ServletUtilities.stringToInt(user); if (userId == Constants.UNDEFINED) { userId = Constants.POOL_USER; } response.setContentType(CONTENT_TYPE); response.addHeader("Cache-Control", "No-cache; No-store; Must-revalidate; Proxy-revalidate"); response.addDateHeader("Expires", 1L); response.addHeader("Pragma", "No-cache"); byte[] buffer = new byte[BUF_SIZE]; OutputStream outstream = response.getOutputStream(); InputStream instream = null; instream = PersistenceFacade.getInstance().getUserImage(userId); if (instream == null) { instream = PersistenceFacade.getInstance().getUserImage(Constants.POOL_USER); } try { int size = -1; while (-1 != (size = instream.read(buffer, 0, BUF_SIZE))) { outstream.write(buffer, 0, size); } } catch (Exception ex) { System.out.println(ex.getMessage()); } finally { if (instream != null) { try { instream.close(); } catch (IOException e) { ; } instream = null; } if (outstream != null) { try { outstream.close(); } catch (IOException e) { ; } outstream = null; } } } } Just to get the principles. It won't compile, for the supporting classes are missing. PersistenceFacade, for instance, reads a user image from the db and dumbly returns it as a stream for whatever use. The Servlet takes that and copies it bytewise down the HTTP stream (try..catch block), with the 'image/jpeg' MIME header being sent before (note: no HTML here at all). You can pass a parameter to the Servlet ('user') telling the Servlet which image to show. Then, there's some standard code to forbid caching, but that's it. The Servlet is mapped to a URL in web.xml, /img or whatever, and in the HTML page including the db pictures, the references simply look like <img src='/img?user=xyz' and the like. As this runs (in a somewhat more sophisticated version, but still) in a real high-volume Java application, (definitely not on christianbollmeyer.de :-), be sure it's just 'how you do it in Java' in terms of Best Practices. HTH, -- Chris (SCPJ2) > i have search on the net for 3 days for sample code of displaying a > byte[] from mysql image field using jsp but without success. > > 1. I have found sample code for php in doing that > http://www.onlamp.com/pub/a/onlamp/2002/05/09/webdb2.html?page=1 > > 2. It can be done via servlet. But i am reluctant to emit all html > content using servlet in this case. > > > Can anyone help? Is there a way using jsp to do so? > > many many thanks > hy > > _________________________________________________________________ > Download ringtones, logos and picture messages from MSN Malaysia > http://www.msn.com.my/mobile/ringtones/default.asp > > ===================================================================== >====== To unsubscribe: mailto [EMAIL PROTECTED] with body: > "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with > body: "set JSP-INTEREST DIGEST". > > Some relevant archives, FAQs and Forums on JSPs can be found at: > > http://java.sun.com/products/jsp > http://archives.java.sun.com/jsp-interest.html > http://forums.java.sun.com > http://www.jspinsider.com =========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant archives, FAQs and Forums on JSPs can be found at: http://java.sun.com/products/jsp http://archives.java.sun.com/jsp-interest.html http://forums.java.sun.com http://www.jspinsider.com