servlet代码
package Reponse;
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;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
@WebServlet("/demo7")
public class Servletdemo7 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width=100;
int height=50;
//1创建一对象,在内存中图片(验证码对象)
BufferedImage image= new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
//2美化图片
//2.1填充背景色
Graphics g = image.getGraphics();//画笔对象
g.setColor(Color.pink);
g.fillRect(0,0,width,height);
//2.2画边框
g.setColor(Color.BLUE);
g.drawRect(0,0,width-1,height-1);
String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789";
//生成随机角标
Random ran = new Random();
for (int i = 1; i <=4; i++) {
int index=ran.nextInt(str.length());
//获取字符
char ch = str.charAt(index);//随机字符
//2.3写验证码
g.drawString(ch+"",width/5*i,height/2 );
}
//2.4画干扰线
g.setColor(Color.GREEN);
//随机生成坐标点
for (int i = 0; i < 10; i++) {
int x1 = ran.nextInt(width);
int x2 = ran.nextInt(width);
int y1 = ran.nextInt(height);
int y2 = ran.nextInt(height);
g.drawLine(x1,y1,x2,y2);
}
//3将图片输出到页面展示
ImageIO.write(image,"jpg",response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
register.html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
/*
分析:点击超链接或者图片,需要换一张
1.给超链接绑定单击事件
2.重新设置图片的SRC属性值
*/
window.onload = function () {
//1.获取图片对象
var img = document.getElementById("checkcode");
var st = document.getElementById(change);
//2绑定单击事件
img.onclick=function () {
//什么东西永不重复 时间
//加时间戳 1ms
var date=new Date().getTime();
img.src="/demo7?"+date;
}
st.onclick=function () {
// st.src="/demo7?"+date;
st.href="/demo7?"+date;
}
}
</script>
</head>
<body>
<img id="checkcode" src="/demo7"/>
<a id="change" href="">看不清换一张?</a>
</body>
</html>
案例总结该案例实现简单的验证码的刷新:两个功能一个是点击图片刷新验证码
另外是通过点击超链接来刷新验证码,servlet里面主要写的是验证码的制作如何画验证码
以及干扰以及Random类的运用,是通过new BufferedImage来创造一个画笔的对象,然后在浏览器页面可以画图以及做一些其他的填充以及其他的操作
主要代码
/3将图片输出到页面展示
ImageIO.write(image,“jpg”,response.getOutputStream());
用到的一些补充的方法
一----------next(),nextInt(),nextLine()
通过上面的例子我们可以知道next(),nextInt(),nextLine()三者的读取与返回情况。
next()与nextInt()读取返回方式一致,但不同之处在于next()返回的值为字符串,但nextInt()的返回值为数值型,而nextLine()和next()返回值的类型一致,均为字符串型。
事实上,next()与nextInt()的结束标志有俩,space和enter,而nextLine()的结束标志只有enter一个。但不同的是,前两个并不会读取space和enter,无论标志在输入的句首或句末,而nextLine()则会读取enter,同样无论其位于句首或句末,然后扔掉enter并结束语句。
因此,为了避免输入的值影响后边的值,常将nextLine()写在前两者后面与前两者搭配使用。
nextInt() :接受从键盘输入的int数据
hasNextInt(); 判断是否输入为整型 int
nextLine(): 返回按 enter键 之前输入的字符
二-------java Random.nextInt()方法
public int nextInt(int n)
该方法的作用是生成一个随机的int值,该值介于[0,n)的区间,也就是0到n之间的随机int值,包含0而不包含n。
三-------Java length() 方法
length() 方法用于返回字符串的长度。
长度等于字符串中 16 位 Unicode 代码单元的数量。
四--------Java String charAt()方法
描述
此方法返回位于字符串的指定索引处的字符。该字符串的索引从零开始。
html代码里面的补充
时间戳对象
//什么东西永不重复 时间
//加时间戳 1ms
var date=new Date().getTime();
img.src="/demo7?"+date;
js补充绑定单击事件
window.onload = function () {
//1.获取图片对象
var img = document.getElementById("checkcode");
var st = document.getElementById(change);
//2绑定单击事件
img.onclick=function () {
//什么东西永不重复 时间
//加时间戳 1ms
var date=new Date().getTime();
img.src="/demo7?"+date;
}
st.onclick=function () {
// st.src="/demo7?"+date;
st.href="/demo7?"+date;
}
}