Asal usul kode verifikasi
Dalam pengembangan proyek web, untuk mencegah beberapa orang menggunakan alat otomatis (seperti mesin registrasi otomatis) untuk memproses data batch, kode verifikasi ditambahkan untuk memverifikasi dalam node fungsional yang berbeda untuk mencapai efek memblokir perangkat lunak otomatis
Aplikasi yang paling klasik adalah: kode verifikasi grafis pendaftaran situs web; Selanjutnya, melalui teknologi Java, dikombinasikan dengan Servlet, program kode verifikasi grafis yang diperlukan untuk pendaftaran situs web diimplementasikan, untuk referensi Anda.
2. Menerapkan efek kode verifikasi grafis dari halaman pendaftaran
1. Buat proyek web: java_servlet_verifyimg
2. Buat pengontrol yang secara otomatis menghasilkan kode verifikasi grafik - verifikasiImgServer
Paket com.phome.util; impor java.awt.color; impor java.awt.font; impor java.awt.graphics2d; impor java.awt.image.bufferedImage; impor java.io.ioexception; impor java.util.random; impor javax.servlet; javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.jpegimageencoder; kelas publik VerifyImgServlet memperluas httpservlet { / ** * * / private static final long serialVersionuid = 1l; // Tetapkan kamus karakter acak. 0,o,1,I, etc. public static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; Publik statis acak acak = acak baru (); // Objek nomor acak public static string getRandomString () {stringBuffer buffer = new StringBuffer (); // STRING cache untuk (int i = 0; i <6; i ++) // enam loop untuk mendapatkan karakter {buffer.append (chars [random.nextint (chars.length)]); // Ambil satu karakter secara acak pada satu waktu} return buffer.toString (); } public static color getRandomColor () {return new color (random.nextint (255), random.nextint (255), random.nextint (255)); } public static color getReversecolor (Color C) {return new color (255 - c.getred (), 255 - c.getgreen (), 255 - c.getBlue ()); } public static color getReversecolor (Color C) {return new color (255 - c.getred (), 255 - c.getgreen (), 255 - c.getBlue ()); } public void doGet (permintaan httpservletRequest, respons httpservletResponse) melempar servletException, ioException {response.setContentType ("gambar/jpeg"); // atur tipe output untuk tidak menghilangkan string randomstring = getRandomString (); // Panggil metode untuk menghasilkan string acak untuk mendapatkan dan menerima permintaan string acak.getSession (true) .setAttribute ("acak", acak); // Simpan string di Sesi Int Width = 100; // Lebar gambar Int tinggi = 30; // Warna Tinggi Gambar = GetRandomColor (); // Dapatkan warna acak untuk latar belakang warna terbalik = getReversecolor (warna); // Balikkan warna untuk foreground warna bufferedImage Bi = BufferedImage baru (lebar, tinggi, bufferedimage.type_int_rgb); // Buat gambar warna grafis2d g = bi.creategraphics (); // Dapatkan objek gambar g.setFont (font baru (font.sans_serif, font.bold, 16)); // atur font g.setColor (warna); // atur warna g.fillrect (0, 0, lebar, tinggi); // gambar latar belakang g.setColor (terbalik); // atur warna g.drawstring (randomstring, 18, 20); // Gambar karakter acak untuk (int i = 0, n = random.nextInt (100); i <n; i ++) // Gambar hingga 100 titik noise {g.drawrect (acak.nextint (lebar), acak.nextint (tinggi), 1, 1); // poin noise acak} serveLetoutputStream out = response.getoutputStream (); // Tampaknya mendapatkan aliran output JPEGIMAGEENCODER encoder = jpegcodec.createJpegencoder (out); // encoder encoder.encode (BI); // mengkode gambar out.flush (); // output ke klien} public void dopost (permintaan httpservletRequest, respons httpservletResponse) melempar servletException, ioException {this.doget (permintaan, respons); }}3. Buat pengontrol register - regisServlet
Paket com.phome.servlet; import java.io.ioException; import javax.servlet.servletException; import javax.servlet.http.httpservlet; import javax.servlet.htp.htpservletrequest; impor javax.servlet.htp.htpservletrequest; impor javax.servlet.htp.htppservletRequest; impor javax.servlet javax.servlet.http.httpservletResponse; import javax.servlet.http.httpsession;/** * Pengontrol pendaftaran * @Author zuoyi * */kelas publik RegisServlet memperluas httpservlet {/** * */private static long voling final longvony = 1LVONE FINAL; @Override Protected void doGet (httpservletRequest req, httpservletResponse resp) melempar servletException, ioException {this.dopost (req, resp); } @Override void dopost yang dilindungi (httpservletrequest req, httpservletresponse resp) melempar servletException, ioException {// Dapatkan kode verifikasi acak dari sesi httpsesion = req.getSesion (); String randomString = (string) session.getAttribute ("randomEstring"); // Dapatkan Kode Verifikasi Input Pengguna String InputRandomString = Req.GetParameter ("Randomstr"); // Pastikan kode verifikasi dilewati, dan daftarkan itu mensimulasikan jika (acak. } else {req.setAttribute ("resInfo", "Kode verifikasi dimasukkan secara tidak benar, silakan periksa dan daftar lagi!"); } // Jika pendaftaran berhasil atau gagal, lompat ke halaman hasil.jsp untuk melihat hasil pendaftaran. . . req.getRequestDispatcher ("result.jsp"). Forward (req, resp); }}4. Konfigurasikan servlet
<? Xml Version = "1.0" encoding = "utf-8"?> <web-app xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xmlns =" http://java.com/xmlns = "http://java.com/xmlns = XSI: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/we-pappe." "" "" "" "" "" "" "" "" "" <play-name> simg </play- <Rerl-Pattern> /Regist.Action </RURL-PATERS> </SERVLET-MAPPING> <!-Mengkonfigurasi kode verifikasi grafik Servlet-> <servlet> <servlet-name> verifyImg </servlet-name> <servlet-class> com.phome.servlet.verifyImget <servlet-name> verifikasiImg </servlet-name> <rerl-pola> /verifyImg.action </rerl-pattern> </servlet-mapping> <creendak-file-file> <creendy-filte> index.html </creendy-file> <creendak-file> index.htm </welcome-file> <creendy-file> </file-file> index.htm </welcome-file> <creendy-file> </file-file> index.htm </welcome-file> <creading-file-file> </FILE-File> index. <creendaking-filte> default.htm </celcome-file> <creendak-file> default.jsp </celcome-file> </celcome-file-list> </web-app>
5. Buat halaman tes tampilan pendaftaran - regist.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "GB18030"%> <%string path = request.getContextPath (); string basepath = request.getscheme ()+":/"+request.getServername ()+":"+miring. "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" > <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "kedaluwarsa" konten = "0"> <meta http-equiv = "kata kunci" konten = "kata kunci1, kata kunci2, koki"> <meta http http-equiv = "koki" ini = "ini" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" > --> </head> <body> <form action="${pageContext.request.contextPath}/regist.action" method="post"> Username: <input type="text" name="username"/> <br /> Password: <input type="text" name="password"/> <br /> Please enter the verification code to register: <img src="${pageContext.request.contextPath }/verifyimg.action"/> <input type="text" name="randomStr"/> <br /> <input type="submit" value="regist"/> </form> </body></html> 5.1 Buat Halaman Hasil Pendaftaran - Hasil.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "GB18030"%> <%string path = request.getContextPath (); string basepath = request.getscheme ()+":/"+request.getServername ()+":"+miring. "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" > <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "kedaluwarsa" konten = "0"> <meta http-equiv = "kata kunci" konten = "kata kunci1, kata kunci2, koki"> <meta http http-equiv = "koki" ini = "ini" type = "text/css" href = "styles.css" rel = "eksternal nofollow" rel = "eksternal nofollow"> -> </head> <body> $ {requestscope.resinfo} </body> </html>6. Tes
(1) a. Buka browser dan masukkan alamat uji: http: // localhost: 8080/java_servlet_verifyimg; Halaman yang ditunjukkan pada gambar di bawah ini muncul
B. Masukkan akun pendaftaran, kata sandi dan kode verifikasi dan klik regis untuk mengirimkan
C. Hasil tes, lompat ke halaman pendaftaran transfer, mendorong bahwa pendaftaran berhasil
(2) Buka halaman pendaftaran
Masukkan kode pendaftaran kesalahan
Halaman hasil tes
lebih!
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.