在jsp中怎么生成验证码啊??需要添加额外的包吗????
看了个方式!!是添加个jGraphic   那怎么做成验证码是”花“的??就是那种防止用读取像素的方法电脑自动获得验证码!!

解决方案 »

  1.   

    不要,给你一段代码吧,也不知道你要的是不是这种效果
    servlet:package com.jhc;import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Random;import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;public class YzmServlet extends HttpServlet { /**
     * Constructor of the object.
     */
    public YzmServlet() {
    super();
    } public char[] getRand() {
    char ch[] = new char[4];
    String s = "ABCDEFGHJKLMNPQRSTUVWXY3456789";
    for (int i = 0; i < 4; i++) {
    Random r = new Random();
    ch[i] = s.charAt(r.nextInt(s.length()));
    }
    return ch;
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { BufferedImage image = new BufferedImage(80, 30, 4);
    Graphics g = image.getGraphics();
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(0, 0, 80, 30);
    g.setColor(Color.blue); char ch[] = this.getRand();
    g.setFont(new Font("宋体", Font.BOLD, 20));
    g.drawString("" + ch[0], 8, 15);
    g.setColor(Color.magenta);
    g.drawString("" + ch[1], 24, 22);
    g.setColor(Color.magenta);
    g.drawString("" + ch[2], 42, 18);
    g.setColor(Color.black);
    g.drawString("" + ch[3], 56, 25);
    g.setColor(Color.black);
    Random r = new Random();
    g.drawLine(r.nextInt(60), r.nextInt(20), r.nextInt(60), r.nextInt(20));
    g.setColor(Color.green);
    g.drawLine(r.nextInt(60), r.nextInt(20), r.nextInt(60), r.nextInt(20));
    g.dispose();
    response.setContentType("image/jpeg");
    ServletOutputStream sos = response.getOutputStream();
    JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(sos);
    jpeg.encode(image);
    //request.getSession().setAttribute("yzm", new String(ch));

    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { this.doGet(request, response);
    }}jsp:
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'yzm.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <script></script>
      </head>
      
      <body>
      <form name="form1" method="post" action="" >
        <img width="56" height="29" src="YzmServlet" id="img"></td>
        </form>
      </body>
    </html>