@WebServlet("/ckeckCodeDemo01")
public class CheckCodeDemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
//response.setContentType("charset=utf-8");
int wight = 100;
int height = 50;
//创建一个对象,在内存中图片
BufferedImage img = new BufferedImage(wight,height,BufferedImage.TYPE_3BYTE_BGR);
//美化图片
Graphics g = img.getGraphics();
g.setColor(Color.PINK);
g.fillRect(0,0,wight,height);
g.setColor(Color.BLUE);
g.drawRect(0,0,wight-1,height-1);
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random ran = new Random();
StringBuilder sb = new StringBuilder();
for(int i = 1; i <= 4; i++) {
int a = ran.nextInt(str.length());
char chr = str.charAt(a);
sb.append(chr);
g.drawString(chr+"",wight/5*i,height/2);
}
String checkCode_session = sb.toString();
request.getSession().setAttribute("checkCode_session",checkCode_session);
for(int i = 0; i <= 6; i++) {
int x1 = ran.nextInt(wight);
int x2 = ran.nextInt(wight);
int y1 = ran.nextInt(height);
int y2 = ran.nextInt(height);
g.drawLine(x1,y1,x2,y2);
}