servlet中内容
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResponseDemo4 extends HttpServlet {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 120;
public static final int HEIGHT = 35;
public ResponseDemo4() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 指定图片的宽和高和其类型
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics(); // 得到一张图片
// 1、设置背景色
setBackGround(g);
// 2、设置边框
setBorder(g);
// 3、画干扰线
drawRandomLine(g);
// 4、写随机数
drawRandomNum((Graphics2D)g);
// 5、图形写给浏览器
response.setContentType("image/jpeg");
//发头控制浏览器不要缓存
response.setDateHeader("expries", -1);
response.setHeader("Catch-Control", "no-cache");
response.setHeader("Param", "no-cache");
ImageIO.write(image, "jpg", response.getOutputStream());
}
//设置背景颜色
private void setBackGround(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
}
//设置边框
private void setBorder(Graphics g) {
g.setColor(Color.BLUE);
g.drawRect(1, 1, WIDTH-2, HEIGHT-2); //前两个是左上角坐标,后两个分别是宽和高
}
//画干扰线
private void drawRandomLine(Graphics g) {
g.setColor(Color.GREEN);
for(int i = 0;i<5;i++)
{
int x1 = new Random().nextInt(WIDTH);
int y1 = new Random().nextInt(HEIGHT);
int x2 = new Random().nextInt(WIDTH);
int y2 = new Random().nextInt(WIDTH);
g.drawLine(x1, y1, x2, y2); //定位,两点确定一条直线
}
}
//产生随机数字
private void drawRandomNum(Graphics2D g) {
g.setColor(Color.RED);
g.setFont(new Font("宋体",Font.BOLD,20));
String base = "\u7684\u4e00\u4e86\u662f\u6212\u4e0d\u5728\u4eba\u4eec\u6709\u6765";
int x = 10;
for(int i=0;i<=4;i++)
{
int degree = new Random().nextInt()%30; //生成0-30的随机数
String ch =base.charAt(new Random().nextInt(base.length()))+"";
g.rotate(degree*Math.PI/180, x, 20);//设置旋转角度
g.drawString(ch, x, 20); //字符位于此图形上下文坐标系统的(x,20)位置处
g.rotate(-degree*Math.PI/180,x,20);
x=x+30;
}
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
前台接收
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function changeImage(img){
img.src = img.src+"?" + new Date().getTime(); //相当于在地址栏后面加了一个随机数,解决了缓存的问题
}
</script>
</head>
<body>
<form>
用户名:<input type="text" name = "username"><br/>
密码:<input type="password" name="password"><br/>
认证码:<input type="text" name="checkcode" >
<img src="/Day06/responseDemo4" οnclick="changeImage(this)" alt="换一张" style="cursor:hand"><br/> <!--路径写的是servlet里面对应的路径 -->
<input type="submit" value="注册">
</form>
</body>
</html>