All pastes #10542 Raw Edit

Anonymous

public java v1 · immutable
#10542 ·published 2005-04-29 04:47 UTC
rendered paste body
//SERVLET:  if you run this application(you'd need tomcat or other servlet container/web.xml/etc.//a DYNAMIC image is generated//I do not understand what to do in order to get this image to display within a JSP or XHTML page//eg: since the content type here is image/jpeg I can't just write out XHTML along with it//Thanksimport javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.awt.*;import java.awt.image.*;import com.sun.image.codec.jpeg.*;import javax.imageio.ImageIO;public class WebImage extends HttpServlet {        /** * Method to receive get requests from the web server * (Passes them onto the doPost method) * @param req The HttpServletRequest which contains the *   information submitted via get * @param res A response containing the required response *  data for this request **/ public void doGet(HttpServletRequest req, HttpServletResponse res)  throws ServletException, IOException {  doPost(req,res); } /** * Method to receive and process Post requests from the web server * @param req The HttpServletRequest which contains the *   information submitted via post * @param res A response containing the required response data *   for this request **/ public void doPost(HttpServletRequest req, HttpServletResponse res)  throws ServletException, IOException {  //*****Make a Simple Image object*****  BufferedImage imageDynamic=   new BufferedImage(230,100, BufferedImage.TYPE_INT_RGB);  Graphics g = imageDynamic.getGraphics();  g.setColor(Color.blue);  g.fillRect(25,25,50,50);      g.setColor(Color.white);    Font tmn = new Font("Times New Roman", Font.PLAIN, 38);    Font ghb = new Font("Goudy Handtooled BT", Font.PLAIN, 100);    g.setFont(tmn);    g.drawString( "AiYai" , 100, /**height - fm.getMaxDescent()*/65);  //*****Send the Image*****  res.setContentType("image/jpeg");    ServletOutputStream sos = res.getOutputStream();  /**  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(res.getOutputStream());  encoder.encode(imageDynamic);  */  //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);  //encoder.encode(imageDynamic);  ImageIO.write(imageDynamic, "JPEG",sos);   }}