在Web程序中,驗證碼是經常使用的技術之一。Web程序永遠面臨未知用戶和未知程序的探測。為了防止惡意腳本的執行,驗證碼技術無疑是首選方案之一。本文將討論如何在JSP和Servlet中使用驗證碼技術。
驗證碼的產生思路很簡單,在Servlet中隨機產生驗證碼字符序列,並計入session中,JSP中以圖片的形式進行顯示。當用戶在JSP表單中輸入驗證碼並提交時,在相應的Servlet中驗證是否與session中保存的驗證碼一致。下面通過代碼,一次演示驗證碼產生和實現的驗證的過程。
1. 驗證碼的產生
我們需要創建一個名為ValcodeServlet的servlet並在其doGet()方法中完成驗證碼的產生。首先通過隨機數的產生類Random隨機產生一個4位的驗證碼,並將其存入session;然后使用BufferedImage和Graphics類把驗證碼轉為圖片,當然為了起到較好的效果,我們需要添加一些干擾線;最后使用ImageIO將圖片輸出。詳細代碼如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 告知瀏覽當作圖片處理 response.setContentType("image/jpeg");
// 告訴瀏覽器不緩存 response.setHeader("pragma", "no-cache"); response.setHeader("cache-control", "no-cache"); response.setHeader("expires", "0");
// 產生由4位數字構成的驗證碼 int length = 4; String valcode = ""; Random rd = new Random(); for(int i=0; i<length; i++) valcode+=rd.nextInt(10);
// 把產生的驗證碼存入到Session中 HttpSession session = request.getSession(); session.setAttribute("valcode", valcode);
// 產生圖片 int width = 80; int height = 25; BufferedImage img = newBufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
// 獲取一個Graphics Graphics g = img.getGraphics();
// 填充背景色 g.setColor(Color.WHITE); g.fillRect(0, 0, width, height);
// 填充干擾線50 for(int i=0; i<50; i++){ g.setColor(new Color(rd.nextInt(100)+155,rd.nextInt(100)+155,rd.nextInt(100)+155)); g.drawLine(rd.nextInt(width), rd.nextInt(height),rd.nextInt(width), rd.nextInt(height)); }
// 繪制邊框 g.setColor(Color.GRAY); g.drawRect(0, 0, width-1, height-1);
// 繪制驗證碼 Font[] fonts = {new Font("隸書",Font.BOLD,18),new Font("楷體",Font.BOLD,18),new Font("宋體",Font.BOLD,18),new Font("幼圓",Font.BOLD,18)}; for(int i=0; i<length; i++){ g.setColor(new Color(rd.nextInt(150),rd.nextInt(150),rd.nextInt(150))); g.setFont(fonts[rd.nextInt(fonts.length)]); g.drawString(valcode.charAt(i)+"", width/valcode.length()*i+2, 18); }
// 輸出圖像 g.dispose(); ImageIO.write(img, "jpeg", response.getOutputStream()); } |
上面的代碼只是產生了一個常規的驗證碼,我們可以根據自己的需要對驗證碼的產生策略和干擾線進行調整。Servlet編寫完畢,別忘了在web.xml中進行配置以便能在JSP中調用,其代碼如下:
<servlet> <description></description> <display-name>ValcodeServlet</display-name> <servlet-name>ValcodeServlet</servlet-name><servlet-class>org.icer.jee.valcode.servlet.ValcodeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ValcodeServlet</servlet-name> <url-pattern>/ValcodeServlet</url-pattern> </servlet-mapping> |
2. 驗證碼的顯示
產生驗證碼的servlet編寫完畢,並且已經web.xml中進行了配置,那么我們在input.jsp中使用<img />標記以圖片的方式調用servlet即可顯示驗證碼。
當然為了能起到驗證效果,本例中還包含了簡單的表單。為了放置驗證碼無法識別,此處還提供了看不清點擊換一張功能,用戶點擊圖片時重新加載驗證碼圖片(問號是為了放置瀏覽器緩存而不能實現重新請求圖片)。JSP中表單部分代碼如下:
<formname="form1"method="post"action="LoginServlet"> 驗證碼: <inputname="vcode"type="text"class="input02"id="vcode"> <imgsrc="ValcodeServlet"align="absmiddle"title="看不清,點擊換一張"onClick="this.src=this.src+'?'"/> <inputtype="submit"name="button"id="button"value=" 提交 "> </form> |
3. 實現驗證功能
當表單提交到CheckServlet時,對用戶填寫的驗證碼和session中存儲的驗證碼進行比對,根據結果給出不同提示。代碼如下:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 獲取驗證碼 String valcode = request.getSession().getAttribute("valcode").toString(); // 獲取用戶填寫的驗證碼 String vcode = request.getParameter("vcode"); // 進行驗證 if(!valcode.equals(vcode)) System.out.println(">>>驗證碼錯誤!"); else System.out.println(">>>驗證碼正確!"); } |
上面只是根據驗證情況在控制台進行了輸出,使用時根據實際的業務邏輯需求進行修改即可。
總起來說,驗證碼技術本質上就是利用Java繪圖技術把隨機產生的驗證碼字符圖形化,並在JSP中以圖形調用,最后在用戶提交表單后在對應的servlet中進行驗證。本文只是提供驗證碼的基本實現思路,希望大家能靈活應用。