java验证码怎么写

1.验证码部分的java代码怎么写package com.bbs.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.> My JSP 'index.jsp' starting page <meta > 。
6.请教前辈们~怎么用JAVA写验证码那块程序~~import java.util.Random;class Random_number { String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random ran = new Random(); char[] chr = str.toCharArray(); int chrlong = chr.length; String rnumber = ""; public Random_number() { } public String randomNumber(int n) { int k; for (int i = 0; i < n; i++) { k = ran.nextInt(35); if (rnumber.indexOf(chr[k]) == -1) {//判读验证新生成的验证码是否与前面的重复 // System.out.println(chr[k]); rnumber = rnumber + chr[k]; } else {//如果重复重新生成 i--; continue; } } return rnumber; }}//测试类public class test { public static void main(String[] args) { Random_number number = new Random_number(); System.out.println(number.randomNumber(4)); }} 。
7.spring框架的java登录验证码怎么写可以参考下下面的代码 , 这个代码是servlet生成的 , 稍微修改下就能用的 package servlet;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.OutputStream;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class CodeServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedImage image=new BufferedImage(100, 30, BufferedImage.TYPE_INT_RGB); Graphics g=image.getGraphics(); Random r=new Random(); g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); g.fillRect(0, 0, 100, 30); g.setColor(new Color(255,255,255)); g.setFont(new Font(null,Font.BOLD,24)); String code=getCode(); HttpSession session=request.getSession(); session.setAttribute("textcode", code); g.drawString(code, 10, 25); response.setContentType("image/jpeg"); OutputStream os=response.getOutputStream(); ImageIO.write(image, "jpeg", os); os.close(); } //随机生成验证码字符串的类 public static String getCode(){ char[] letters={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t', 'u','v','w','x','y','z'}; String code=""; boolean[] bool=new boolean[letters.length]; for(int i=0;i<5;i++){ int num ; do{ num=(int)(Math.random()*(letters.length)); }while(bool[num]); code+=letters[num]; bool[num]=true; } return code.toUpperCase(); }} 。
8.java 验证码【java验证码怎么写】public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int width=500, height=250;//设置图片大小 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 获取图形上下文 Graphics g = image.getGraphics(); // 生成随机类 Random random = new Random(); // 设定背景色 Color c = new Color(255-random.nextInt(100),255-random.nextInt(100),255-random.nextInt(100)); g.setColor(c); g.fillRect(0,0,100,100); // 设定字体 g.setFont(new Font("Times New Roman",Font.PLAIN,18)); // 随机产生155条干扰线 , 使图象中的认证码不易被其它程序探测到 for (int i=0;i<1550;i++) { Color cx = new Color(random.nextInt(100),random.nextInt(100),random.nextInt(100)); g.setColor(cx); int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x,y,x+xl,y+yl); } // 取随机产生的认证码(4位数字) String sRand=""; for (int i=0;i