jsp生成好看的验证码
这是一个Servlet,名字是ImageServlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | package a; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; 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; import javax.servlet.http.HttpSession; public class ImageServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding( "utf-8" ); BufferedImage bfi = new BufferedImage(80,25,BufferedImage.TYPE_INT_RGB); Graphics g = bfi.getGraphics(); g.fillRect(0, 0, 80, 25); //验证码字符范围 char [] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" .toCharArray(); Random r = new Random(); int index; StringBuffer sb = new StringBuffer(); //保存字符串 for ( int i=0; i<4; i++){ index = r.nextInt(ch.length); g.setColor( new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); Font font = new Font( "宋体" , 30, 20); g.setFont(font); g.drawString(ch[index]+ "" , (i*20)+2, 23); sb.append(ch[index]); } // 添加噪点 int area = ( int ) (0.02 * 80 * 25); for ( int i=0; i<area; ++i){ int x = ( int )(Math.random() * 80); int y = ( int )(Math.random() * 25); bfi.setRGB(x, y, ( int )(Math.random() * 255)); } //设置验证码中的干扰线 for ( int i = 0; i < 6; i++) { //随机获取干扰线的起点和终点 int xstart = ( int )(Math.random() * 80); int ystart = ( int )(Math.random() * 25); int xend = ( int )(Math.random() * 80); int yend = ( int )(Math.random() * 25); g.setColor(interLine(1, 255)); g.drawLine(xstart, ystart, xend, yend); } HttpSession session = request.getSession(); //保存到session session.setAttribute( "verificationCode" , sb.toString()); ImageIO.write(bfi, "JPG" , response.getOutputStream()); //写到输出流 } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } private static Color interLine( int Low, int High){ if (Low > 255) Low = 255; if (High > 255) High = 255; if (Low < 0) Low = 0; if (High < 0) High = 0; int interval = High - Low; int r = Low + ( int )(Math.random() * interval); int g = Low + ( int )(Math.random() * interval); int b = Low + ( int )(Math.random() * interval); return new Color(r, g, b); } } |
接下来是一个jsp文件,先访问jsp文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>Insert title here</title> </head> <script type= "text/javascript" > function reload(){ document.getElementById( "image" ).src= "<%=request.getContextPath() %>/imageServlet?date=" + new Date().getTime(); $( "#checkcode" ).val( "" ); // 将验证码清空 } function verificationcode(){ var text=$.trim($( "#checkcode" ).val()); $.post( "${pageContext.request.contextPath}/verificationServlet" ,{op:text},function(data){ data=parseInt($.trim(data)); if (data>0){ $( "#span" ).text( "验证成功!" ).css( "color" , "green" ); } else { $( "#span" ).text( "验证失败!" ).css( "color" , "red" ); reload(); //验证失败后需要更换验证码 } }); $( "#checkcode" ).val( "" ); // 将验证码清空 } </script> <body> 验证码:<input type= "text" name= "checkcode" id= "checkcode" /> <img src= "<%=request.getContextPath() %>/ImageServlet" alt= "验证码" id= "image" /> <a href= "reload();" ><label>换一张</label></a><br> <input type= "button" value= "提交" onclick= "verificationcode();" > <span id= "span" ></span> </body> </html> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)