@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//第一步:在内存中生成width height图片
int width=100;
int height=40;
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
//第二步:获得图片的画笔,设置颜色,画一个带颜色的矩形
Graphics g=image.getGraphics();
g.setColor(Color.pink);
g.fillRect(0,0,width,height);
g.setColor(Color.BLACK);
g.drawRect(0,0,width-1,height-1);
//第三步:在矩形图片上随机画4个字母或数字
String s="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random ran = new Random();
g.setFont(new Font("宋体",Font.BOLD,24));
for (int i = 1; i <=4 ; i++) {
int index=ran.nextInt(s.length());
char c=s.charAt(index);
g.drawString(""+c,width/5*i,height*2/3);
}
//第四步:画干扰线
for (int i = 0; i < 10; i++) {
int x1=ran.nextInt(width);
int x2=ran.nextInt(width);
int y1=ran.nextInt(height);
int y2=ran.nextInt(height);
g.drawLine(x1,y1,x2,y2);
}
//第五步:把这个图片写到response中
ImageIO.write(image,"jpg",resp.getOutputStream());
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
F12看下前端网页控制台报什么错
F12打开浏览器控制台,切换到network,刷新页面,查看请求。