/**A servlet demonstrating the Client Pull technique. * Internet Programming 2 - Course * @author Martin Carlsson */ import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.awt.*; import java.awt.image.*; import java.util.*; import com.sun.image.codec.jpeg.JPEGCodec; import java.text.*; public class ClientPull extends HttpServlet { private int yPos = 40, xPos = 40, TEXT_SIZE = 24; public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { System.setProperty("java.awt.headless", "true"); BufferedImage buffImg = new BufferedImage(400,TEXT_SIZE*2,BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); g.setFont(new Font("Serif", Font.ITALIC,TEXT_SIZE)); g.setColor(Color.RED); DateFormat f = DateFormat.getTimeInstance(); g.drawString("Dynamic clock using client pull",xPos,yPos/2); g.setColor(Color.GREEN); g.drawString(f.format(new Date()),xPos*3,yPos); // Encodes and sends the buffered image res.setHeader("Refresh","1");// The client/browser will pull/request the this servlet every second ServletOutputStream sOs = res.getOutputStream(); res.setContentType("image/jpeg"); JPEGCodec.createJPEGEncoder(sOs).encode(buffImg); sOs.flush(); sOs.close(); } }// End class ClientPull