This is my first servlet to generate and display image to the client,
But There is compile time error in the mentioned section by commect.
public class DynamicImage extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("image/jpeg");
//create image
int width = 200;
int height = 30;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//get drawing context
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(Color.GRAY);
g.fillRect(0, 0, width, height);
//draw a string
g.setColor(Color.WHITE);
g.setFont(new Font("Dialog", Font.PLAIN, 14));
g.drawString("My String!", 10, height / 2 + 4);
//draw a border
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
//dispose context
g.dispose();
//send back image to the client
ServletOutputStream sos = response.getOutputStream();
com.sun.image.codec.jpeg.JPEGImageEncoder encoder = JPEGCodec // Compile Time Error, cant find JPEGCodec
}
}
My IDE (Netbeans) cant find and import JPEGCodec
, What is the solution?