1. 기본 프로세스 :
확인 코드 페이지 (매우 작은) → 양식에 포함됩니다 → 클릭하여 페이지를 새로 고치려면 → 양식이 제출 된 시점 확인.
2. 방법 :
1. 고문을 정의하고 드로잉 방법을 구현하십시오
패키지 com.zhuguang.action; import java.awt.color; Java.awt.Font 가져 오기; java.awt.graphics 가져 오기; java.awt.image.bufferedimage import; java.util.map import; import javax.servlet.http.httpservletresponse; import org.apache.struts2.interceptor.servletresponseaware; org.apache.struts2.interceptor.sessionaware import; com.opensymphony.xwork2.actionSupport 가져 오기; import com.sun.image.codec.jpeg.jpegcodec; import com.sun.image.codec.jpeg.jpegimageencoder; 공개 클래스 검정은 ActionSupport Ampess SessionAware, ServletReponseaware {private map <String, Object> Session; 개인 httpservletResponse 응답; 개인 정적 최종 최종 긴 SerialversionUID = 1L; 개인 문자열 chknumber; @override public string execute ()는 예외 {response.setheader ( "캐시-콘트롤", "no-cache"); int 너비 = 50; // 이미지 너비 int 높이 = 20; // 이미지 높이 BufferedImage Image = New BufferedImage (너비, 높이, BufferedImage.type_int_rgb); 그래픽 그래픽 = image.creategraphics (); Graphics.setColor (this.getColor ()); // 배경색 그래픽 .fillRect (0, 0, 너비, 높이); Graphics.setFont (New Font ( "Arial", font.bold, 18)); Graphics.setColor (this.getColor ()); // 단어 문자열의 색상 숫자 = string.valueof (System.CurrentTimeMillis ()%9000+1000); // 4 비트 랜덤 숫자 세션을 생성합니다. // 세션 graphics.DrawString (숫자, (int) (너비*0.1), (int) (높이*0.8)에 씁니다. 그래픽 .dispose (); jpegimageEncoder encoder = jpegcodec.createjpegencoder (response.getOutputStream ()); encoder.encode (이미지); response.getOutputStream (). flush (); // 이미지 응답을 생성하기 위해 페이지로 새로 고침 .getoutPutStream (). cose (); // 작가를 닫습니다. } private color getColor () {int red = (int) (math.random ()*1000%256); int green = (int) (math.random ()*1000%256); int blue = (int) (math.random ()*1000%256); 새로운 색상 (빨간색, 녹색, 파란색)을 반환합니다. } public String getchknumber () {return chknumber; } public void setchknumber (String chknumber) {this.chknumber = chknumber; } @override public void setsession (map <string, object> session) {// todo 자동 생성 메소드 스터브 this.session = session; } @override public void setsession (map <string, object> session) {// todo 자동 생성 메소드 스터브 this.session = session; } @override public void setservletResponse (httpservletResponse 응답) {// todo 자동 생성 메소드 스터브 this.response = response; }}세션 및 응답 사용에주의하십시오
2. struts.xml 파일로 등록 :
<action name = "randomcode"> </action>
페이지가 리디렉션되지 않도록 정보가 반환되지 않습니다.
3. JSSP 페이지 쓰기
<%@ page language = "java"contenttype = "text/html; charset = utf-8"pageencoding = "utf-8"%> <%@ taglib uri = "/struts-tags"prefix = "s"%> <%string path = request.getContextPath (); String BasePath = request.getScheme ()+": //"+request.getServerName ()+":"+request.getServerport ()+path+"/"; %> <! docType html public "-// w3c // dtd html 4.01 Transitional // en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-6. <script type = "text/javaScript"> <!- 함수 repoadCode (obj, base) {var rand = new date (). gettime (); // 현재 시간은 URL의 매개 변수로 사용되어 URL을 변경하여 확인 코드가 동적으로로드됩니다. // 단지 간섭 효과 일뿐 아니라 의미가 없지만 매우 영리합니다. haha obj.src = base+"randomcode.action? abc ="+rand; // 실제로 서버 측에 ABC 필드가 없습니다. } //-> </script> <title> 테스트 페이지 </title> </title> </head> <body> <form action = "testLogin"메소드 = "post"> username <input type = "text"name = "name"> <br> 암호 <입력 유형 = "text"name = "passwor"> <br> verification code : <input type = "ind"id "icknumber" "chnambumer. src = "<%= basepath%> randomcode.action"id = "safecode"onclick = "ReloadCode ( '<%= basepath%>')"/> <br> <input type = "value ="login "> </body> </html>4. 확인
(1) 검증 방법을 작동시킵니다
public String testLogin () {if (session.get ( "randomcode"). Equals (chknumber)) {return success; } else {return error; }}(2) struts.xml에 등록하십시오
<action name = "testLogin"method = "testLogin"> <result name = "success"> success.jsp </result> <result name = "error"> error.jsp </result> </action>
위의 것은 편집기가 동적 검증 코드를 생성하고 예제 코드를 확인하기 위해 소개 한 struts2 구현입니다. 나는 그것이 모두에게 도움이되기를 바랍니다!