當前位置:名人名言大全網 - 短信平臺 - JSP實現驗證碼 代碼怎麽寫?

JSP實現驗證碼 代碼怎麽寫?

<%@pagecontentType=\"image/jpeg\"import=\"java.awt.*,java.awt.image.*,java.util.*,javax.imageio.* \"%> <%! //createbysmallnest //email:smallnest@gmail.com //website: www.kuaff.com //生成隨機顏色 ColorgetRandColor(Randomrandom,intfc,intbc)...{ if(fc>255)fc=255; if(bc>255)bc=255; intr=fc+random.nextInt(bc-fc); intg=fc+random.nextInt(bc-fc); intb=fc+random.nextInt(bc-fc); returnnewColor(r,g,b); } %> <% //設置頁面不緩存 response.setHeader(\"Pragma\",\"No-cache\"); response.setHeader(\"Cache-Control\",\"no-cache\"); response.setDateHeader(\"Expires\",0); //設置圖片的長寬 intwidth=176,height=30; //設置備選漢字,剔除壹些不雅的漢字 Stringbase=\"的壹了是我不在人們有來他這上著個地到大裏說就去子得也和那要下看天時過出小麽起妳都把好 還多沒為又可家學只以主會樣年想生同老中十從自面前頭道它後然走很像見兩用她國動進成回什邊作對開而己 些現山民候經發工向事命給長水幾義三聲於高手知理眼誌點心戰二問但身方實吃做叫當住聽革打呢真全才四已 所敵之最光產情路分總條白話東席次親如被花口放兒常氣五第使寫軍吧文運再果怎定許快明行因別飛外樹物活 部門無往船望新帶隊先力完卻站代員機更九您每風級跟笑啊孩萬少直意夜比階連車重便鬥馬哪化太指變社似士 者幹石滿日決百原拿群究各六本思解立河村八難早論嗎根***讓相研今其書坐接應關信覺步反處記將千找爭領或 師結塊跑誰草越字加腳緊愛等習陣怕月青半火法題建趕位唱海七女任件感準張團屋離色臉片科倒睛利世剛且由 送切星導晚表夠整認響雪流未場該並底深刻平偉忙提確近亮輕講農古黑告界拉名呀土清陽照辦史改歷轉畫造嘴 此治北必服雨穿內識驗傳業菜爬睡興形量咱觀苦體眾通沖合破友度術飯公旁房極南槍讀沙歲線野堅空收算至政 城勞落錢特圍弟勝教熱展包歌類漸強數鄉呼性音答哥際舊神座章幫啦受系令跳非何牛取入岸敢掉忽種裝頂急林 停息句區衣般報葉壓慢叔背細\"; [Page] //備選漢字的長度 intlength=base.length(); //創建內存圖像 BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //獲取圖形上下文 Graphicsg=image.getGraphics(); //創建隨機類的實例 Randomrandom=newRandom(); //設定圖像背景色(因為是做背景,所以偏淡) g.setColor(getRandColor(random,200,250)); g.fillRect(0,0,width,height); //備選字體 String[]fontTypes=...{ \"宋體\",\"新宋體\",\"黑體\",\"楷體\",\"隸書\"}; intfontTypesLength=fontTypes.length; //在圖片背景上增加噪點 g.setColor(getRandColor(random,160,200)); g.setFont(newFont(\"TimesNewRoman\",Font.PLAIN,14)); for(inti=0;i<6;i++)...{ g.drawString(\"*********************************************\",0,5*(i+2)); } //取隨機產生的認證碼(6個漢字) //保存生成的漢字字符串 StringsRand=\"\"; for(inti=0;i<6;i++)...{ intstart=random.nextInt(length); Stringrand=base.substring(start,start+1); sRand+=rand; //設置字體的顏色 g.setColor(getRandColor(random,10,150)); //設置字體 g.setFont(newFont(fontTypes[random.nextInt(fontTypesLength)],Font.BOLD,18+random.nextInt(6))); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 很多新手對圖片驗證碼不是很了解,今天我們就來看看壹個實例吧,首先,先寫壹個動態圖片生成的Servlet類,如下: package org.yuanliang; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class RandomCode extends HttpServlet { private static final long serialVersionUID = 1L; private static int WIDTH = 65;//設置圖片的寬度 private static int HEIGHT = 22;//設置圖片的高度 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); response.setContentType(\"image/jpeg\"); ServletOutputStream sos = response.getOutputStream(); response.setHeader(\"Pragma\", \"No-cache\"); response.setHeader(\"Cache-Control\", \"no-cache\"); response.setDateHeader(\"Expires\", 0); BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); char[] rands = generateCheckCode(); drawBackground(g); drawRands(g, rands); g.dispose(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(image, \"JPEG\", bos); byte[] buf = bos.toByteArray(); response.setContentLength(buf.length); sos.write(buf); bos.close(); sos.close(); session.setAttribute(\"randomCode\", new String(rands)); } private void drawBackground(Graphics g) { g.setColor(new Color(0xDCDCDC)); g.fillRect(0, 0, WIDTH, HEIGHT); for (int i = 0; i < 120; i++) { int x = (int) (Math.random() * WIDTH); int y = (int) (Math.random() * HEIGHT); int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random() * 255); g.setColor(new Color(red, green, blue)); g.drawOval(x, y, 1, 0);[Page] } } private void drawRands(Graphics g, char[] rands) { // g.setColor(Color.BLUE); Random random = new Random(); int red = random.nextInt(110); int green = random.nextInt(50); int blue = random.nextInt(50); g.setColor(new Color(red, green, blue)); g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18)); g.drawString(\"\" + rands[0], 1, 17); g.drawString(\"\" + rands[1], 16, 15); g.drawString(\"\" + rands[2], 31, 18); g.drawString(\"\" + rands[3], 46, 16); } private char[] generateCheckCode() { String chars = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\"; char[] rands = new char[4]; for (int i = 0; i < 4; i++) { int rand = (int) (Math.random() * 36); rands[i] = chars.charAt(rand); } return rands; } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } } OK!!!保存為RandomCode.java吧,然後進行編譯,生成RandomCode.class文件,放到WEB-INF/classes/org/yuanliang目錄下; 然後在WEB-INF目錄下建立web.xml,添加Servlet配置: jsp驗證碼生成以及刷新 關於jsp驗證碼生成,顯示,驗證和刷新: 1:四位數據的驗證圖片生成,用servlet實現 2: 在web.xmlli配置servlet 3:頁面上顯示servlet生成的圖片 4: 關於驗證碼輸入數據的驗證 5: 驗證碼的刷新 ——————————————————————– 1:四位數據的驗證圖片生成,用servlet實現,servlet代碼如下: import java.io.*; import javax.servlet.http.*; import javax.servlet.*; import java.util.*; import java.awt.*; import java.awt.image.*; import javax.imageio.*; public class vImage extends HttpServlet { public void init(ServletConfig conf) throws ServletException { super.init(conf); } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType(”image/jpeg”); res.setHeader(”Pragma”, “No-cache”); res.setHeader(”Cache-Control”, “no-cache”); res.setDateHeader(”Expires”, 0); HttpSession session = req.getSession(); // 在內存中創建圖象 int width = 60, height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 獲取圖形上下文 Graphics g = image.getGraphics(); // 生成隨機類 Random random = new Random(); // 設定背景色 g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); // 設定字體 g.setFont(new Font(”Times New Roman”, Font.PLAIN, 18)); // 畫邊框 //g.setColor(new Color()); //g.drawRect(0,0,width-1,height-1); // 隨機產生155條幹擾線,使圖象中的認證碼不易被其它程序探測到 g.setColor(getRandColor(160, 200)); for (int i = 0; i 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } } 2: 在web.xmlli配置servlet: vImage /vimage 註:註意元素在web.xml文件裏的位置 3:頁面上顯示servlet生成的圖片: 註:在需要顯示驗證碼的地方加上這句,name在刷新驗證碼的時候會用到。 4: 關於驗證碼輸入數據的驗證: 驗證碼生成時存入了session,就是servlet裏的這句session.setAttribute(”post_validate_code”, sRand);具體驗證就不多說了。 5: 驗證碼的刷新 刷新驗證碼 function reloadImage(url) { document.form名字.pic.src = url; } 前兩個有時間參考 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////