后台代码:RandomImageServlet.java
package com.zhaoran.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.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.RandomStringUtils;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
@WebServlet("/randomImageServlet.action")
public class RandomImageServlet extends HttpServlet {
private static final long serialVersionUID = 1921126722013879767L;
public void doTask(HttpServletRequest request,HttpServletResponse response) throws IOException{
int len=4,width=70,height=30;
String randomStr=null;
HttpSession session=request.getSession(true);
if(randomStr==null){
randomStr=RandomStringUtils.random(len, true, false).toLowerCase();
session.setAttribute("vaildcode", randomStr);
}
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream out=response.getOutputStream();
BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics gra=image.getGraphics();
gra.setColor(new Color(228, 230, 229));
gra.fillRect(0, 0, width, height);
gra.setColor(Color.red);
gra.setFont(new Font("Lucida Sans", Font.BOLD, 20));
Random r=new Random();
gra.drawString(randomStr, 10, 20);
int red=0,green=0,blue=0;
for(int i=0;i<30;i++){
red=r.nextInt(255);
green=r.nextInt(255);
blue=r.nextInt(255);
gra.setColor(new Color(red, green, blue));
int x=r.nextInt(width);
int y=r.nextInt(height);
int x1=r.nextInt(12);
int y1=r.nextInt(12);
gra.drawLine(x, y, x+x1,y+y1);
}
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doTask(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doTask(request, response);
}
}
前台代码:index.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%>">
<script src="js/jquery-1.9.1.js"></script>
<script type="text/javascript">
function changeCode(){
$('#codeYZ').attr('src','randomImageServlet.action?'+Math.random());
}
</script>
<title>My JSP 'index.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">
</head>
<body>
<div class="div_validcode">
验证码:<input type="text" class="validcode1" name="validcode" id="validcode"/>
</div>
<div class="div_validimg">
<div>
<img src="randomImageServlet.action" alt="验证码" id="codeYZ" class="validimg" onclick="changeCode();"/>
</div>
</div>
</body>
</html>