package lon;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import javax.imageio.*;
public class Img extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
response.setContentType("image/jpeg");//设置content-type响应头
response.setHeader("Pragma","NO-cache"); //设置不缓存
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
OutputStream os= response.getOutputStream();
int width=80,height=20;
BufferedImage image= new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphics g=image.getGraphics();
Random random=new Random();
g.fillRect(0,0,width,height);
g.setFont(new Font("Times New Roman",Font.ITALIC,18));
String str="";
for(int i=0;i<4;i++){
String st=String.valueOf(random.nextInt(10));
str+=st;
g.setColor(new Color(random.nextInt(250),random.nextInt(250),random.nextInt(250)));
g.drawString(st,20*i+6,16);
}
g.dispose();
ImageIO.write(image,"JPEG",os);
out.println("<html>");
out.println("<head><title>Servlet</title></head>");
out.println("<body bgcolor=\"#ccffcc\">");
out.println("<p>");
out.println("</body>");
out.println("</html>");
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request,response);
}
//Clean up resources
public void destroy() {
}
}