在JSP页面中动态生成图片验证码的方法实例

2023-12-07 0 1,014

在JSP页面中动态生成图片验证码

<%@ page language=\”java\” pageEncoding=\”UTF-8\”%>
<%@ page
import=\”java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*\” %>
<%@ taglib http://struts.apache.org/tags-bean\”>http://struts.apache.org/tags-bean\”
prefix=\”bean\” %>
<%@ taglib http://struts.apache.org/tags-html\”>http://struts.apache.org/tags-html\”
prefix=\”html\” %>
<%@ taglib http://struts.apache.org/tags-logic\”>http://struts.apache.org/tags-logic\”
prefix=\”logic\” %>
<%@ taglib http://struts.apache.org/tags-tiles\”>http://struts.apache.org/tags-tiles\”
prefix=\”tiles\” %>
<!DOCTYPE HTML PUBLIC \”-//W3C//DTD HTML 4.01 Transitional//EN\”>
<html:html lang=\”true\”>
<head>
<html:base />
<title>MyJsp.jsp</title>
<meta http-equiv=\”pragma\” content=\”no-cache\”>
<meta http-equiv=\”cache-control\” content=\”no-cache\”>
<meta http-equiv=\”expires\” content=\”0\”>
<meta http-equiv=\”keywords\” content=\”keyword1,keyword2,keyword3\”>
<meta http-equiv=\”description\” content=\”This is my page\”>
<!–
<link rel=\”stylesheet\” type=\”text/css\” href=\”styles.css\” rel=\”external nofollow\” >
–>
</head>
<body>
<h3>在jsp页面生成验证码</h3>
<hr/>
<%
//out.clear();
//response.setContentType(\”image/jpeg\”);//设置响应类型
//response.addHeader(\”pragma\”,\”NO-cache\”);
//response.addHeader(\”Cache-Control\”,\”no-cache\”);
//response.addDateHeader(\”Expries\”,0);
int width=400, height=30;//图片的大小(宽和高)
//构架画布,第一个参数表示画布的宽,第二个参数表示画布的高,第三个参数的含义有待确定
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();//实例化画图对象
//以下设置背景色
g.setColor(Color.yellow);
Font DeFont=new Font(\”宋体\”, Font.ITALIC, 20);
g.setFont(DeFont);
//将已经设置好的背景颜色填充到指定的画布区域
g.fillRect(0,0, width, height);
//置字体色
g.setColor(Color.blue);
int x=10,y=10,xl=550,yl=15;
g.drawLine(x,y,x+xl,y+yl);
//在画布中画椭圆形,参数为椭圆的坐标,用于确定椭圆的大小
g.drawOval(0,10,200,10);
//在画布上输出文字信息,第一个参数表示要显示的文字,第二和第三个参数表示起始点的X、Y坐标
g.drawString(\”想要输出的文字-我是陈杉\”,70,20);
g.dispose();
ServletOutputStream outStream = response.getOutputStream();
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(outStream);
encoder.encode(image);
outStream.close();
%>
</body>
</html:html>

--将该文件保存为pic.jsp,该文件负责生成图片!如果要在其他的页面显示该图片只需要写上

<img src=\”pic.jsp\”/>

仅此一句就ok了,适用于生成各种验证码!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对悠久资源网的支持。如果你想了解更多相关内容请查看下面相关链接

您可能感兴趣的文章:

  • js插件实现图片滑动验证码
  • node.js WEB开发中图片验证码的实现方法
  • JSP实用教程之简易图片验证码的实现方法(附源码)
  • 纯JS实现图片验证码功能并兼容IE6-8(推荐)
  • Nodejs中使用captchapng模块生成图片验证码
  • JS扩展Z-Blog图片验证码的单击自动刷新与评论内容自动保存
  • Node.JS用纯JavaScript生成图片或滑块式验证码功能
  • js+h5 canvas实现图片验证码
  • Js逆向实现滑动验证码图片还原的示例代码
  • JS实现图片验证码功能

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 JSP编程 在JSP页面中动态生成图片验证码的方法实例 https://www.u-9.cn/biancheng/jsp/127267.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务