</tr>
   <tr><td>验证码:</td><td><input type="text" name="identfiy"></td><td><img src="/servlet/ImageGerator"> <%=session.getAttribute("Login_Image_Code")%></td>
   </tr>
我想在这后面再加上个“看不清,换一张”应该怎么写程序?

解决方案 »

  1.   

    配id,用js搞img的src。然后ok.<img id="my_captcha_image" src="/servlet/ImageGerator" /><a href="#" onclick="document.getElementById('my_captcha_image').src='/servlet/ImageGerator?sed=' + new Date()">给大爷换一张养眼的</a>
      

  2.   

    ?sed=' + new Date()"> 为什么要传参数,下面是产生验证码的程序:package com.fuyou.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.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class ImageGerator extends HttpServlet {
    private static final long serialVersionUID = 5688443850114813473L; private static 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);
    } private static String charsLong = "23456789abcdefghjklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"; private static String chars = charsLong; public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { int charsLength = chars.length(); response.setHeader("ragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0); int width = 70, height = 20;
    BufferedImage image = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics(); Random random = new Random();
    g.setColor(getRandColor(200, 250));//a
    g.fillRect(0, 0, width, height);
    g.setFont(new Font("Times New Roman", Font.ITALIC, height));
    g.setColor(getRandColor(160, 200));//b for (int i = 0; i < 35; 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);
    }
    StringBuilder sRand = new StringBuilder();
    String[] fontNames = { "Times New Roman", "Arial", "Book antiqua", "" };
    for (int i = 0; i < 4; i++) {
    g.setFont(new Font(fontNames[random.nextInt(3)], Font.ITALIC,
    height));  //a
    char rand = chars.charAt(random.nextInt(charsLength));
    sRand.append(rand);
    g.setColor(new Color(20 + random.nextInt(110), 20 + random
    .nextInt(110), 20 + random.nextInt(110)));  //b
    g.drawString(String.valueOf(rand), 16 * i + random.nextInt(6) + 3,
    height - random.nextInt(4)); }
      g.setColor(getRandColor(160, 200));
    for (int i = 0; i < 30; i++) {
    int x = random.nextInt(width);
    int y = random.nextInt(height);
    int xl = random.nextInt(width);
    int yl = random.nextInt(width);
    g.drawLine(x, y, x + xl, y + yl);
    }
    request.getSession().setAttribute("Login_Image_Code", sRand.toString());
    // System.out.println(request.getSession().getAttribute("Login_Image_Code").toString());; g.dispose();
    try {
    Thread.sleep(100);
    } catch (Exception ex) {
    }

        OutputStream os = response.getOutputStream();
        ImageIO.write(image, "JPEG", os);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out
    .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    out.println("  <BODY>");
    out.print("    This is ");
    out.print(this.getClass());
    out.println(", using the POST method");
    out.println("  </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();
    }}
    是不是根据时间来判断啊?
      

  3.   

    晕,你不知道图片有缓存?
    url一样的时候有可能触发304,最后图片不变化,一般就是想偶这样在后台硬加一个不断变换的sed,保证url一直不一样,这样不让傻了吧唧的服务器以为咱们是重复请求一个静态没变的东西。唉,多说无意,兄弟自己去玩玩就明白了。