Including External Image in JSP

Hi all,
I need to be able to load a external image (image in a specific loaction on the server but not in the webapp location). Unfortunealty I cannot use servlets and have to code it in the JSP directly.
So far I have this code :
<%@ page import="java.io.*"%>
<%@ page import="javax.imageio.stream.*"%>
<%@ page import="br.gov.sc.tce.sco.view.framework.Constants"%>
<%
     response.setContentType("image/gif");
     String  fileName = Constants.UPLOAD_FOTO_HOME + ( request.getParameter("fileName") == null ? "" : request.getParameter("fileName") );
     FileInputStream fis = null;
     OutputStream os = null;
     try {
          fis = new FileInputStream(fileName);
          os = response.getOutputStream();
          while ( fis.available() > 0  ) {
               os.write(fis.read());
     } catch (Exception e) {
          e.printStackTrace();
     } finally {
          if (fis != null) {
               try {fis.close();} catch (IOException e1) {} finally {fis = null;}
          if (os != null) {
               try {os.flush();os.close();} catch (IOException e1) {} finally {os = null;}
%>and in the pages that need to include pages I do something like the following :
<img src="<%=appName%>/include/jsp/showImage.jsp?fileName=<%=((Foto)fotoCad).getNomeArquivo()%>" width="60" height="60">This all seems to work ok , the images display , except that I get a stacktrace error afterwards of java.lang.IllegalStateException: getOutputStream() has already been called for this response
Any help would be appreciated
Regards
Robin

If you want to get rid of the exception, you have to remove all carriage returns between your JSP tags
Explanation:
If you put a carriage return in your code, the generated servlet generates: out.println("\r\n") (depending on your os)
In other words it has already got the output stream/writer in order to print out some characters to the stream. Hence the generated error message.
The following should generate without any exception raised.
See how carriage returns are eliminated?
Note this also includes no carriage return at the END of the file after the closing %>
<%@ page import="java.io.*"
%><%@ page import="javax.imageio.stream.*"
%><%@ page import="br.gov.sc.tce.sco.view.framework.Constants"
%><%@ page contentType="image/gif" %><%
     String  fileName = Constants.UPLOAD_FOTO_HOME + ( request.getParameter("fileName") == null ? "" : request.getParameter("fileName") );
     FileInputStream fis = null;
     OutputStream os = null;
     try {
          fis = new FileInputStream(fileName);
          os = response.getOutputStream();
          while ( fis.available() > 0  ) {
               os.write(fis.read());
     } catch (Exception e) {
          e.printStackTrace(System.out);
     } finally {
          if (fis != null) {
               try {fis.close();} catch (IOException e1) {} finally {fis = null;}
          if (os != null) {
               try {os.flush();os.close();} catch (IOException e1) {} finally {os = null;}
%>

Similar Messages

  • How to include external files in JSP

    hi,
    How to include external files like image or javascript in to jsp pages.
    I am using MVC frame work. So i have to use request dispatcher. While calling the pages trough dispatcher the external files are missing from the output. Also am using tomcat as server,web.xml(deployment descriptor ) is also configured.
    regards
    sree

    When you use the request dispatcher, the relative path for all your ressources becomes the relative path of your calling serlvet and not the JSP/servlet that you call. Make sure to use the full relative path such as:
    /PATH_TO_YOUR_DIRECTORY/myRessource.ext
    NOT
    SOME_DIRECTORY/myRessource.ext
    Jeff

  • How to include external .js file into .ear file using NetBean 5.5.1?

    is it possible to include external .js file into .ear file using NetBean 5.5.1? if not, then where should I placed the external .js file and how to write the src="xxx" element? (that is , how to write the path of "xxx"?)
    I am using JBOSS 4.2.2 GA as server
    thx!!

    Even this is also not possible because, the code in that package is developed by DWR and i am just adding it to my lib folder and importing the respective classes. In the same manner i need to include the *.js* file in the JSP.It took me about 3 minutes reading through the [DWR Getting Started|http://getahead.org/dwr/getstarted] page to learn that you don't have to do any of that. Perhaps you should read that page?

  • Display image on jsp page

    I have to display image on jsp page with some text output. This image is already saved at a location parallel to web-inf and is generated dynamically using a servlet. I have used img tag html to display the image. Other outputs are taking their values from database.
    First problem is that image will be taking time to display in comparision of other outouts from database. I have to refresh the page to get imageon my page.
    Second is that if I save image in a folder parallel to web-inf in my project then it will not be displaying the image.
    Can I use any jsp functionality to display image with other outputs from database. I have used "*include*". but it shows only that image and other outputs.

    Best way is to use a servlet for this.
    <img src="path/to/imageservlet?id=someidentifier">
    <!-- or -->
    <img src="path/to/imageservlet/someidentifier">In the servlet's doGet() just write code which gets an InputStream of the image (either directly generated, or just read from the local disk file system, if necessary with help of ServletContext#getRealPath(), or even from the DB by ResultSet#getBinaryStream()) and writes it to the OutputStream of the response. That's basically all. Don't forget to buffer the streams properly to speedup performance and to save memory.
    You can find here a basic example: [http://balusc.blogspot.com/2007/04/imageservlet.html].

  • How to include external style sheet

    Hi !!
    Can any one explain how to include external style sheet.

    Anil,
    Simply include the css file as I demonstrated in my last post and then reference one the css classes by name in the "styleClass" attribute of the component. So, if the css file that have included in my JSP file (as demonstrated in the prev post) contains a class like:
    .spicyPink {
    color:#FF1CAE;
    font-weight:bold;
    Then I would simply reference this class in the component as folows:
    <af:outputText value="Pretty In Pink" styleClass="spicyPink"/>
    However, you can also add styling to a component inline and accomplish the same task:
    <af:outputText value="Pretty In Pink" inlineStyle="color:#FF1CAE;
    font-weight:bold;"/>
    The thing to remember is that other than the use of styleClass and inlineStyle, referencing and including css in your JSP/JSPX pages is really no different that used in an HTML page.
    Here is reference to help you along:
    http://www.w3.org/TR/REC-CSS1#containment-in-html
    --RiC                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Include external html trough code in a custom tag

    Hi,
    I have a custom tag. The component class (say MyComponentUI) for my custom component extends UIComponentBase.
    I want to include an external html page, which url is formed using the content of an attribute of my tag (for example, the "value" attribute), through the MyComponentUI code.
    Can anyone give me some help ?
    Pietro

    That's actually the same result as you get with a normal include - The method I suggested is a dynamic include similar to the <jsp:include> tag, not the <%@ include %> tag.
    When you use a dynamic include, the included page is compiled and 'invoked' as serarately, and the result is what gets included, not the actual source. To get the behaviour you want would require an include directive (the <%@ include %> tag) which I don't think has an equivalence you can use inside a custom tag.
    What you can do is pass attributes from the tag class like this:
    pageContext.setAttribute(<name>, <object>, pageContext.REQUEST_SCOPE);
    and then remove them after the include using pageContext.removeAttribute.
    I'm not certain, but this implies that if you declare your variables with a <jsp:usebean> tag with request scope, instead of normal Java variable declarations, you should be able to use them in the page included by the custom tag.
    *** in the jsp ***
    <jsp:useBean id="myVar" scope="request" class="java.lang.String" />
    <% myVar = "Something"; %>
    // Now call the tag which uses the pageContext.include() mehtod.
    *** end ***
    *** in the include ***
    <jsp:useBean id="myVar" scope="request" class="java.lang.String" />
    <%= myVar + " something else" %>
    *** end ***
    Let me know if it works.

  • Use Web API url for loading an external image in a report

    Hi there,
    I am trying to host an image in a report, which is coming out of a Web API. I am able access the image through a URL in a browser:
    https://companytest.com/api/TestFormImage?id=10358&EntityId=6719&imgMetadataName=TestDiagram
    Can anyone please help me how to include this in a report ? I have tried to include this as an external image with the URL embedded as a string like: 
    =string.Format("https://companytest.com/api/TestFormImage?id=10358&EntityId=6719&imgMetadataName=TestDiagram")
    NOTE: the output of the Web API is an image of MIME type PNG.
    Thanks in advance!
    Regards, Laks

    To add an external image
    In report design view, on the Insert tab, click Image.
    On the design surface, click and then drag a box to the desired size of the image.
    On the General tab of the Image Properties dialog box, type a name in the
    Name text box or accept the default.
    (Optional) In the Tooltip text box, type text to display when the user hovers over the image in a report rendered for HTML.
    In Select the image source, select External.
    For an image on a report server in native mode, type a relative path to the image in the
    Use this image box—for example, ../images/image1.jpg.
    For an image on a report server in SharePoint integrated mode, or any other Web site, type a full URL to the image in the
    Use this image box—for example, http://<SharePointservername>/<site>/Documents/images/image1.jpg.
    For more information, see Specifying Paths to External Items (Report Builder and SSRS).
    (Optional) Click Size, Visibility, Action, or
    Border to set additional properties for the image report item.
        Click OK.
    Andy Tauber
    Data Architect
    The Vancouver Clinic
    Website | LinkedIn
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click
    "Mark as Answer" and "Vote as Helpful" on posts that help you. This can be beneficial to other community members reading the thread.

  • (HELP) - Loading multiple external images error #1010

    I'm a newbie at Flash and spent the whole day trying to fix this one problem and can't seem to fix it no matter what I do.
    I'm using Action Script 3.0, CS5 on Windows 7.
    I'm trying to load two external images (thumbnail images) in the loader "see image below for reference", where the full image size will be. However, I am getting this error when testing my movie. What am I doing wrong?
    Test Movie error message:
    "port1_btn
    TypeError: Error #1010: A term is undefined and has no properties.
        at index_fla::MainTimeline/fl_MouseClickHandler_6()"
    "port2_btn
    TypeError: Error #1010: A term is undefined and has no properties.
        at index_fla::MainTimeline/fl_MouseClickHandler_7()"
    FYI:
    Loader instance name: mainLoader
    Thumbnail #1 instance name: port1_btn
    Thumbnail #2 instance name: port2_btn
    This is my code

    Go into your Flash Publish Settings and select the option to Permit Debugging.  Then the error messages could include an indication of which lines are causing the problem.
    And just for the sake of asking since the code is somewhat minimal... is the mainLoader inside a container named portfolioBkgd?

  • Load external images

    Im wanting to make a photo gallery that I can update easy, I
    thought if I could have a movieclip load external images for my
    thumbnails it would be nice but Im not sure how to set it up. I
    would make a file for my images and then another for my thumbnails,
    then if or when I want to switch out pictures I'd just have to drop
    new images into my folders and not even touch the FLA. Could anyone
    point me in the right direction with the right action script to use
    or a tutorial that explains how this is done..
    And when I say load external I want it so I dont have to
    click on the image for it to load, I want it to just load
    automatically , I would put an invisible button or something to
    that efect over the images for a animated effect.
    Thanks for your help.

    I don't know if you are super enthusiastic about building it,
    or if you just want the utility. If the later is the case, I just
    purchased (cheap) a really great product that does all of what you
    want, fairly automatically, including scaling large pictures down
    to smaller size so that they load quickly.
    I am not sure of the policy of recommending third-party
    applicatons here. So, if you are interested, send an email (private
    message) to me and I will show you the simple site I put together
    using it and where to get it.

  • How to add external images onto a control and still be able to resize

    Hello,
    I'm a LabVIEW newbie.  I'm trying to customize the appearance of my VIs, and one of the things I like to do is to import external image and paste it onto the faceplate of the gauge indicator.  I've followed the instruction in the Labview application note using the control editor and was able to paste the picture into indicator.  But when I use it in the front panel and resize the gauge indicator, the image (added as a decoration) doesn't resize together with the indicator.  My questions are:
    1. How to add external images onto a control/indicator and still be able to resize the image automatically when I resize the control/indicator?
    2. How to "add" a new part to an existing control/indicator?  It looks like I can only customize/modify the existing parts of the control/indicator in the control editor. 
    Any help is appreciated.  Thanks.

    1/ Do not use the image as an added decoration. Instead replace part of the control with the image. Tis is illustrated in the attached vi : the arrow was pasted as a decoration, and also used to replace the slide cursor. Changing the control size do not affect the decoration, but changes the cursor.
    2/ What do you mean by adding new parts to a control. We have just seen that it was possible to modify a control. Now, if you want to include additionnal functionnality, that's another story. You can replace parts of the control, and this can give interesting results.  You can edit a slide control, and replace the numeric indicator by another control, including a numeric indicator, that you can replace with etc...
    But there, it still the same info displayed under different forms. If you want to have several independant functions on the same control, such as a string display and a boolean and a numeric indicator, then that's a job for a cluster...
    Chilly Charly    (aka CC)
             E-List Master - Kudos glutton - Press the yellow button on the left...        
    Attachments:
    Slide with Arrow.vi ‏13 KB

  • Use of external image

    With AS3 if you need to make an external file such as a image appear in flash you use the UIloader component.
    How do you achieve the same effect with AS1, i strictly need to make it happen in AS1

    that is great
    a few questions
    in your example you used instancename.loadmovie("file location"); and put the code in the pearents actions
    instead i have used this.loadmovie("file location"); and put the code in the childs actions
    does it matter which version of the code i use?
    also i may decide to add a preloader to it.
    to do this would i simply use the AS1 versions of commands such as loaderInfo.bytesTotal; and loaderInfo.bytesLoaded; on the child clip?
    If yes does preload code such as bytestotal include external data which is called upon by a loadmovie command?
    if not how would i make a preloader take account of external content?

  • Load external images and video in edge

    Hi.
    I am looking into edge instead of flash.
    My question is how do you load external images and videos from an external server?
    To clarify, I need to insert a link from where the images can load into the stage, so they don´t take up any of the final weight.
    Thanks in advance.
    Best,
    Stig.

    The above solution I provided works if you already have the images in your composition and than if you want form load images from another server rather than the relative location to your html file.
    Now if you want to load images dynamically into a rectagle containter you will have to code it.
    below is a sample for that
    Create a 'rectangle' with name 'Rectagle'.
    Add the snippet in the compositon ready or on other user actiosn based on your requirement
    var myImagetag = '<img src="http://wwwimages.adobe.com/include/style/default/SiteHeader/logo-2x.png">'
    sym.$("Rectangle").append(myImagetag);

  • Problem with displaying BLOB images on JSP page using a servlet

    hi. I have a big problem with displaying BLOB images using JSP. I have a servlet that connects to the oracle database, gets a BLOB image , reads it, and then displays it using a BinaryStream. The problem is , this works only when i directly call that servlet, that is http://localhost:8080/ImageServlet. It doesn't work when i try to use that servlet to display my image on my JSP page (my JSP page displays only a broken-image icon ) I tried several coding approaches with my servlet (used both Blob and BLOB objects), and they work just fine as long as i display images explicitly using only the servlet.
    Here's what i use : ORACLE 10g XE , Eclipse 3.1.2, Tomcat 5.5.16 , JDK 1.5
    here is one of my image servlet's working versions (the essential part of it) :
                   BLOB blob=null;
              rset=st.executeQuery("SELECT * FROM IMAGES WHERE ID=1");
              while (rset.next())
                   blob=((OracleResultSet)rset).getBLOB(2);
              response.reset();
              response.setContentType("image/jpeg");
              response.addHeader("Content-Disposition","filename=42.jpeg");
                    ServletOutputStream ostr=response.getOutputStream();
                   InputStream istr=blob.getBinaryStream(1L);
                    int size=blob.getBufferSize();
              int len=-1;
                    byte[] buff = new byte[size];
                         while ((len=istr.read( buff ))!=-1 ) {
                   ostr.write(buff,0,len);
             response.flushBuffer();
             ostr.close(); and my JSP page code :
    <img src="/ImageServlet" border="0"  > If you could just tell me what i'm doing wrong here , or if you could show me your own solutions to that problem , i would be very greatful ,cos i'm realy stuck here , and i'm rather pressed for time too. Hope someone can help.

    I turns out that it wasn't that big of a problem after all. All i had to do was to take the above code and place it into another JSP page instead of into a servlet like i did before. Then i just used that page as a source for my IMG tag in my first JSP. It works perfectly well. Why this doesn't work for servlets i still don't know, but it's not a problem form me anymore . Ofcourse if someone knows the answer , go ahead and write. I would still appriceatte it.
    here's the magic tag : <img src="ImageJSP.jsp" border="0"  > enjoy : )

  • Problems with animated masks and external images

    I have an instance of a movieClip on the stage and I load an external JPG into the movieClip.  The movieClip is masked by a layer that begins animating on a certain frame.  As soon as I reach that frame my externally loaded JPG disappears.  The movieClip doesn't mind being masked, but as soon as the mask animates the external image goes away.  The mask animation is shapes drawn frame by frame.
    It is interesting to note that if I create a Class and load the JPG into the movieClip (or do this in a constructor) I do not experience this problem - it is when the JPGs are loaded into an instance of the movieClip that lives on the stage that I encounter this. Unfortunately that is not an option in my project (since I have to get the path for the JPG from an xml file I am loading).
    Could someone please help me with this?

    that's a flash bug.

  • Display image in JSP Portlet

    I create a JSP portlet. But The portlet can't display image(gif file, jpg file). I have modified the provider.xml and the following line is added:
    <imageURL>URL_Path</imageURL>
    But, the image still cannot be displayed.
    How can I display image in JSP portlet?

    Leo Cheung,
    You could try the following :
    1. Add a virtual directory path Alias 'imgf' in the Apache configuration file httpd.conf to load the image file. Add the following line under the alias section :
    Alias /imgf/ "<your directory>\images/"
    2. Place your gif/jpg files (eg., work.gif) in the images directory.
    3. Use the IMG tag of HTML :
    <IMG src="/imgf/work.gif" border=0 width=80 height=80> in the JSP file at the location where you need to display the image.
    Hope this helps
    Pushkala

Maybe you are looking for

  • Microphone troubles

    I am running Vista 32bit and have had some microphone troubles in the past few months. Several months ago my mic was working fine on my x-fi xtreme audio sound card. Vista updated one day, and my microphone stopped working. I checked all of the usual

  • Unicode characters stored as "square box" in Sybase

    Hii.. My application is using weblogic.jdbc.sybase.SybaseDriver to connect to my Sybase Database. Through the application, if i try to insert some Polish characters in univarchar column, it is inserting "square box" at the backend. I am using Weblogi

  • Decode vs. Case in OWB Transformation

    Hello all, I am trying to count or sum all of Reasons where the value is Test. Here is the code which I would like to covert into OWB transformations: sum(decode( REASON ,'Test',1,0) ) Step 1: I tried to use Decode in expression (decode( REASON ,'Tes

  • Return from customer to unrestricted stock

    Dear Expert, is there is a way to make the return from the customer from return order to be back to unrestricted stock

  • Dreamweaver on OSX Color menus freezing

    I have 2 computers both running OSX 10.10 and have the same issue with Dreamweaver 8 on both systems. Unfortunately I have both systems completely upgraded from Apples update site. Dreamweaver is updated to 8.0.2 as well. When I attempt to access the