在小程序应用中,,,,,我们会遇到随机验证码图片,,,,,那么怎样使用代码实现一个简朴的天生随机验证码的小程序

//随机天生验证码
//第一步:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test(response);
}
private void test(HttpServletResponse response) throws IOException {
int width = 120,height=25;
//天生一张图片 此时获得一张宽120,,,,,长25的一张玄色图片
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics grap = img.getGraphics();//获得一个画笔
//填充配景致
grap.setColor(Color.pink);
//设置填充的区域
grap.fillRect(1, 1, width-2, height-2);
//设置边框的颜色 同填充配景颜色 靠近谁是设置哪个属性的颜色
grap.setColor(Color.red);
grap.drawRect(0, 0, width-1, height-1);
//设置字体
grap.setFont(new Font("黑体", Font.BOLD|Font.ITALIC, 18));
//向图片上写字 嘿嘿随机天生了字符串
Random r = new Random();
int p = 15;
for(int i=1;i<=4;i++)
{
grap.drawString(r.nextInt(10)+"", p,20);
p+=15;
}
//向图片上画线
for(int i=1;i<=10;i++)
{
grap.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
}
//把图片发送给客户端
ImageIO.write(img, "jpg", response.getOutputStream());
}
//第二部:新建login.html
<!DOCTYPE html>
<html>
<head>
<title>login.html</title>
<script type="text/javascript">
function ff(){
var img = document.getElementById("image");
img.src="/day33_response/demo4?user=1"+new Date().getTime();
// img.setAttribute("src","/day33_response/demo4?user="+new Date().getTime());
}
</script>
</head>
<body>
<form action="#" method="get">
用户名<input type="text" name="uname"><br/><br/>
密 码<input type="password" name="pwd"><br/><br/>
验证码<input type="text" name="code">
<!-- 若是image没有写src页面刚加载时没有工具,,,,,刷新之后才会显示验证码图片 -->
<img id="image" src='/day33_response/demo4'>
<!-- 换两行 -->
<a href="javascript:ff()">换一张</a><br/> <br/>
<input type="submit" value="提交">
</form>
</body>
</html>
// 大功告成绩可以宣布到tomcat上浏览了
//此处介绍一种懒人要领,,,,,在doGet要领中,,,,,其中ValidateCode四个函数划分是矩形的宽、高以及验证码的个数和滋扰线的条数,,,,,然后第一步的函数就可一切省略了
ValidateCode code = new ValidateCode(320, 25, 4, 8);
code.write(response.getOutputStream());
//注:导响应的ValidateCode的JAR包喔。。
KESION pp电子软件
KESION pp电子软件是海内领先的在线教育软件及私域社交电商软件服务提供商,,,,,恒久专注于为企业提供在线教育软件及社交电商SaaS平台解决方案。。
公司焦点产品云开店SaaS社交电商服务平台、在线教育SaaS服务平台、教育企业数字化SaaS云平台、企微营销助手、私有化自力安排品牌网校和在线教育咨询等。。KESION 一直通过手艺立异,,,,,提供产品和服务,,,,,助力企业向数字化转型,,,,,通过科技驱动商业刷新,,,,,让商业变得更智慧!
日历签到是小程序中常用的技巧,,,,,下面为各人介绍怎样实现小程序日历签到。。...
微信宣布通告称,,,,,将持续整理违规小程序。。将重点关注四大类违规小程序:“赝品、高仿” 类、“色情、低俗”类、违规“现金贷”小程序以及违规门店小程序。。...