Dynamic img src in a JSP

Hey all
Nevermind!
cheers
Message was edited by:
syncro

SeniorProject, don't cross post the same question in multiple forums.
AnanSmriti: No, that is incorrect. That code is for including jsp pages - NOT images.
http://forum.java.sun.com/thread.jspa?threadID=721993&messageID=4165741#4165741

Similar Messages

  • I want to display BLOB image in JSP Using  html tags IMG src=

    GoodAfternoon Sir/Madom
    I Have got the image from oracle database but want to display BLOB image using <IMG src="" > Html tags in JSP page . If it is possible than please give some ideas or
    Send me sample codes for display image.
    This code is ok and working no problem here Please send me code How to display using html tag from oracle in JSP page.
    <%@ page import="java.sql.*" %>
    <%@ page import="java.io.*" %>
    <%@ page import="javax.swing.ImageIcon;" %>
          <%
            out.print("hiiiiiii") ;
                // declare a connection by using Connection interface
                Connection connection = null;
                /* Create string of connection url within specified format with machine
                   name, port number and database name. Here machine name id localhost
                   and database name is student. */
                String connectionURL = "jdbc:oracle:thin:@localhost:1521:orcl";
                /*declare a resultSet that works as a table resulted by execute a specified
                   sql query. */
                ResultSet rs = null;
                // Declare statement.
                PreparedStatement psmnt = null;
                  // declare InputStream object to store binary stream of given image.
                   InputStream sImage;
                try {
                    // Load JDBC driver "com.mysql.jdbc.Driver"
                    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
                        /* Create a connection by using getConnection() method that takes
                        parameters of string type connection url, user name and password to
                        connect to database. */
                    connection = DriverManager.getConnection(connectionURL, "scott", "root");
                        /* prepareStatement() is used for create statement object that is
                    used for sending sql statements to the specified database. */
                    psmnt = connection.prepareStatement("SELECT image FROM img WHERE id = ?");
                    psmnt.setString(1, "10");
                    rs = psmnt.executeQuery();
                    if(rs.next()) {
                          byte[] bytearray = new byte[1048576];
                          int size=0;
                          sImage = rs.getBinaryStream(1);
                        //response.reset();
                          response.setContentType("image/jpeg");
                          while((size=sImage.read(bytearray))!= -1 ){
                response.getOutputStream().write(bytearray,0,size);
                catch(Exception ex){
                        out.println("error :"+ex);
               finally {
                    // close all the connections.
                    rs.close();
                    psmnt.close();
                    connection.close();
         %>
         Thanks

    I have done exactly that in one of my applications.
    I have extracted the image from the database as a byte array, and displayed it using a servlet.
    Here is the method in the servlet which does the displaying:
    (since I'm writing one byte at a time, it's probably not terribly efficient but it works)
         private void sendImage(byte[] bytes, HttpServletRequest request, HttpServletResponse response) throws IOException {
              ServletOutputStream sout = response.getOutputStream();
              for(int n = 0; n < bytes.length; n++) {
                   sout.write(bytes[n]);
              sout.flush();
              sout.close();
         }Then in my JSP, I use this:
    <img src="/path-to-servlet/image.jpg"/>
    The name of the image to display is in the URL as well as the path to the servlet. The servlet will therefore need to extract the image name from the url and call the database.

  • What syntax should be used in jspx for img src= c:out value="${}" /

    Dear Friend If I query in a database and the database return a url and with this url  I like to generate an image in jspx.
    *The syntax work ok with the jsp <img src=<c:out value="${abc.xyz}" />*
    *But with jspx it shows syntax error of < What you people recommend for it.*

    I stored all pix in my Tomcat web server where the url of them is as
    http://localhost:8080/pix/anna.jpg etc.
    and I stored the same url in the database table.
    <?xml version="1.0"?>
    <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
    xmlns:c="http://java.sun.com/jstl/core"
    xmlns:sql="http://java.sun.com/jstl/sql"
    version="1.2">
    <jsp:directive.page contentType="text/html"/>
    <jsp:directive.page import="java.util."/>
    <jsp:directive.page import="java.text."/>
    <html>
    <head>
    <title>Snap Diary</title>
    </head>
    <body bgcolor="white">
    <sql:query var="img">
    SELECT snap FROM snap_pix
    WHERE name =?
    <sql:param value="${param.name}" />
    </sql:query>
    <img src="${img.snap}" />
    <p>
    <form action="recentsnap.jsp" method="post" >
    <input type="submit" value=" Recent Snap " />
    </form>
    <form action="oldsnap.jsp" method="post" >
    <input type="submit" value="Old Snap" />
    </form>
    </p>
    </body>
    </html>
    </jsp:root>
    the right click on image show this url http://localhost:8080/Snap_Diary/${img.snap}
    where the actual folder in which I stored snap is http://localhost:8080/pix/ and the url which I stored in the database
    is also as http://localhost:8080/pix/anna.jpg etc...
    Query should return the value as http://localhost:8080/pix/anna.jpg not as http://localhost:8080/Snap_Diary/${img.snap}.

  • Dynamic iframe src

    Is there a way to dynamically load the src attribute of an iframe component with a jsf session value? I have a jsf page with an iframe which points to another web site. This website can be different dependand on a session variable. I am currently using an outputlink to target the iframe but would like to eliminate the need to press the link . Here is my code below:
    <f:view>
    <html lang="en-US" xml:lang="en-US">
    <head>
    <meta content="no-cache" http-equiv="Cache-Control"/>
    <meta content="no-cache" http-equiv="Pragma"/>
    <title>web Title</title>
    <link href="resources/stylesheet.css" rel="stylesheet" type="text/css"/>
    </head>
    <body style="-rave-layout: grid">
    <div style="left: 0px; top: 0px; position: absolute">
    <jsp:directive.include file="header.jspf"/>
    </div>
    <h:form binding="#{web.form1}" id="form1">
    <h:outputLink binding="#{web.hyperlink1}" id="hyperlink1" style="left: 20px; top: 152px; position: absolute" target="t1" value="#{SessionBean1.webPath}">
    <h:outputText binding="#{web.hyperlink1Text}" id="hyperlink1Text" value="Press here to view images"/>
    </h:outputLink>
    <div style="height: 600px; left: 20px; top: 192px; position: absolute; width: 754px">
    <iframe frameborder="1" height="600px" marginheight="0" marginwidth="0" name="t1" scrolling="yes" src="" escape="false"/>' width="750px"></iframe>
    </div>
    </h:form>
    </body>
    </html>
    </f:view>
    </jsp:root>
    What I would like is to populate the src of the iframe with "#{SessionBean1.webPath}" . Am I missing something extremely obvious?
    Any help would be greatly appreciated.

    Yes it's possible to load dynamically the src attribute. In fact the solution is almost simple.
    The problem is that iframe is a pure html component so it's impossible to bind src with backing bean's attribute.
    To resolve this pb you can use an outputText attribute which is binded with backing bean's attribute. This attribute could be filled with a string such as <iframe id="MyFrame" src="http://xxxxxx"></iframe>. On the other hand you have to unselect escape option of outputText to be sure that <iframe> string will be translated as a html string.
    The frame will be generated at runtime.

  • IMG SRC="???" I've read old threads...

    Hello,
    I've spent 2 days pulling my hair out trying to get a servlet to present an image in html the same old boring easy way you would do it in a web page. I have searched this forum and read many threads, trying what each of them suggested, and uniformly failing to get anything going. In a last ditch effort, I am going to bug you folks with what is obviously an old problem. I certainly appreciate any help. By the way, I am a complete newbie to web programming, just in case that wasn't obvious ;)
    Directory Structure
    _________ROOT___________
    | | |
    WEB-INF PotteryPics index.html
    | |
    | _________
    | | |
    | Pottery Utility
    |
    | | |
    classes lib web.xml
    | |
    | catalina-root.jar
    |
    org
    |
    apache
    |
    jsp
    In total desperation I tried the following code just to see if it would work on the server machine. It did indeed produce a perfect looking page, but, of course, it failed when accessed from the web:
    "<IMG SRC=\"C:/jakarta-tomcat-5.0.24/webapps/ROOT/PotteryPics/Pottery/Picture.jpg\">"
    This also worked:
    "<IMG SRC=\"C:\\jakarta-tomcat-5.0.24\\webapps\\ROOT\\PotteryPics\\Pottery\\ Picture.jpg\">"
    This, however, and MANY other attempts similar to it uniformly fail:
    "<IMG SRC=\"../PotteryPics/Pottery/Picture.jpg\">"
    I'll refrain from saying anything more for now...no sense in repeating everything I tried with moving directories etc.
    Thanks for you time,
    Keith Myers

    Non-mangled version ( I hope )
                                                 _________ROOT___________
                                                |          |             |
                                            WEB-INF    PotteryPics    index.html
                                                |          |
                                                |       _________
                                                |      |         |
                                                |   Pottery    Utility
                                                |
                                       |        |          |
                                   classes     lib      web.xml
                                       |        |
                                       |    catalina-root.jar
                                       |
                                      org
                                       |
                                    apache
                                       |
                                      jsp

  • Help needed with IMG SRC

    Can anyone please help me with this: I have created my own website on IWeb and now I want to use some of my banners on other websites. Problem is: there is something wrong when I fill in the img src code. The banner does not appear, only a question mark. I do not know the answer to that: am I using the wrong URL code or is it something else? I have placed my banners in the public folder, but that makes no difference. Thank you in advance for your help.

    Welcome to the discussions. Assuming your image is in the top level of your iDisk's Public folder, the HTML should look similar to this:
    <IMG SRC="http://idisk.me.com/membername-Public/Image.jpg"/>
    ...If the above doesn't help, post back here with the HTML you're using. You can convert it to character entities for display in this forum via _this site_.

  • Local "img src" in JComboBox

    I've got a JComboBox where one of the elements are added with this text:
    "<html><body style=\"padding:0px;margins:0px;\"><table style=\"padding:0px;margins:0px;\"><tr style=\"padding:0px;margins:0px;\"><td style=\"width:20px;height:20px;vertical-align:middle;padding:0px;margins:0px;\"><img src=\"resources/image.png\"></td><td style=\"height:20px;vertical-align:middle;padding:0px;margins:0px;\"><b> MANAGER</b></td></tr></table></body></html>"How do I get this to work for local folders? I've tried absolute paths, I've tried relative paths (like above), but I just can't seem to get it to work. Internet URLs do work though, but they seem to make the JComboBox lag.
    Thanks in advance!

    Someone has a solution in this post
    http://forum.java.sun.com/thread.jspa?forumID=257&threadID=290240

  • Using multiple img src / with htmlText --display problem

    I need help.
    I am using multiple <img src /> tag with htmlText and
    pictures displays in layers(overlay)?
    html_txt.html = true;
    html_txt.htmlText = "text<br><img src = '
    http://picture1.jpg 'width='500'
    height='491' hspace='0' /><br>more text<br><img
    src = '
    http://picture2.jpg' width='299'
    height='612' hspace='101' ><br>end with text";
    Is there any way to refresh text field when pictures are
    fully loaded so it will display like regular html page?
    this is flash version, evrything is shown but it is not
    right.
    http://www.sosui.jp/flash/test/v001/pages/home/homeBlogV002.swf
    I want to show them like this page.
    http://sosui.jp/flash/test/v001/pages/home/blog.html
    I tried onEnterFrame but pictures will not show. I think it
    is because it is trying to load pictures everytime. So I cannot use
    onEnterFrame, i need other way to refresh text field.
    Please help me. you might of guessed I am not an English
    speaker, so my English might be little off.
    Thank you.

    does anybody know anything? how long do i need to wait to see
    if someone replies? I just need to know if it is possible or not.

  • HTML tags img src="Pic.gif" alt="blah..." a href="file" title="blah..."

    Has anyone managed to have the "alt" tag on an image <img src="abc.gif" alt="my image descriptor"> or the title tag (such as in ) work in the JavaHelp. If so, how did you accomplish the task. I have exhausted every avenue and have not been able to get those tags to work. They display fine in all browsers (IE, Moz, Firefox and Netscape), I am baffled.
    Furthermore, I have still to be able to get any of the org.netbeans.modules.javahelp.* to work of the JavaHelp. Checked all params and values, they are correct, but all i get in the JavaHelp are the cursed ??. Is their a file or library that needs to be included at the top of the HTML file?
    This question is also posted in JavaHelp Blogs
    Thanks
    Pablo

    I have posted similar items in every blog i could think of in order to fetch an answer. Unfortunatelly, my belief is that JavaHelp is unable to support the alt and title tags within html code.
    TY Balus
    ps I wish I could award more stars for an answer to this dilema...
    Edited by: pablodiaz on Dec 4, 2008 9:21 PM

  • Dreamweaver CSS Mac writes image links wrong... img src="/www/images/asian_sales_office.jpg"

    Dreamweaver CSS Mac writes image links wrong... <img src="/www/images/asian_sales_office.jpg" width="x" height="x" alt="x"> Took me a bit to find this, now back to hand coding all image links and also checking other code. Any fixes?

    That looks to me like you may have your site definition set up incorrectly and you appear to be using "Site Root Relative" vs "Document Relative"
    Go to Site > Manage Sites > click the  site you are working on > Click the Edit icon > Advanced Settings > Links relative to: should be set as Document, unless you have a very specific reason for choosing Site Root there
    If your server root folder is called www, you'll need to remove that folder from your site root. You are only transferring files from root to root, so the name of your local root folder is not important (the files inside it go to the server root). It is important not to accidentally have a duplicate of the server root folder inside your local root though which would accidentally nest your files within another folder when transferring to the server.

  • Regular Expresson to Extrcat URLs from img src tags

    Hi,
    I have a huge string containing html tags, some of these tags being <img src="URL"> ones. I need to extract the urls from all the occurences of these tags in the input string. This is what I am doing:
    Pattern p=null;
    Matcher m= null;
    String word0= null;
    String word1= null;
    p= Pattern.compile(".*<img[^>]*src=\"([^\"]*)",Pattern.CASE_INSENSITIVE);
    m= p.matcher(txt);
    while (m.find())
        word0=m.group(1);
        System.out.println(word0.toString());
         }The problem with this code is that this prints only the last URL. For example if there are 5 <img src="URL"> tags, this code prints only the URL contained withn the 5th< img src> tag. Please tell me how to solve this.
    Thanking you in advance

    Here's another approach:
    ok so this is assuming that the source text is not one line of strings, but mulitple lines.
    String[] lines = txt.split("\n")this splits the string based on the newline character. then you can iterate through the array and use the pattern on each string. The only thing that can go wrong here is if there are two images in one line of code. Then you can use some of the methods in the String class to find each instance of "img src=", and you can figure out the rest.
    another, better, way to split the string is with a string tokenizer (simple method in my library):
    * Returns an array of strings from splitting the givin string
    * with the given deliminator.  See the example<br/>
    * <br/>
    * <i>Example:</i><br/>
    * <code>splitTokens("ID: 1920129 NAME: JOHNY IAMYOURFATHER: IDONOTWANT", ": ")</code> would return:<br/>
    * <code>["ID",190129","NAME","JOHNY","IAMYOURFATHER","IDONOTWANT"]</code>
    * @param base
    *               the string to be split
    * @param delim
    *               the deliminator that is used to split the string.  If the
    *               deliminator is <code>", "</code>, then the string will be
    *               split on any combination of commas and spaces, along with
    *               individual commas and spaces.
    * @return an array of strings split from the base string by the deliminator
    public static String[] splitTokens(String base, String delim) {
         StringTokenizer toker = new StringTokenizer(base, delim);
         String pieces[] = new String[toker.countTokens()];
         int index = 0;
         while (toker.hasMoreTokens()) {
              pieces[index++] = toker.nextToken();
         return pieces;
    }{code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Trying to dynamic include HTML into a JSP

    All,
    I am trying to include an HMTL file into a JSP file. I wish to do this dymanically. So, the following is not an option:
    <%@ include file="relativeURL" %>
    So, I am forced to resort to such measures as the following:
    <jsp:include page="<%=myPath%>"/>
    However, I get the following error:
    java.lang.IllegalStateException: Response has already been committed
         at java.lang.Throwable.fillInStackTrace(Native Method)
         at java.lang.Throwable.fillInStackTrace(Compiled Code)....
    I tried it like this:
    <jsp:include page="<%=myPath%>" flush="true"/>
    I encoutered the same result. I have JSP 1.2 running Tomcat 3.1 with Apache 1.3.14, all on SunOS. Thanks in advance for any help.
    Nicholas

    One of the worst error messages you can get is the IllegalStateException. It simply means that an attempt has been made to forward or include but only a portion of the include was sent. In a situation like this the best way to handle the dynamic page content is to include it from a Servlet by sending the information you need through beans. Including the Servlet rather than the dynamic path would clear those errors right up. One thing to keep in consideration is that you can't just get the page and display it, you have to buffer it in so the state can be kept alive without timing out like it does with the dynamic include.
    Hope that helps. Been there, done that, I know how frustrating the error was.

  • Dynamic text box generation in jsp page by using net-ui jsp html tags

    Hi All,
    I Need to generate the "TextBoxes" Dynamically by using the "Net-UI HTML Tags".How can i do this ?
    I am using
    1. WebLogic portal server 8.1 sp4 (Workshop dev environment)
    2. JavaPageFlow Applications
    3. Net-UI tags html jsp
    I need to generate the textboxes dynamically and i have to
    get those values in the page flow .
    How it is possible by using the "Net-UI Tags".
    Thanks In Advance,
    KiranBabu.Karusala

    how we create dynemic text box in jsp based one value which we give at runtime

  • Is anyone know how to create dynamic pdf file from a JSP page or Servlet ?

    Hi
    Is any one know how to create a dynamic PDF file from JSP or servlet. I need to have standard form format needs to be created and want to send the info to that form dynamically.
    Is there any libraries available?
    Thanks

    I posted a link in this thread to a lib that lets you create PDFs from an app. They have a Java version. I have not used it but I know someone who used it with Microsoft ASP and they liked it.
    http://forum.java.sun.com/thread.jsp?forum=31&thread=287285
    -S-

  • How to prevent 404 error  in Img Src attribute

    Hi everyone ,
    I'm searching something like "spry:content" to be used with
    image source attribute . Why i need this ?
    When a spry page is first loaded , browsers try to get image
    http://your_server/{image_url}".but
    there is no such file and i got lots of 404 errors in w3svc logs .
    Here is a simple example:
    <div id="regionSolSerit" spry:region="dsSolSerit"
    class="SpryHiddenRegion" >
    <img alt="" style="display:none;border:0" width="165px;"
    height="100px" src="{image_url}"
    </div>
    Waiting for solutions!

    I ran into this issue myself. Here is a solution:
    1) Give the image a unique id.
    2) Leave the "src" blank... // this will keep you from
    getting a 404 error.
    3) In your javascript, write an observer like this:
    Spry.Data.Region.addObserver("regionSolSerit", {
    onPostUpdate: function(){ // set observer for when the thumbnail
    achors get loaded
    var rows = dsSolSerit.getCurrentRow(); // get the data
    var daImage = document.getElementById('yourimageId');
    var daImage.src = rows['image_url'];
    If you want to do something like multiple images like a
    thumbnail gallery, you would put a for loop in the observer and
    just cylce through the urls and place them.
    Hope this helps...

Maybe you are looking for