前端html页面代码
1.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>1.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 change(img){
img.src = img.src+"?"+new Date().getTime();
}
</script>
</head>
<body>
<div style="height:200px"></div>
<div style="width:400px;hight:500px;margin:0 auto">
<img src="/day05/servlet/Demo2" onclick="change(this)" alt="点击换一张" style="cursor:hand">
</div>
</body>
</html>
后端servlet控制
Demo2.java
package com.cx;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
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 Demo2 extends HttpServlet {
private static final int WIDTH = 120;
private static final int HEIGHT = 30;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
//设置背景颜色
setBackgroundcolor(g);
//设置边框
setBorder(g);
//设置干扰线
setLine(g);
//设置随机文字
setChar((Graphics2D)g);
//写给浏览器
response.setContentType("image/jpeg");
ImageIO.write(image,"jpg",response.getOutputStream());
}
private void setChar(Graphics2D g) {
//设置随机文字
String base = "\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709