1.前台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>管理員登陸</title>
<script type="text/javascript" language="javascript">
function change(){
var o = document.getElementById("random");
var timenow = new Date().getTime();
o.src = "<%=path%>/server/ImageValidate?d=" + timenow;
}
</script>
</head>
<body>
<img width="80px" height="30px" id="random" src="<%=path %>/server/ImageValidate"/>
<input type="button" value="換一張圖片" id="label" onclick="change()"></input>
</body>
</html>
2.後台生存隨機碼的servlet
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
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;
@SuppressWarnings("serial")
public class ImageValidate extends HttpServlet {
public ImageValidate() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
response.setContentType("image/jpeg");
response.addHeader("pragma","NO-cache");
response.addHeader("Cache-Control","no-cache");
response.addDateHeader("Expries",0);
int width=80, height=30;
response.setContentType("text/html; charset=GBK");
String chose="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char temp;//儲存隨機產生的chose元素
Random rand=new Random();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
//以下填充背景顏色
g.setColor(Color.YELLOW);
g.fillRect(0, 0, width, height);
//畫邊框
g.setColor(Color.BLACK);
Font font;
g.drawRect(0,0,width-1,height-1);
StringBuffer sRand = new StringBuffer();
for(int i=0;i<4;i++){
temp=chose.charAt(rand.nextInt(chose.length()));
sRand.append(temp);
font=new Font("Arial",Font.PLAIN,15 + rand.nextInt(10));//字型
g.setFont(font);
g.setColor(new Color(rand.nextInt(255),0,rand.nextInt(255)));//驗證碼的顏色
g.drawString(String.valueOf(temp),15*i+5,20+rand.nextInt(10));//位置
}
request.getSession().setAttribute("randomNumber",sRand.toString());//儲存產生的驗證碼值
//設定字型顏色
g.dispose();
ServletOutputStream outStream = response.getOutputStream();
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(outStream);
encoder.encode(image);
outStream.close();
}
public void init() throws ServletException {
}
}
3.xml設定檔<servlet> <servlet-name>ImageValidate</servlet-name> <servlet-class>user.ImageValidate</servlet-class> </servlet> <servlet-mapping> <servlet-name>ImageValidate</servlet-name> <url-pattern>/server/ImageValidate</url-pattern> </servlet-mapping>