Results 1 to 3 of 3

Thread: Can not display image and text from servlet

  1. #1
    Join Date
    Mar 2006

    Can not display image and text from servlet

    Hello friends,
    I am very new in Java (servlets). I am trying to display (created) image depends on the input parameters. example: i might send parameter as square/circle/poly/rectangle etc to the servlet. Servlet will open my image from disk and draw picture of the parameter sent.
    e.g. if i sent a "rectangle" as as a parameter value, servlet will draw a rectangle on a loaded image (which can be just blank image). BUT till here it is okay, my question comes here:
    We can't use response.setContentType("image/gif"); and response.setContentType("text/html"); together... I want to display some text which depends on the parameter and text is calculated by the same servlet which is drawing image. I do not want to draw a text on image, want to print it separately on a html page bellow the image.

    sample program: (bellow program need to edit to make runnable)

    import Acme.JPM.Encoders.GifEncoder;

    public class SampleServlet extends HttpServlet {
    public void doGet or doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    out = res.getOutputStream();

    shape = req.getParameter("shape");

    URL url = null;
    Image image;
    url = new URL ("http://hostname/examples/example.gif");
    mt = new MediaTracker(frame);
    image = Toolkit.getDefaultToolkit().getImage(url);
    mt.addImage(image, 0);
    w = image.getWidth(frame);
    h = image.getHeight(frame);
    Image offscreen = frame.createImage(w, h+200);
    g = offscreen.getGraphics();

    g.drawShape .... dummy statement, this will draw shape

    // do some manipulation...
    // here I am expecting bunch of "xyz" text lines from above manipulation.

    GifEncoder encoder = new GifEncoder(offscreen, out);

    This servlet executes nicely but displays only sample image in the html frame; can someone tell me how can i display the "xyz" text lines generated by same servlet in a seperate table, just bellow the image???

    Thanks in advance.

  2. #2
    Join Date
    Dec 2002
    Calgary, Canada
    my guess is u will have to use a different approach.
    That is send html back to browser that includes the text as well as an img tag where src will point to another or the same servlet which returns the image in a specific scenario....let me know if u need more explanation then this..


    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Mar 2006
    Thanks Khalid for your suggession. Yes I also think so... need to change the approch.
    I might have to use two different servlets... and pass data from one to another or some other approch. Session can be used to store only text data and let the servlet send only image then get the text data in next session request....

    if u think any other approch, your help or comments will be appreciated!


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center

Recent Articles