You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@jakarta.apache.org by "Stella Fredö (EMW)" <St...@emw.ericsson.se> on 2000/10/04 09:32:39 UTC

code sample of servlet that sends out graphic

Ok, there comes the code sample for servlet that sends out graphic.
I think you also need to create another servlet that sends out "HTML" and point to this graphic servlet, if you want to mix up the picture with html text and forms.

Have fun.//stella

There also is servlet sample that retrieve data from oracle database and pooled the dataconnection and cache the retrieved data on the server, and draw the picture using the retrieved data. However, it is not included here. 

--------------------------------
// jpgSample.java

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.servlet.*;
import javax.servlet.http.*;

// see comments below about the encoder
//import com.sun.image.codec.jpeg.*;

// download java advance imaging API jai from 
//http://java.sun.com/products/java-media/jai/index.html
// and update your classpath
import com.sun.media.jai.codec.*;  


public class jpgSample extends HttpServlet {

 public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException,
                                                                            IOException
 {
   res.setContentType ("image/jpg"); 
//   res.setContentType ("image/png");
   OutputStream out=res.getOutputStream ();

   BufferedImage image=new BufferedImage (600, 475, BufferedImage.TYPE_4BYTE_ABGR);
   Graphics g=image.createGraphics ();
   g.setColor(Color.white);
   g.fillRect(0,0,600,475);
   g.setColor(Color.red);
   g.drawOval(0,0,600,475);	

  // do it this way, you need to import com.sun.image.codec.jpeg.*;
  //JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out); 

 // do it this way, you need to import com.sun.media.jai.codec.*;
 // JAI writable file format: BMP, JPEG,PNG,PNF, TIFF

   JPEGEncodeParam encodeParam = new JPEGEncodeParam();

// if you do not want to specify the quality of JPEG, can do this way
//    JPEGEncodeParam encodeParam = null;

   ImageEncoder encoder=ImageCodec.createImageEncoder ("JPEG",out,
                           encodeParam);
// another format PNG
//   ImageEncoder encoder=ImageCodec.createImageEncoder ("PNG", out,
                         // PNGEncodeParam.getDefaultEncodeParam(image));
   encoder.encode (image);
   out.flush();
   out.close();
   g.dispose ();

 }


 public String getServletInfo()
 {
   return "Sample jpegcoding";
 }

};
//------ end of code----------email: Stella.Fredo@emw.ericsson.se


Re: code sample of servlet that sends out graphic

Posted by Mike <mi...@mgisoft.com>.
I don't think you should close the output stream, and calling flush is
also not necessary.


-Mike-


Stella Fredö (EMW) wrote:
> 
> Ok, there comes the code sample for servlet that sends out graphic.
> I think you also need to create another servlet that sends out "HTML" and point to this graphic servlet, if you want to mix up the picture with html text and forms.
> 
> Have fun.//stella
> 
> There also is servlet sample that retrieve data from oracle database and pooled the dataconnection and cache the retrieved data on the server, and draw the picture using the retrieved data. However, it is not included here.
> 
> --------------------------------
> // jpgSample.java
> 
> import java.io.*;
> import java.awt.*;
> import java.awt.image.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
> 
> // see comments below about the encoder
> //import com.sun.image.codec.jpeg.*;
> 
> // download java advance imaging API jai from
> //http://java.sun.com/products/java-media/jai/index.html
> // and update your classpath
> import com.sun.media.jai.codec.*;
> 
> public class jpgSample extends HttpServlet {
> 
>  public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException,
>                                                                             IOException
>  {
>    res.setContentType ("image/jpg");
> //   res.setContentType ("image/png");
>    OutputStream out=res.getOutputStream ();
> 
>    BufferedImage image=new BufferedImage (600, 475, BufferedImage.TYPE_4BYTE_ABGR);
>    Graphics g=image.createGraphics ();
>    g.setColor(Color.white);
>    g.fillRect(0,0,600,475);
>    g.setColor(Color.red);
>    g.drawOval(0,0,600,475);
> 
>   // do it this way, you need to import com.sun.image.codec.jpeg.*;
>   //JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
> 
>  // do it this way, you need to import com.sun.media.jai.codec.*;
>  // JAI writable file format: BMP, JPEG,PNG,PNF, TIFF
> 
>    JPEGEncodeParam encodeParam = new JPEGEncodeParam();
> 
> // if you do not want to specify the quality of JPEG, can do this way
> //    JPEGEncodeParam encodeParam = null;
> 
>    ImageEncoder encoder=ImageCodec.createImageEncoder ("JPEG",out,
>                            encodeParam);
> // another format PNG
> //   ImageEncoder encoder=ImageCodec.createImageEncoder ("PNG", out,
>                          // PNGEncodeParam.getDefaultEncodeParam(image));
>    encoder.encode (image);
>    out.flush();
>    out.close();
>    g.dispose ();
> 
>  }
> 
>  public String getServletInfo()
>  {
>    return "Sample jpegcoding";
>  }
> 
> };
> //------ end of code----------email: Stella.Fredo@emw.ericsson.se
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: general-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: general-help@jakarta.apache.org

-- 
=Mike=