検証コードの関数:通常、システムをログインまたは登録する場合、ユーザーの動作をコンピュータープログラムの動作と区別するために、検証コードを入力する必要があります。目的は、悪意のある登録、ブルートフォースのパスワードの割れなどを防ぐことです。
検証コードを実装するためのアイデア:サーバーを使用して、数字と文字をランダムに生成する機能を実現して画像を形成し、JSPページを使用して検証コードとユーザー入力検証コードを表示する機能を実現し、サーバークラスを使用して画像とユーザーによるデータ入力を取得して、2つのデータが一貫しているかどうかを判断します。
コード実装
1.数字と英語でランダムに生成されたサーバークラスを書き込む、ソースコード:
パッケージcom; import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.image.bufferedimage; Import java.io.io.impod java.io.ioexception; Import Java.io.io.Printwriter; Import Javax.imageioioio; javax.servlet.servletexception; Import javax.servlet.servletoutputStream; Import javax.servlet.http.httpservlet; Import javax.servlet.http.httpservletrequest; Import javax.servlet.http.httpservletrespponess; import; javax.servlet.http.httpsesssion; public class logcheckはhttpservlet {public logcheck(){super(); } public void Destroy(){super.destroy(); } public void doget(httpservletrequest request、httpservletresponse応答)servletexception、ioexception {dopost(request、response); }/*コアコードの実装コード*/public void dopost(httpservletrequest request、httpservletResponse応答)Servletexception、ioException {respons.setContentType( "Image/jpeg"); httpsession session = request.getSession(); int width = 60; int height = 20; //この画像応答をキャッシュしないようにブラウザを設定します。 Response.setheader( "Cache-Control"、 "no-cache"); Response.setDateHeader( "Expires"、0); //メモリ画像を作成してグラフィックコンテキストを取得しますBufferEdImage画像= new BuffereDimage(幅、高さ、bufferedimage.type_int_rgb);グラフィックg = image.getGraphics(); /**ランダム検証コードを生成する*検証コードを定義する文字テーブル*/文字列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); } /**画像を生成*背景を描画* / g.setColor(new Color(0xDCDCDC)); G.FillRect(0、0、幅、高さ); /** 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(新しい色(赤、緑、青)); G.drawoval(x、y、1、0); } 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); g.dispose(); //画像をクライアントに貼り付けますservletoutputStream sos = respons.getOutputStream(); bytearrayoutputStream baos = new bytearrayoutputStream(); Imageio.write(image、 "jpeg"、baos); byte [] buffer = baos.tobytearray(); Response.setContentLength(buffer.length); sos.write(バッファー); baos.close(); sos.close(); session.setattribute( "checkcode"、new String(rands)); } public void init()throws servletexception {//コードをここに置く}}} 2。検証コードを表示するために使用されるページ:
<%@ page Language = "Java" Import = "Java.util。*" PageEncoding = "Utf-8"%> <%string path = request.getContextpath(); string basepath = request.getscheme()+"://"+request.getServername()+":"+request.getServerport( " - // w3c // dtd HTML 4.01 Transitional // En"> <HTML> <head> <base href = "<%= basepath%>"> <title> index </title> <meta http-equiv = "pragma" content = "no-cache"> <meta http-equiv = "cache-cach" http-equiv = "expires" content = "0"> <meta http-equiv = "keywords" content = "keyword1、keyword2、keyword3"> <meta http-equiv = "description" content = "これは私のページ"> <! - <link rel = "styleheet" type = "hed/css" <form action = "yanzheng" method = "post"> <input type = "text" name = "name" size = "5" maxlength = "4"> <a href = "index.jsp"> <img src = "logcheck"> </a> <br> <br> <inputタイプ= "
3。入力された検証コードが正しいかどうかを確認するために使用されます。
パッケージcom; import java.io.ioexception; Import java.io.printwriter; Import javax.jms.session; Import javax.servlet.servletexception; Import javax.servlet.http.httpservlet; Import javax.servlet.http.httpservletrequest; import; javax.servlet.http.httpservletResponse; Import javax.servlet.http.httpsession; Import javax.servlet.servlet.http.httpsession; public class yanzhengはhttpservletを拡張します{public yanzheng;(){super(); } public void Destroy(){super.destroy(); } public void doget(httpservletrequest request、httpservletresponse応答)servletexception、ioexception {dopost(request、response); } /*core code* / public void dopost(httpservletrequest request、httpservletresponse応答)throws servletexception、ioexception {string info = null; /*入力値を取得*/ string value1 = request.getParameter( "name"); /*画像値を取得*/ httpsession session = request.getSession(); string value2 =(string)session.getAttribute( "checkcode"); /*2つの値(文字はcase-inssensitive)を比較します*/ if(value2.equalsignorecase(value1)){info = "検証コード入力正しい"; } else {info = "検証コード入力エラー"; } system.out.println(info); request.setattribute( "info"、info); request.getRequestDispatcher( "/login.jsp")。 } public void init()throws servletexception {//コードをここに置く}}}4.入力構造インターフェイスを表示します(入力検証コードが正しいかどうか):
<%@ page Language = "Java" Import = "Java.util。*" PageEncoding = "Utf-8"%> <%string path = request.getContextpath(); string basepath = request.getscheme()+"://"+request.getServername()+":"+request.getServerport( " - // w3c // dtd HTML 4.01 Transitional // En"> <HTML> <head> <head> <base href = "<%= basepath%>"> <title>私のJSP 'login.jsp'開始ページ</title> <meta http-equiv = "pragma" content = "no-cache"> <meta http-equiv = "compers" content = "0"> <meta http-equiv = "keywords" content = "keyword1、keyword2、keyword3"> <meta http-equiv = "description" content = "this is my page"> <! href = "styles.css"> - > </head> <body> <%= request.getAttribute( "info")%> </body> </html>
5。プロジェクト構造と効果スクリーンショット:
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。