<div id="article_content" class="article_content csdn-tracking-statistics tracking-click" data-mod="popu_519" data-dsm="post" style="overflow: hidden;"> <div class="htmledit_views">html
<p><span style="font-size:18px;"> 在登陸頁面和各類頁面,會看到有驗證碼輸入,這樣作的目的是爲了防止密碼猜想工具破解密碼,保護了用戶密碼安全,驗證碼只能使用一次,這樣就給密碼猜想工具帶來了很大的困難,基本上阻斷了密碼猜想工具的使用。</span></p> <p><span style="font-size:18px;"> 能夠使用session得到一次性驗證碼。先看一下登陸頁面,即顯示驗證碼的頁面,代碼爲:</span></p> <p><span style="font-size:18px;"></span></p><div class="dp-highlighter bg_html"><div class="bar"><div class="tools"><b>[html]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 498px; top: 388px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-xml"><li class="alt"><span><span><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"</span><span class="tag">></span><span> </span></span></li><li class=""><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li><li class="alt"><span> <span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li><li class=""><span> <span class="tag"><</span><span class="tag-name">title</span><span class="tag">></span><span>CheckCode.html</span><span class="tag"></</span><span class="tag-name">title</span><span class="tag">></span><span> </span></span></li><li class="alt"><span> <span class="tag"><</span><span class="tag-name">meta</span><span> </span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"content-type"</span><span> </span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html; charset=UTF-8"</span><span class="tag">></span><span> </span></span></li><li class=""><span> <span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li><li class="alt"><span> <span class="tag"><</span><span class="tag-name">form</span><span> </span><span class="attribute">action</span><span>=</span><span class="attribute-value">"/learnJS/servlet/LoginFormServlet"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"post"</span><span class="tag">></span><span> </span></span></li><li class=""><span> 用戶名:<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"name"</span><span class="tag">/></span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">/></span><span> </span></span></li><li class="alt"><span> 密 碼:<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"password"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"pass"</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">/></span><span> </span></span></li><li class=""><span> 驗證碼:<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"check_code"</span><span class="tag">/></span><span> </span></span></li><li class="alt"><span> <span class="tag"><</span><span class="tag-name">img</span><span> </span><span class="attribute">src</span><span>=</span><span class="attribute-value">"/learnJS/servlet/CheckCodeServlet"</span><span class="tag">/></span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">/></span><span> </span></span></li><li class=""><span> <span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"submit"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"登陸"</span><span class="tag">/></span><span> </span></span></li><li class="alt"><span> <span class="tag"></</span><span class="tag-name">form</span><span class="tag">></span><span> </span></span></li><li class=""><span> <span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li></ol></div><pre class="html" name="code" style="display: none;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>CheckCode.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head>java
<body> <form action="/learnJS/servlet/LoginFormServlet" method="post"> 用戶名:<input type="text" name="name"/><br/> 密 碼:<input type="password" name="pass"><br/> 驗證碼:<input type="text" name="check_code"/> <img src="/learnJS/servlet/CheckCodeServlet"/><br/> <input type="submit" name="登陸"/> </form> </body> </html> </pre><br> 驗證碼存放在一張圖片上,那圖片是經過servlet產生的,在servlet中先產生驗證碼存放到session中,供之後驗證使用,而後在畫一張圖片,將驗證碼無規則的放在圖片上,在圖片上畫上干擾字符,而後就能夠啦。代碼以下:瀏覽器
<p><span style="font-size:18px;"></span></p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 496px; top: 859px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">package</span><span> com.you.servlet; </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">import</span><span> java.awt.Color; </span></span></li><li class=""><span><span class="keyword">import</span><span> java.awt.Font; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> java.awt.Graphics; </span></span></li><li class=""><span><span class="keyword">import</span><span> java.awt.image.BufferedImage; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> java.io.ByteArrayOutputStream; </span></span></li><li class=""><span><span class="keyword">import</span><span> java.io.IOException; </span></span></li><li class="alt"><span> </span></li><li class=""><span><span class="keyword">import</span><span> javax.imageio.ImageIO; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> javax.servlet.ServletException; </span></span></li><li class=""><span><span class="keyword">import</span><span> javax.servlet.ServletOutputStream; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> javax.servlet.http.HttpServlet; </span></span></li><li class=""><span><span class="keyword">import</span><span> javax.servlet.http.HttpServletRequest; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> javax.servlet.http.HttpServletResponse; </span></span></li><li class=""><span><span class="keyword">import</span><span> javax.servlet.http.HttpSession; </span></span></li><li class="alt"><span> </span></li><li class=""><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> CheckCodeServlet </span><span class="keyword">extends</span><span> HttpServlet { </span></span></li><li class="alt"><span> <span class="keyword">private</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">int</span><span> WIDTH = </span><span class="number">60</span><span>; </span></span></li><li class=""><span> <span class="keyword">private</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">int</span><span> HEIGHT = </span><span class="number">20</span><span>; </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> doGet(HttpServletRequest request, HttpServletResponse response) </span></span></li><li class="alt"><span> <span class="keyword">throws</span><span> ServletException, IOException { </span></span></li><li class=""><span> HttpSession session = request.getSession(); </span></li><li class="alt"><span> response.setContentType(<span class="string">"image/jpeg"</span><span>); </span></span></li><li class=""><span> ServletOutputStream sos = response.getOutputStream(); </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="comment">//設置瀏覽器不要緩存此圖片</span><span> </span></span></li><li class="alt"><span> response.setHeader(<span class="string">"Pragma"</span><span>, </span><span class="string">"No-cache"</span><span>); </span></span></li><li class=""><span> response.setHeader(<span class="string">"Cache-Control"</span><span>, </span><span class="string">"no-cache"</span><span>); </span></span></li><li class="alt"><span> response.setDateHeader(<span class="string">"Expires"</span><span>, </span><span class="number">0</span><span>); </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">//建立內存圖像並得到其圖形上下文</span><span> </span></span></li><li class=""><span> BufferedImage image = <span class="keyword">new</span><span> BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); </span></span></li><li class="alt"><span> Graphics g = image.getGraphics(); </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">//產生隨機的驗證碼</span><span> </span></span></li><li class=""><span> <span class="keyword">char</span><span>[] rands = generateCheckCode(); </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="comment">//產生圖像</span><span> </span></span></li><li class="alt"><span> drawBackground(g); </span></li><li class=""><span> drawRands(g, rands); </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="comment">//結束圖像的繪製過程,完成圖像</span><span> </span></span></li><li class="alt"><span> g.dispose(); </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">//將圖像輸出到客戶端</span><span> </span></span></li><li class=""><span> ByteArrayOutputStream bos = <span class="keyword">new</span><span> ByteArrayOutputStream(); </span></span></li><li class="alt"><span> ImageIO.write(image, <span class="string">"JPEG"</span><span>, bos); </span></span></li><li class=""><span> <span class="keyword">byte</span><span>[] buf = bos.toByteArray(); </span></span></li><li class="alt"><span> response.setContentLength(buf.length); </span></li><li class=""><span> sos.write(buf); </span></li><li class="alt"><span> bos.close(); </span></li><li class=""><span> sos.close(); </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="comment">//將當前驗證碼存入到session中</span><span> </span></span></li><li class="alt"><span> session.setAttribute(<span class="string">"check_code"</span><span>, </span><span class="keyword">new</span><span> String(rands)); </span></span></li><li class=""><span> </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> doPost(HttpServletRequest request, HttpServletResponse response) </span></span></li><li class=""><span> <span class="keyword">throws</span><span> ServletException, IOException { </span></span></li><li class="alt"><span> </span></li><li class=""><span> doGet(request, response); </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">private</span><span> </span><span class="keyword">char</span><span>[] generateCheckCode() { </span></span></li><li class=""><span> <span class="comment">//定義驗證碼的字符集</span><span> </span></span></li><li class="alt"><span> String chars = <span class="string">"0123456789abcdefghigklmnopqrstuvwxyz"</span><span>; </span></span></li><li class=""><span> <span class="keyword">char</span><span>[] rands = </span><span class="keyword">new</span><span> </span><span class="keyword">char</span><span>[</span><span class="number">4</span><span>]; </span></span></li><li class="alt"><span> <span class="keyword">for</span><span>(</span><span class="keyword">int</span><span> i = </span><span class="number">0</span><span>; i < </span><span class="number">4</span><span>; i++) { </span></span></li><li class=""><span> <span class="keyword">int</span><span> rand = (</span><span class="keyword">int</span><span>)(Math.random() * </span><span class="number">36</span><span>); </span></span></li><li class="alt"><span> rands[i] = chars.charAt(rand); </span></li><li class=""><span> } </span></li><li class="alt"><span> <span class="keyword">return</span><span> rands; </span></span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">private</span><span> </span><span class="keyword">void</span><span> drawRands(Graphics g, </span><span class="keyword">char</span><span>[] rands) { </span></span></li><li class="alt"><span> g.setColor(Color.BLACK); </span></li><li class=""><span> g.setFont(<span class="keyword">new</span><span> Font(</span><span class="keyword">null</span><span>,Font.ITALIC|Font.BOLD,</span><span class="number">18</span><span>)); </span></span></li><li class="alt"><span> <span class="comment">//在不一樣的高度上輸出驗證碼的每一個字符</span><span> </span></span></li><li class=""><span> g.drawString(<span class="string">""</span><span> + rands[</span><span class="number">0</span><span>], </span><span class="number">1</span><span>, </span><span class="number">17</span><span>); </span></span></li><li class="alt"><span> g.drawString(<span class="string">""</span><span> + rands[</span><span class="number">1</span><span>], </span><span class="number">16</span><span>, </span><span class="number">15</span><span>); </span></span></li><li class=""><span> g.drawString(<span class="string">""</span><span> + rands[</span><span class="number">2</span><span>], </span><span class="number">31</span><span>, </span><span class="number">18</span><span>); </span></span></li><li class="alt"><span> g.drawString(<span class="string">""</span><span> + rands[</span><span class="number">3</span><span>], </span><span class="number">46</span><span>, </span><span class="number">16</span><span>); </span></span></li><li class=""><span> System.out.println(rands); </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">private</span><span> </span><span class="keyword">void</span><span> drawBackground(Graphics g) { </span></span></li><li class=""><span> <span class="comment">//畫背景</span><span> </span></span></li><li class="alt"><span> g.setColor(<span class="keyword">new</span><span> Color(</span><span class="number">0xDCDCDC</span><span>)); </span></span></li><li class=""><span> g.fillRect(<span class="number">0</span><span>, </span><span class="number">0</span><span>, WIDTH, HEIGHT); </span></span></li><li class="alt"><span> <span class="comment">//隨機產生120個干擾點 </span><span> </span></span></li><li class=""><span> <span class="keyword">for</span><span>(</span><span class="keyword">int</span><span> i = </span><span class="number">0</span><span>; i < </span><span class="number">120</span><span>; i++) { </span></span></li><li class="alt"><span> <span class="keyword">int</span><span> x = (</span><span class="keyword">int</span><span>)(Math.random() * WIDTH); </span></span></li><li class=""><span> <span class="keyword">int</span><span> y = (</span><span class="keyword">int</span><span>)(Math.random() * HEIGHT); </span></span></li><li class="alt"><span> <span class="keyword">int</span><span> red = (</span><span class="keyword">int</span><span>)(Math.random() * </span><span class="number">255</span><span>); </span></span></li><li class=""><span> <span class="keyword">int</span><span> green = (</span><span class="keyword">int</span><span>)(Math.random() * </span><span class="number">255</span><span>); </span></span></li><li class="alt"><span> <span class="keyword">int</span><span> blue = (</span><span class="keyword">int</span><span>)(Math.random() * </span><span class="number">255</span><span>); </span></span></li><li class=""><span> g.setColor(<span class="keyword">new</span><span> Color(red, green, blue)); </span></span></li><li class="alt"><span> g.drawOval(x, y, <span class="number">1</span><span>, </span><span class="number">0</span><span>); </span></span></li><li class=""><span> } </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span>} </span></li></ol></div><pre class="java" name="code" style="display: none;">package com.you.servlet;緩存
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 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 CheckCodeServlet extends HttpServlet { private static int WIDTH = 60; private static int HEIGHT = 20;session
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中 session.setAttribute("check_code", new String(rands)); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } private char[] generateCheckCode() { //定義驗證碼的字符集 String chars = "0123456789abcdefghigklmnopqrstuvwxyz"; 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; } private void drawRands(Graphics g, char[] rands) { g.setColor(Color.BLACK); 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); System.out.println(rands); } private void drawBackground(Graphics g) { //畫背景 g.setColor(new Color(0xDCDCDC)); g.fillRect(0, 0, WIDTH, HEIGHT); //隨機產生120個干擾點 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); } }
} </pre><br> 而後是登陸以後處理用戶是否登陸成功的servlet,在這個servlet中經過比較session中存放的驗證碼和用戶輸入的驗證碼,若是匹配則進行下一步判斷,若是不匹配直接輸出驗證碼不匹配的問題。代碼爲:app
<p><span style="font-size:18px;"></span></p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 496px; top: 2914px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">package</span><span> com.you.servlet; </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">import</span><span> java.io.IOException; </span></span></li><li class=""><span><span class="keyword">import</span><span> java.io.PrintWriter; </span></span></li><li class="alt"><span> </span></li><li class=""><span><span class="keyword">import</span><span> javax.servlet.ServletException; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> javax.servlet.http.HttpServlet; </span></span></li><li class=""><span><span class="keyword">import</span><span> javax.servlet.http.HttpServletRequest; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> javax.servlet.http.HttpServletResponse; </span></span></li><li class=""><span><span class="keyword">import</span><span> javax.servlet.http.HttpSession; </span></span></li><li class="alt"><span> </span></li><li class=""><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> LoginFormServlet </span><span class="keyword">extends</span><span> HttpServlet { </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> doGet(HttpServletRequest request, HttpServletResponse response) </span></span></li><li class="alt"><span> <span class="keyword">throws</span><span> ServletException, IOException { </span></span></li><li class=""><span> response.setContentType(<span class="string">"text/html;charset=utf-8"</span><span>); </span></span></li><li class="alt"><span> PrintWriter out = response.getWriter(); </span></li><li class=""><span> </span></li><li class="alt"><span> HttpSession session = request.getSession(<span class="keyword">false</span><span>); </span></span></li><li class=""><span> <span class="keyword">if</span><span>(session == </span><span class="keyword">null</span><span>) { </span></span></li><li class="alt"><span> out.print(<span class="string">"驗證碼處理問題"</span><span>); </span></span></li><li class=""><span> <span class="keyword">return</span><span>; </span></span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> String saveCode = (String)session.getAttribute(<span class="string">"check_code"</span><span>); </span></span></li><li class=""><span> <span class="keyword">if</span><span>(saveCode == </span><span class="keyword">null</span><span>) { </span></span></li><li class="alt"><span> out.print(<span class="string">"驗證碼處理問題"</span><span>); </span></span></li><li class=""><span> <span class="keyword">return</span><span>; </span></span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> String checkCode = request.getParameter(<span class="string">"check_code"</span><span>); </span></span></li><li class=""><span> <span class="keyword">if</span><span>(!saveCode.equals(checkCode)) { </span></span></li><li class="alt"><span> out.print(<span class="string">"驗證碼無效!"</span><span>); </span></span></li><li class=""><span> <span class="keyword">return</span><span>; </span></span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> session.removeAttribute(<span class="string">"check_code"</span><span>); </span></span></li><li class=""><span> out.print(<span class="string">"驗證碼經過,服務器正在校驗用戶名和密碼!"</span><span>); </span></span></li><li class="alt"><span> </span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> doPost(HttpServletRequest request, HttpServletResponse response) </span></span></li><li class="alt"><span> <span class="keyword">throws</span><span> ServletException, IOException { </span></span></li><li class=""><span> </span></li><li class="alt"><span> doGet(request, response); </span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span>} </span></li></ol></div><pre class="java" name="code" style="display: none;">package com.you.servlet;dom
import java.io.IOException; import java.io.PrintWriter;工具
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 LoginFormServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(false); if(session == null) { out.print("驗證碼處理問題"); return; } String saveCode = (String)session.getAttribute("check_code"); if(saveCode == null) { out.print("驗證碼處理問題"); return; } String checkCode = request.getParameter("check_code"); if(!saveCode.equals(checkCode)) { out.print("驗證碼無效!"); return; } session.removeAttribute("check_code"); out.print("驗證碼經過,服務器正在校驗用戶名和密碼!"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
} </pre><br><br> </div> </div>