PLEASE HELP!!! using servlet to generate an image in jsp page

Hi,
I am so stuck on this... please help.
I have a servlet that generated a gif image dynamically. It uses a bean that stores the gif image in a byte array.
The servlet outputs the byte data to the output stream. The code is really simple and looks like this:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //HttpSession session = request.getSession();
        try {
            ServletOutputStream stream = response.getOutputStream();
            ImageByteInformation imageByteInfo = (ImageByteInformation) request.getAttribute("imageByteInformation");
            response.setContentType("image/gif");
            response.setContentLength(imageByteInfo.getByteData().length);
            stream.write(imageByteInfo.getByteData(), 0, imageByteInfo.getByteData().length);
            stream.flush();
        }catch( Exception e){   
            System.out.println("You are hooped!: " + e.getMessage() + " " + e.toString());           
        }When I redirect from the dispatch servlet straight to this servlet an image shows up in the browser window.
However when I try to use this jsp page to display the image nothing happens...
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h2>Hello World!</h2>
        <img src="servlets/Map24ImageDisplayServlet"/>
         //I also tried src/servlets/Map24..., /src/servlets/Map24..., /display, servlets/display
        <h2>Did you see the image?</h2>
    </body>
</html>My web.xml is here if it helps...
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>HelloProject</display-name>
<servlet>
    <display-name>ServletDispatcher</display-name>
    <servlet-name>ServletDispatcher</servlet-name>
    <servlet-class>servlets.ServletDispatcher</servlet-class>
  </servlet>
  <servlet>
    <display-name>Map24ImageDisplayServlet</display-name>
    <servlet-name>Map24ImageDisplayServlet</servlet-name>
    <servlet-class>servlets.Map24ImageDisplayServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletDispatcher</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
  <error-page>
    <error-code>404</error-code>
    <location>/404_error.html</location>
  </error-page>
  <servlet-mapping>
    <servlet-name>Map24ImageDisplayServlet</servlet-name>
    <url-pattern>/display</url-pattern>
  </servlet-mapping>
</web-app>I can never get an image to come up. In fact I can never get the jsp page to run the servlet at all! HELP!!! What am I doing wrong?
Thanks
Edited by: Kind_of_Green on May 5, 2008 3:55 PM
Edited by: Kind_of_Green on May 5, 2008 4:00 PM

OK... so you WERE absolutely right about the src path for the image tag.
However I also had another problem that was quite a bit more insidious and mostly just a symptom
of how little I know about what goes on under the hood of a web app.
My bean storing the image info was stored as a request attribute. When the servlet was called from the
jsp page the request object was either reset or just never initialized. Anyway it is not the same request
object I assumed it was being passed in the doGet method. I added my bean as a session
attribute and everything is sparkly :)
I can only assume that when a request is neither forwarded nor included (as is the case with
calling the servlet from the img tag) it is disappeared.
Anyway, thanks a mint man. I so totally appreciate your time.
Ciao :)

