圖例如下
具體操作如下:
新建一個(gè)servlet,代碼如下:標(biāo)記一個(gè)WebServlet,
1 @WebServlet(urlPatterns = {"/checkCode"}) //驗(yàn)證碼Servlet
繪制驗(yàn)證碼圖片的核心代碼:
int width = 100; int height = 50; //創(chuàng)建圖片對象 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //美化圖片 Graphics g = image.getGraphics(); //1:填充背景 g.setColor(Color.pink); g.fillRect(0, 0, width, height); //畫邊框 g.setColor(Color.blue); g.drawRect(0, 0, width - 1, height - 1); //生成一個(gè)驗(yàn)證碼字符串 String strCheckCode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; Random random = new Random(); StrCheckCode = ""; for (int i = 1; i <= 4; i++) { int index = random.nextInt(strCheckCode.length()); char ch = strCheckCode.charAt(index); StrCheckCode += ch;//拼接驗(yàn)證碼 g.drawString(ch + "", width / 5 * i, height / 2); } //畫干擾線 for (int i = 0; i < 5; i++) { int x1 = random.nextInt(width); int x2 = random.nextInt(width); int y1 = random.nextInt(height); int y2 = random.nextInt(height); g.drawLine(x1, x2, y1, y2); } //輸出,顯示出來 ImageIO.write(image, "jpg", resp.getOutputStream()); }
本文標(biāo)題:JavaWeb制作登錄驗(yàn)證碼實(shí)現(xiàn)代碼解析-創(chuàng)新互聯(lián)
文章路徑:http://www.rwnh.cn/article44/icgee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、商城網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、做網(wǎng)站、企業(yè)網(wǎng)站制作、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容