下面的代码都是抄过来的,不怎么理解,用的时候方便copy
在jsp页面写上<img src="authImg" height:20px; width:60px"/>,就会自动的将request中的authImg加进去,不知道怎么回事,应该是会自动调用的。高和宽的值也要和代码中的一致。
代码如下,这段代码会生成随机的字母与数字混合的验证码:
public class AuthImg extends HttpServlet {
private static final long serialVersionUID = -8281845659081613239L;
private Font mFont = new Font("Times New Roman", Font.PLAIN, 18);
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
ServletOutputStream out = response.getOutputStream();
int width = 60, 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));
g.fillRect(0, 0, width, height);
g.setFont(mFont);
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 rand = RandomChar.getChars(4, 4);
char c;
for (int i = 0; i < 4; i++) {
c = rand.charAt(i);
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(String.valueOf(c), 13 * i + 6, 16);
}
HttpSession seesion = request.getSession();
seesion.setAttribute("authCode", rand);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void destroy() {
}
private 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);
}
}
代码中涉及到一个类RandomChar。代码如下
public class RandomChar {
private static final String CHAR_ALL = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
private static final String CHAR_LOWERCASE = "qwertyuiopasdfghjklzxcvbnm";
private static final String CHAR_UPPERCASE = "QWERTYUIOPLAKSJDHFGZXCVBNM";
private static final String NUMBERS = "0123456789";
private static final String ALL = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
private static final String CHAR_SPECIAL_ALL = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM~!@#$%^&*";
public static final int RANDOM_CHAR_UPPERCASE = 0;
public static final int RANDOM_CHAR_LOWERCASE = 1;
public static final int RANDOM_CHAR_ALL = 2;
public static final int RANDOM_NUMBERS = 3;
public static final int RANDOM_ALL = 4;
public static final int RANDOM_SPECIAL_ALL = 5;
public static String getChars(int MOD, int count) {
Random r = new Random();
int i = 0;
String random_source = null;
switch (MOD) {
case RANDOM_CHAR_UPPERCASE:
random_source = CHAR_UPPERCASE;
break;
case RANDOM_CHAR_LOWERCASE:
random_source = CHAR_LOWERCASE;
break;
case RANDOM_CHAR_ALL:
random_source = CHAR_ALL;
break;
case RANDOM_NUMBERS:
random_source = NUMBERS;
break;
case RANDOM_ALL:
random_source = ALL;
break;
case RANDOM_SPECIAL_ALL:
random_source = CHAR_SPECIAL_ALL;
break;
default:
random_source = CHAR_SPECIAL_ALL;
break;
}
int c = random_source.length();
String result = "";
while (i < count) {
int v = Math.abs(r.nextInt()) % c;
result += random_source.substring(v, v + 1);
i++;
}
return result;
}
}