1.生成验证码:使用的技术 jsp,servlet
2. 生成验证码的servlet:
package 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 javax.servlet.http.HttpSession;
// 在jt.jar中(jdk自带的不需要附加另一个jar)
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* User Verify Code servlet
* @author zhouhe
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SNValidateCode extends HttpServlet {
private static final long serialVersionUID = -7527932071648662604L;
private Font imgFont = new Font("SansSerif", Font.PLAIN, 16);
private final String[] code = new String[]{"2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","m","n","p","q","r","s","t","u","v","w","x","y","z"};
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request
* servlet request
* @param response
* servlet response
*/
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("image/jpeg");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/gif");
String flg = request.getParameter("flg");
String vCode = "";
int intCode = 0;
Random ran = new Random();
ran.setSeed(System.currentTimeMillis());
for(int i=0; i<4; i++){
intCode = ran.nextInt(code.length - 1);
vCode = vCode + code[intCode];
}
/*
* 绘图
*/
ServletOutputStream responseOutputStream = response.getOutputStream();
BufferedImage image = new BufferedImage(50, 20,
BufferedImage.TYPE_INT_RGB);
Graphics graph = image.getGraphics();
// 设置背景颜色
graph.setColor(Color.white);
graph.fillRect(1, 1, 48, 18);
char c;
for (int i = 0; i < 4; i++) {
int red = ran.nextInt(200);
int green = ran.nextInt(200);
int blue = ran.nextInt(200);
// 设置字体颜色
graph.setColor(new Color(red,green,blue));
graph.setFont(imgFont);
c = vCode.charAt(i);
graph.drawString(c + "", 9 * i + 6, 16);
}
JPEGImageEncoder encoder = JPEGCodec
.createJPEGEncoder(responseOutputStream);
encoder.encode(image);
// 对session赋值
HttpSession session = request.getSession();
if(null == flg){
session.setAttribute("verifyCode", vCode);
}else{
session.setAttribute("REGCode", vCode);
}
responseOutputStream.flush();
responseOutputStream.close();
}
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);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on
// the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request
* servlet request
* @param response
* servlet response
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request
* servlet request
* @param response
* servlet response
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
(web.xml文件配置省略)
3.在页面上使用
<div>
验证码:
<img id="vcodeimg1" valign="middle" name="vcodeimg1" onclick="fun_getVcode()" src="<%=request.getContextPath() %>/servlet/SNValidateCode" alt="flesh" />
<span><a href="javascript:fun_getVcode();" title="看不清?换一张">看不清?换一张</a></span>
</div>
js:
<script type="text/javascript">
function fun_getVcode(){
var timenow = new Date().getTime();
document.getElementById("vcodeimg1").src = "<%=request.getContextPath() %>/servlet/SNValidateCode?flg="+timenow;
}
</script>