用java怎么样生成登录验证码

解决方案 »

  1.   


    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ page  import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
    <%@ page import="java.io.OutputStream" %>
    <%!
    Color getRandColor(int fc,int bc){
    Random random = new Random();
    if(fc>255) fc=255;
    if(bc>255) bc=255;
    int r=fc+random.nextInt(bc-fc);
    int g=fc+random.nextInt(bc-fc);
    int b=fc+random.nextInt(bc-fc);
    return new Color(r,g,b);
    }
    %>
    <%
    try{
    response.setHeader("Pragma","No-cache");
    response.setHeader("Cache-Control","no-cache");
    response.setDateHeader("Expires", 0);
    int width=60, height=20;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    OutputStream os=response.getOutputStream();
    Graphics g = image.getGraphics();
    Random random = new Random();
    g.setColor(getRandColor(200,250));
    g.fillRect(0, 0, width, height);
    g.setFont(new Font("Times New Roman",Font.PLAIN,18));
    g.setColor(getRandColor(160,200));
    for (int i=0;i<155;i++){
    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);
    }
    String sRand="";
    for (int i=0;i<4;i++){
    String rand=String.valueOf(random.nextInt(10));
    sRand+=rand;
    g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
    g.drawString(rand,13*i+6,16);
    }
    session.setAttribute("rand",sRand);
    g.dispose();
    ImageIO.write(image, "JPEG",os);
    os.flush();
    os.close();
    os=null;
    response.flushBuffer();
    out.clear();
    out = pageContext.pushBody();
    }catch(IllegalStateException e){
    System.out.println(e.getMessage());
    e.printStackTrace();
    }
    %>这是我现在用的,建个jsp文件贴进去。
    <img border=0 src="image.jsp">
      

  2.   

    给你个我用的servlet的
    [code=Java]ublic class ValidationCode extends HttpServlet { /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    //选择其中的字符作为验证码
    private static String codeChars="%#123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static Color getRandomColor(int minColor,int maxColor){
    Random random=new Random();
    if(minColor>255)
    minColor=255;
    if(maxColor>255)
    maxColor=255;
    int red=minColor+random.nextInt(maxColor-minColor);
    int green=minColor+random.nextInt(maxColor-minColor);
    int blue=minColor+random.nextInt(maxColor-minColor);
    return new Color(red,green,blue);
    }
    public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    int charsLength=codeChars.length();
    //关闭缓冲区
    response.setHeader("ragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);

    int width=90,height=20;
    BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    Graphics g=image.getGraphics();
    Random random=new Random();
    g.setColor(getRandomColor(180,250));//背景
    g.fillRect(0, 0, width, height);
    g.setFont(new Font("Times New Roman",Font.ITALIC,height));
    g.setColor(getRandomColor(120,180));//字体
    StringBuilder validationCode=new StringBuilder();
    String[] fontNames={"Times New Roman","Book antique","Arial"};
    for(int i=0;i<3+random.nextInt(3);i++){
    g.setFont(new Font(fontNames[random.nextInt(3)],Font.ITALIC,height));
    char codeChar=codeChars.charAt(random.nextInt(charsLength));
    validationCode.append(codeChar);
    g.setColor(getRandomColor(10,100));
    g.drawString(String.valueOf(codeChar), 16*i+random.nextInt(7),height-random.nextInt(6) );
    }
    HttpSession session=request.getSession();
    session.setMaxInactiveInterval(5*60);
    session.setAttribute("validation_code", validationCode.toString());
    g.dispose();//关闭graphic
    OutputStream os=response.getOutputStream();
    ImageIO.write(image, "JPEG", os);
    }
    }/code]
      

  3.   

    其实就是用一个servlet生成一个图片
      

  4.   

    楼上的基本可以解决问题了..我就不贴代码了..我用的是一个servlet..生成图片!
      

  5.   

    验证码?网上一堆  http://esffor.javaeye.com/blog/96467
      

  6.   

    可以用开源已经实现好的组件 jcaptcha
      

  7.   

    这个不错,是apache的。楼主可以试试。哈