tags:

views:

20

answers:

3

hi i wrote a servlet which takes the name of a image from the client and displays it to client by converting it to byte array.after displaying the image now when i am trying to print some text i m not able to do so.it simply doesnt print the text even after using PrintWriter.

i am using jboss application server to deploy it.

here is the servlet-Image.java:

package javaserv.image;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ImageIcon;

    public class Image extends HttpServlet {
        private static final long serialVersionUID = 1L;
        public Image() 
    {
        super();
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        String imagename = request.getParameter("imagename");

        File fileimage = new File("C:/langs/projects/javaserv/"+imagename+".jpg");
        byte [] imagearray;
        imagearray = new byte[(int)fileimage.length ()];
        FileInputStream fis = new FileInputStream (fileimage);
        fis.read (imagearray);
        response.setContentType("image/jpeg");
        response.setContentLength((int)fileimage.length ());
        ServletOutputStream out = response.getOutputStream();
        int i=0;
        while(i<imagearray.length)
        {
            out.write(imagearray[i]);
            ++i;
        }

        out.flush();
        out.close();
        out = response.getOutputStream();
        response.setContentType("text/html");
        out.println("<html><body>");
        out.println("here is your image");
        out.println("<p>");


        out.println("</html></body>");

        out.close();
    }

}

"imagename" is the name of the image entered by client

+1  A: 

You can either write your JPEG to the out, or the HTML, not both.

Your code writes the JPEG to the output, closes the output stream, then tries to open it again to write some HTML.

If you want to display a synamic image on a HTML page, you should be using a servlet for the image, and a JSP page for the HTML.

skaffman
A: 

Those interested in a similar code in C rather than Java can find it there:

http://gwan.ch/source/fractal.c.txt

Pierre
A: 

You basically need to let the client fire two HTTP requests. One to the HTML which in turn contains an <img> element which let the webbrowser automagically send another HTTP request to the image file.

First create a HTML page like follows:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>An image</title>
    </head>
    <body>
        <p>Here is your image</p>
        <p><img src="images?imagename=foo.gif"></p>
    </body>
</html>

Where I assume that your image servlet is listening on an url-pattern of /images. Finally just give the enduser the URL to the HTML page (can also be a JSP page by the way) instead of the URL to the image. The enduser will then retrieve a HTML page with some text and an image.


That said, your imageservlet is honestly said pretty poor written in terms of speed, memory efficiency and resource handling. I'd suggest to have a look for this basic example how to do it properly.

BalusC