实验结果如图所示:
作用是生成验证码图片
作用相当于画笔
作用是生成随机数
把生成的验证码图片作为输出流输出到界面
java生成图片代码:
//输出验证码
public class ServletResponse3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//创建变量
int width = 120 ;
int height = 30 ;
//创建一个内存图像
BufferedImage image = new BufferedImage(
width, height, BufferedImage.TYPE_INT_RGB) ;
//创建画笔
Graphics g = image.getGraphics() ;
//指定边框的颜色
g.setColor(Color.RED) ;
//画图像的边框
g.drawRect(0, 0, width, height) ;
//填充一下矩形的背景色
//设定画笔的颜色
g.setColor(Color.YELLOW) ;
//填充矩形的背景
g.fillRect(1, 1, width-2, height-2) ;
//设置字体大小
g.setFont(new Font("幼圆",Font.BOLD + Font.ITALIC,22)) ;
//填充内容
Random r = new Random() ;
//设置画笔的颜色
g.setColor(Color.GRAY) ;
//画30条干扰线
for (int i = 0; i < 30; i++) {
g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height)) ;
}
//设定画笔的颜色
g.setColor(Color.RED) ;
//随机产生4个汉字
String s = "diuvhiufshvfsd;oivjspj23932749732" ;
for (int i = 0; i < 4; i++) {
char c = s.charAt(r.nextInt(s.length())) ;
int flag = r.nextBoolean()?1:-1 ;
g.drawString(c+"", 20 + 20*i + flag*r.nextInt(5), 23 + flag * r.nextInt(5)) ;
}
//告诉客户端不要缓存图像
response.setHeader("Expires", -1 + "") ;
response.setHeader("Cache-control", "no-cache") ;
response.setHeader("Pragma", "no-cache") ;
//将图片输出到客户端
ImageIO.write(image, "jpg", response.getOutputStream()) ;
}
对应的页面代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>splash</title>
</head>
<script type=text/javascript> function change() { var img = document.getElementById("img"); //加一个无意义的参数,目的就是让地址每次都发生变化 img.src = "/servlet/servletDemo1?i="+new Date().getTime(); alert("iiii"); } </script>
<body>
<form>
姓名 <input><br><br>
年龄 <input><br><br>
验证码<input type=text name=code>
<img src="/servlet/servletDemo1" id=img ><a href=javascript:change()>看不清楚</a>
</form>
</body>
</html>
点击超链能够刷新验证码而不是使用缓存
html中的逻辑
img.src = "/servlet/servletDemo1?i="+new Date().getTime();
java中的逻辑
response.setHeader("Expires", -1 + "") ;
response.setHeader("Cache-control", "no-cache") ;
response.setHeader("Pragma", "no-cache") ;
实质就是设置响应头的属性Refresh,代码如下:
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//每隔2秒刷新自己
// response.setHeader("Refresh", "2") ;
// response.getWriter().write("aa"+ new Random().nextInt()) ;
//每隔2秒刷新到其他资源
response.setHeader("Refresh", "2;url=/servlet/servletDemo1") ;
response.getWriter().write("aa"+ new Random().nextInt()) ;
}
实质就是设置响应头Expires,代码如下:
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html;charset= UTF-8") ;
response.getWriter().write("大家好") ;
//控制缓存的时间
response.setDateHeader("Expires", System.currentTimeMillis() + 10000) ; //时间是从1970年的1月1日0点0分0秒0毫秒开始计时
}
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//第一种方式
//response.setStatus(302) ;
//response.setHeader("Location", "/servlet/servletDemo1") ;
//第二种
response.sendRedirect("/servlet/servletDemo1") ;
}