publicclassUserNumServletextendsHttpServlet{ @Override protectedvoidservice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int width=120; int height=60; BufferedImage img=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); Graphics2D g=img.createGraphics(); g.setColor(Color.LIGHT_GRAY); g.fillRect(0, 0, width, height); g.setColor(Color.YELLOW); Random rand=new Random(); for (int i = 0; i < 15; i++) { int x1=rand.nextInt(width); int y1=rand.nextInt(width); int x2=rand.nextInt(width); int y2=rand.nextInt(width); g.drawLine(x1, y1, x2, y2); } Font f=new Font("Times New Roman",Font.BOLD,50); g.setFont(f); int red=0,green=0,blue=0; String code=""; for (int i = 0; i < 4; i++) { red=rand.nextInt(255); green=rand.nextInt(255); blue=rand.nextInt(255); Color c=new Color(red,green,blue); g.setColor(c); int num=rand.nextInt(10); code+=num; g.drawString(num+"", i*20+20, 49); } HttpSession session=req.getSession();
publicclassUserNumServletextendsHttpServlet{ @Override protectedvoidservice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int width=120; int height=60; BufferedImage img=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); Graphics2D g=img.createGraphics(); g.setColor(Color.LIGHT_GRAY); g.fillRect(0, 0, width, height); g.setColor(Color.YELLOW); Random rand=new Random(); for (int i = 0; i < 15; i++) { int x1=rand.nextInt(width); int y1=rand.nextInt(width); int x2=rand.nextInt(width); int y2=rand.nextInt(width); g.drawLine(x1, y1, x2, y2); } Font f=new Font("Times New Roman",Font.BOLD,50); g.setFont(f); int red=0,green=0,blue=0; String code=""; for (int i = 0; i < 4; i++) { red=rand.nextInt(255); green=rand.nextInt(255); blue=rand.nextInt(255); Color c=new Color(red,green,blue); g.setColor(c); int num=rand.nextInt(10); code+=num; g.drawString(num+"", i*20+20, 49); } HttpSession session=req.getSession(); session.setAttribute("syscode", code);