----发个随机验证码示例
来源:互联网 发布:淘宝多长时间确认收货 编辑:程序博客网 时间:2024/06/04 23:25
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=GBK");
- PrintWriter out = response.getWriter();
-
-
- char[] strChar=new char[]{'a','b','c','d','e','f','g','h','k','j','想','中','天','地','华','1','2','3','4','5','6','7','8','9','0'};
-
- BufferedImage image=new BufferedImage(60,50,BufferedImage.TYPE_INT_RGB);
- Graphics gra=image.getGraphics();
-
- gra.setColor(Color.white);
- gra.fillRect(0, 0, 60, 50);
-
- gra.setColor(Color.red);
- gra.setFont(new Font("宋体",Font.BOLD,15));
- String strTest="";
- char strNum=0;
-
- Random r=new Random(new Date().getTime());
-
- for (int i = 0; i < 4; i++) {
-
- strNum=strChar[r.nextInt(strChar.length)];
-
- strTest+=strNum;
-
- gra.drawString(Character.toString(strNum), i*15, 35);
- }
- System.out.println(strTest);
-
-
-
- request.setAttribute("strTest", strTest);
- request.getRequestDispatcher("../main.jsp").forward(request, response);
-
-
-
-
-
-
-
-
-
-
-
- }