CodeServlet.java
package org.zbq.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.OutputStream;
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;
public class CodeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
execute(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
execute(req, resp);
}
private void execute(HttpServletRequest req, HttpServletResponse resp){
resp.setHeader("Pragma", "No-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
int width = 80; //图片宽度
int height = 35; // 图片高度
//内存中创建图像
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();//创建随即类
g.setColor(Color.WHITE);//设置背景色
g.fillRect(0, 0, width, height); //填充指定矩形
g.setColor(Color.GRAY);//设置边框颜色
g.drawRect(0, 0, width - 1, height - 1);
//随机产生5条干扰线
g.setColor(Color.BLACK);
for(int i=0; i<5; i++){
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
int x2 = random.nextInt(width);
int y2 = random.nextInt(height);
g.drawLine(x1, y1, x2, y2);
}
//随机产生100个点
g.setColor(Color.GREEN);
for(int i=0;i<100;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawLine(x, y, x, y);
}
//创建字体
Font font = new Font(null, Font.ITALIC, 24);
g.setColor(Color.RED);
g.setFont(font);
int length = 4; //4位的验证码
String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
String srand = "";
for(int i=0; i<length; i++){
String ch = String.valueOf(s.charAt(random.nextInt(s.length())));
srand += ch;
g.drawString(ch, 18*i+8, (random.nextInt(5)-2)*i+25);
}
System.out.println(srand);
g.dispose();
try {
OutputStream out = resp.getOutputStream();
ImageIO.write(image, "jpeg", out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<servlet-name>codeServlet</servlet-name>
<servlet-class>org.zbq.servlet.CodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>codeServlet</servlet-name>
<url-pattern>/servlet/code</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">
$().ready(function(){
$("#change").click(function(){
$("#code").attr("src",'${pageContext.request.contextPath}/servlet/code?'+Math.random()*10);
return false;
});
});
</script>
</head>
<body>
<img alt="hello" id="code" src="<%=request.getContextPath()%>/servlet/code">
<a href="#" id="change">看不清,换一张</a>
<div id="d">123</div>
</body>
</html>