1.准备一个servelt类,里面是生成图片的代码
package com.rhcy.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.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;
import javax.servlet.http.HttpSession;
public class GetRandomCode extends HttpServlet {
/**
* Constructor of the object.
*/
public GetRandomCode() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("success");
//HttpSession session = request.getSession();
int width = 60;
int height = 20;
BufferedImage image = new BufferedImage(width, height, 1);
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", 0, 24));
Color randomColor = getRandColor(160, 200);
g.setColor(randomColor);
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 = "";
String sRand = "";
for (int i = 0; i < 4; i++) {
switch (random.nextInt(2)) {
case 0: {
rand = String.valueOf(random.nextInt(10));
sRand = sRand + rand;
break;
}
case 1: {
rand = String.valueOf((char) (97 +random.nextInt(26)));
sRand = sRand + rand;
break;
}
default:break;
}
g.setColor(new Color(20 + random.nextInt(110), 20 + random .nextInt(110), 20 +random.nextInt(110)));
g.drawString(rand, 13 * i +random.nextInt(8), 15 + random .nextInt(5));
}
System.out.println("验证数字"+sRand);
request.setAttribute("session_seccodeverify", sRand);
g.dispose();
ImageIO.write(image, "PNG", response.getOutputStream());
}
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);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
// Put your code here
}
}
2. 配置文件web。xml里配置servlet配置
<servlet>
<servlet-name>GetRandomCode</servlet-name>
<servlet-class>com.rhcy.servlet.GetRandomCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetRandomCode</servlet-name>
<url-pattern>/GetRandomCode</url-pattern>
</servlet-mapping>
3. 页面代码如下
<script type="text/javascript">
function loadimage(){
var radom = Math.random()
document.getElementById("randImage").src = "GetRandomCode?"+radom;
}
function sub(){
return;
}
</script>
</tr>
<tr width="80" align="center">
<td>
验证码:
</td>
<td width="150">
<input name="yanzhengimg" type="text" id="yanzhengimg" size="20" />
</td>
<td><img alt="如果看不清楚,请点击图片重新刷新!" name="randImage" id="randImage" src="GetRandomCode" width="51" height="25" border="1" onClick="loadimage()" style="cursor:hand"></td>
</tr>