//srevlet
package com;
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.io.PrintWriter;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.imageio.spi.ImageInputStreamSpi;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.org.apache.commons.collections.Buffer;
//输出一张随机图片
public class ResponseDemo4 extends HttpServlet {
public static final int width=120;
public static final int height=25;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics graphics=image.getGraphics();
//1.设置背景色
setBackGround(graphics);
//2.设置边框
setBorder(graphics);
//3.画干扰线
drawRandomLine(graphics);
//4.写随机数
drawRandomNum((Graphics2D)graphics);
//5.图像写给浏览器
response.setContentType("image/jpeg");
//控制浏览器不要缓存
response.setDateHeader("expries", -1);
ImageIO.write(image,"jpg", response.getOutputStream());
}
private void drawRandomNum(Graphics2D graphics) {
// TODO Auto-generated method stub
graphics.setColor(Color.RED);
graphics.setFont(new Font("宋体", Font.BOLD, 20));
String base="0123456789";
int x=10;
for (int i=0;i<4;i++){
int degree=new Random().nextInt()%30;
String ch=base.charAt(new Random().nextInt(base.length()))+"";
graphics.rotate(degree*Math.PI/180,x,20);
graphics.drawString(ch, x,20);
graphics.rotate(-degree*Math.PI/180,x,20);
x+=30;
}
}
private void drawRandomLine(Graphics graphics) {
// TODO Auto-generated method stub
graphics.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(height);
graphics.drawLine(x1, y1, x2, y2);
}
}
private void setBorder(Graphics graphics) {
// TODO Auto-generated method stub
graphics.setColor(Color.BLUE);
graphics.drawRect(1, 1, width-2, height-2);
}
private void setBackGround(Graphics graphics) {
// TODO Auto-generated method stub
graphics.setColor(Color.WHITE);
graphics.fillRect(0,0, width, height);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
//jsp
<!DOCTYPE html>
<html>
<head>
<title>register.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function changeImage(img){
img.src=img.src+'?'+new Date().getTime();
}
</script>
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<!--This is my HTML page. <br>-->
<form action="">
用户名:<input type="text" name="username"><br>
密 码:<input type="password" name="password"><br>
验证码:<input type="text" name="checkcode">
<img src="/day06/servlet/ResponseDemo4" οnclick="changeImage(this)" alt="换一张" style="cursor:hand"><br>
<input type="submit" value="注册">
</form>
</body>
</html>