Similar Messages

  • Please help me!--rendering makes the images or video blurry (very pixelated) deteriorates the image  Adobe Premier Elements 13  need help!  .jpg and mpeg images,  but I have never "rendered" before since I got APE 13 about 6 weeks ago.  I am desperate for

    Please help me!--rendering makes the images or video blurry (very pixelated) deteriorates the image  Adobe Premier Elements 13  need help!  .jpg and mpeg images,  but I have never "rendered" before since I got APE 13 about 6 weeks ago.  I am desperate for assistance!

    That's going to be a ridiculous waste of money and energy.
    First of all, the current ATI drivers don't support multiple GPUs, so at the moment even a single 4870X2 would be only a 'normal' 4870 (which is quite a speed beast already). GFX drivers evolve rapidly, so things might look different next month, but when it comes to Linux and hardware there's one Golden Rule: stay away from the newest stuff and wait for proper support to get coded.
    I also wonder what power supply could possibly cope with the differences between idle and full load; that's way beyond 400W. But then, I'm one of those "quiet&green" types where >100W idle is already a bit much.
    I kind of understand that you want to get it done and not worry about hardware for the next 10 years or so, but that's simply not how the hardware world works and never did. At least not for the average consumer.

  • Why we are using servlet as a controller instead of Jsp?

    Can any explain answer for this question
    Thanks in Advance
    Why we are using servlet as a controller instead of Jsp?

    nareshannam wrote:
    Can any explain answer for this question
    Thanks in Advance
    Why we are using servlet as a controller instead of Jsp?Basically JSPs are suited to do just one thing, to generate HTML with variable content. You can put Java code in there too, but it's not a good place for it. Messy to validate, messy to debug. In fact, with tag libraries like JSTL, actual Java code is rare in professionally written JSPs.
    A controller does the bit that comes before you've decided what kind of response the user is going to get, typically it may invoke one of several JSPs according to validation etc.. There's a lot of logic, a lot of parsing parameters and so on. Much cleaner to do this from a Java class like a Servlet.
    Judging by this forum quite a lot of people come to JSPs from ASP or PHP and don't want to bother with actually writing Java classes. That's never going to be much good.

  • Please help us, we have a problem with loading a page is slow we have to wait a longer time to open our one page, thanks in advance

    Please help us, we have a problem with loading a page is slow we have to wait a longer time to open our one page, thanks in advance

    You can try these steps in case of issues with web pages:
    You can reload web page(s) and bypass the cache to refresh possibly outdated or corrupted files.
    *Hold down the Shift key and left-click the Reload button
    *Press "Ctrl + F5" or press "Ctrl + Shift + R" (Windows,Linux)
    *Press "Command + Shift + R" (Mac)
    Clear the cache and cookies only from websites that cause problems.
    "Clear the Cache":
    *Firefox/Tools > Options > Advanced > Network > Cached Web Content: "Clear Now"
    "Remove Cookies" from sites causing problems:
    *Firefox/Tools > Options > Privacy > "Use custom settings for history" > Cookies: "Show Cookies"
    Start Firefox in <u>[[Safe Mode|Safe Mode]]</u> to check if one of the extensions (Firefox/Tools > Add-ons > Extensions) or if hardware acceleration is causing the problem.
    *Switch to the DEFAULT theme: Firefox/Tools > Add-ons > Appearance
    *Do NOT click the Reset button on the Safe Mode start window
    *https://support.mozilla.org/kb/Safe+Mode
    *https://support.mozilla.org/kb/Troubleshooting+extensions+and+themes

  • Please help, after is downloaded IOS 7 I lost my Pages App and a few more and also I now have restriction set on please help

    Please help, after is downloaded IOS 7 I lost my Pages App and a few more and also I now have restriction set on please help

    If you can't reach the person that sold it to you, you essentially have an iBrick.
    You are encountering Activation Lock, which cannot be bypassed.
    You may want to just turn the phone over to the police as it's nothing more to you than a paperweight and may be stolen.
    ~Lyssa

  • 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].

  • ADF BC:Displaying Image On JSP page

    Hai All
    I'm using ADF BC and JDev 10.1.3.
    I want to show an image on my jsp page from database.
    For this I have written an servlet with the following code
    response.setContentType("image/gif");
    OutputStream os = response.getOutputStream();
    String amDef = "package.AppModuleName";
    String config = "Configuration Name";
    ApplicationModule am = Configuration.createRootApplicationModule(amDef, config);
    ViewObjectImpl vo =
    (ViewObjectImpl)am.findViewObject("ViewObject"); // get view object (the same as used in the table)
    System.out.println("vo:" + vo);
    vo.executeQuery();
    Row row = vo.first();
    BlobDomain image = (BlobDomain)row.getAttribute("field");
    //System.out.println("image:" + image);
    InputStream is = image.getInputStream();
    // copy blob to output
    byte[] buffer = new byte[10 * 1024];
    int nread;
    while ((nread = is.read(buffer)) != -1)
    os.write(buffer, 0, nread);
    os.close();
    Configuration.releaseRootApplicationModule(am, false);
    This servlet working perfectly.The image is displayed on the page while running
    the servlet alone.
    My problem is the image is not got displyed on the jsp page
    I tried following code to call the servlet
    <af:objectImage source="ImageServlet"/>
    <img src="ImageServlet" width="140px" height="50px" align="right">
    Where ImageServlet is the url-pattern in the web.xml for the servlet
    Both method are not working
    Any body please help me.......
    what I'm missing..............
    Is there any other way to display an image on JSP page using backing bean
    method.
    Thanks
    Ans

    HI,
    See: http://kuba.zilp.pl/?id=241
    Kuba

  • How to display uploaded image in jsp page.

    Hello,
    I am using struts 1.2.9 and and have uploaded image on the server. Now what I want to do display the image in jsp page after clicking on one link in jsp. I have tried many thing to display image in jsp page. But I am getting an error during displaying image in jsp. I have displayed absolute path in servlet. and used InputStream and outputstream to display image in jsp page.
    Can any one help.
    Thanks in advance
    Manveer Singh

    Follow this. This topic is very popular recently on the forum.

  • Problem in displaying image on jsp page

    I want to display an image on jsp page.I copied the image the image in WebContent folder.
    I am able to see the image on the design pane when using the following code:-
    <img src="/image.gif" height="50" width="50"> but when i run it in the browser nothing gets diplsyed.
    also when i use the image container and select the source option that image is not displayed in the WebContents file..plz help.

    Hi,
    Seems that the Problem is there with the way how you specifuied the Image relative Path:
    You have specified the below: The opath of image starts with (/) forward slash:
    *<img src="/image.gif" height="50" width="50">*
    It means to access this Image WebLogic Container will form a Path like this:
    http://localhost:7001/image.gif
    BUT may be the image is available inside your Context root: So Change the <Img> tag like following in your JSP:
    *<img src="image.gif" height="50" width="50">*
    (*NOTE:* Never Start your src path with a Preceesing /)
    Now WLE will consider the path like below ..if your Applications Context root is "TestApp"
    http://localhost:7001/TestApp/image.gif
    I am Assuming that Inside the TestApp Application "image.gif" And "your.jsp" jSP pages are Co-Located (Means available in the Same Directory).
    Thanks
    Jay SenSharma
    http://jaysensharma.wordpress.com (WebLogic Magical Stuff)

  • How can we  use java variable in javascript code on JSP page?

    How can we use java variable in javascript code on JSP page?
    Pls help

    Think about it:
    JSP/Java is executed on the Server and produces HTML+JavaScript.
    Then that HTML+JavaScript is transfered to the client. The client now interpretes the HTML+JavaScript.
    Obviously there's no way to access a Java variable directly from JavaScript.
    What you can do, however, is write out some JavaScript that creates a JavaScript variable containing the value of your Java variable.

  • How we can generate dynamic menu in jsp page

    Hi all,
    how we can generate dynamic menu in jsp page.
    Thanks
    Manjinder

    by reading more about them on the web or in a good book, OR BY HIRING SOMEBODY TO DO SO. ;)

  • 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 : )

  • Using servlet to generate XML file

    What I want to do is simple but can't work. Hope you guys can give me a hint.
    I succesfully generate XML file in command line using Oracle XML parser and class generator. Then I was trying to do it using servlet. it compiles fine but generate NullPointer exception at the line:
    Emp e1= new Emp(); //Emp is the root of XML
    I suspect the Emp constructor can't correctly find the globalDTD in its superclass -CGDocument. Please note this only happens in servlet setting, not is command line setting. Any suggestion to get arounf this?
    Another unrelated question is that when I create a XML file using the Oracle XML parser, it seems all the elements a file has to be added once, otherwise the compiler will compalain about the missing element. this will be inconvinient when I constructing a big XML file, which I 'd liek to split into small piece and add them up. Maybe there is a good way but I just don't know it.
    my email: [email protected]

    Hi,
    I'm running into the same problem deploying the classes generated by the class generator. Code works fine from JDeveloper, but had to put my DTD in the directory where my classes are. Deploying the classes with Apache's JServ gives me a NullPointer exception on the first addNode method. I guess it can't find the DTD. I tried to put the DTD in many locations but this didn't fix the problem. Any suggestions?
    Steve,
    Did you fix this problem? Thanx!
    null

  • Please help me(How to allign an Image.)

    I'm using
    StyledDocument document = (StyledDocument) tp.getDocument();
    Style imageStyle = document.addStyle("StyleName", null);
    StyleConstants.setIcon(imageStyle,new ImageIcon("Images\\image1.gif")
    document.insertString(document.getLength(), "ignored text",imageStyle);codes to append an image to JTextPane and
    Style style = document.addStyle("StyleName", null);
    StyleConstants.setForeground(style, new Color(r, g, b));
    document.insertString(document.getLength(), appendMessage, style);
    codes to append text to a JTextPane.
    But when i append both images and texts ,Images are above the text.
    Here I mean the bottom level of Images are at the same level of texes.
    I want to make images below the texes
    How can i do it?
    If you can give me a little exampl
    I think You can understand my requirement.
    If it is difficult to understand tell me.
    please help me

    I'm using
    StyledDocument document = (StyledDocument) tp.getDocument();
    Style imageStyle = document.addStyle("StyleName", null);
    StyleConstants.setIcon(imageStyle,new ImageIcon("Images\\image1.gif")
    document.insertString(document.getLength(), "ignored text",imageStyle);codes to append an image to JTextPane and
    Style style = document.addStyle("StyleName", null);
    StyleConstants.setForeground(style, new Color(r, g, b));
    document.insertString(document.getLength(), appendMessage, style);
    codes to append text to a JTextPane.
    But when i append both images and texts ,Images are above the text.
    Here I mean the bottom level of Images are at the same level of texes.
    I want to make images below the texes
    How can i do it?
    If you can give me a little exampl
    I think You can understand my requirement.
    If it is difficult to understand tell me.
    please help me

  • Please help with servlet and database!!

    Hello,
    i first created a servlet that generated an html page and printed out the parameters of a user's username when they logged in. example, "Welcome user" and it worked fine...
    i inserted a database into my site that validates the username and password, and ever since i did that in dreamweaver, when a user logs in sucessfully, it returns the servlet page like its supposed to, only that it says "Welcome null" instead of "Welcome John." pretty strange, huh!? can anyone please help? thanks!

    Check whether the username textfield name has changed or not. Also print the code over here so that we can check the code.

Maybe you are looking for