Your Servlet is going to have to get a little fancier.  Basically, you have
to look at what's being requested, and serve up the right component.  Here's
a start:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;

public class GraphicsTest extends HttpServlet {
   private static final int WIDTH = 650;
   private static final int HEIGHT = 650;

   public void doGet(HttpServletRequest request,HttpServletResponse
response) throws ServletException, IOException {
      if(request.getRequestURI().endsWith(".jpeg")) {
          // open the output and set the type to JPEG
        ServletOutputStream out = response.getOutputStream();
        response.setContentType("image/jpeg");

          // create a blank image
        BufferedImage image = new BufferedImage(WIDTH,
HEIGHT,BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        g2.setColor(Color.blue);
        g2.fillRect(0,0,WIDTH,HEIGHT);

          // encode the output to JPEG
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(image);

          // send the output out
        out.flush();
        out.close();
    } else {
      PrintWriter out = response.getWriter();
      response.setContentType("text/html");
      out.println("<html>");
      out.println("  <head>");
      out.println("    <title>Blue Box Test</title>");
      out.println("  </head>");
      out.println("  <body>");
      out.println("    <h1>Blue Box Test</h1>");
      out.println("    <img src=\"/servlet/GraphicsTest/bluebox.jpeg\">");
      out.println("    <em>This is a blue box</em>");
      out.println("  </body>");
      out.println("</html>");
    }
   }
}

Basically the flow is like this.  When a request comes in, check if it's a
request for a jpeg image, if it is, create a blue box jpeg image and stream
it back to the client.  If it's not, send the client some HTML.  Inside the
HTML is an Image tag that requests the blue box image from the Graphics Test
Servlet (or whatever you named your servlet).  When the browser starts to
display the HTML, it will see the image tag and request the image from the
servlet.  Since the image name ends with .jpeg, the blue box image will be
created and sent.
  (*Chris*)

----- Original Message -----
From: "Kenny G. Dubuisson, Jr." <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, September 30, 2002 12:27 PM
Subject: [SERVLET-INTEREST] Mixing HTML and JPEG image on one output page


> I'm trying to write a servlet that draws a chart and put's some HTML info
> about the chart on a page.  For a test, I got a servlet to draw a blue
> rectangle (proving my graphics and JPEG encoding worked) in my browser.
The
> problem I'm having is that I can't seem to add HTML to the page; it only
> draws the rectangle when I send the graphics as a single JPEG image.
>
> Does anyone know how to mix an embedded JPEG image and regular HTML?  I've
> attached my little test servlet below for reference.  Thanks,
> Kenny
>
> import java.io.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.awt.*;
> import java.awt.image.*;
> import com.sun.image.codec.jpeg.*;
>
> public class GraphicsTest extends HttpServlet
> {
>    private static final int WIDTH = 650;
>    private static final int HEIGHT = 650;
>
>    public void doGet(HttpServletRequest request,
>       HttpServletResponse response) throws ServletException, IOException
>    {
>       // open the output and set the type to JPEG
>       ServletOutputStream out = response.getOutputStream();
>       response.setContentType("image/jpeg");
>
>       // create a blank image
>       BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
>          BufferedImage.TYPE_INT_RGB);
>       Graphics2D g2 = image.createGraphics();
>       g2.setColor(Color.blue);
>       g2.fillRect(0,0,WIDTH,HEIGHT);
>
>       // encode the output to JPEG
>       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
>       encoder.encode(image);
>
>       // send the output out
>       out.flush();
>       out.close();
>    }
> }
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to