码迷,mamicode.com
首页 > 其他好文 > 详细

用struts2做一个带有图片效果的登陆验证码

时间:2016-12-03 18:28:21      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:ring   rac   get   except   文件   exp   dea   exception   ini   

    我们在登陆网站的时候总是会有带有图片验证功能的验证码,它是怎么做出来的了,今天我就详细的将每一步步骤写出来。

1.在action层

技术分享
 1 package cn.itcast.javaee.js.checkcode;
 2 
 3 import java.io.PrintWriter;
 4 import javax.servlet.http.HttpServletResponse;
 5 import org.apache.struts2.ServletActionContext;
 6 import com.opensymphony.xwork2.ActionContext;
 7 import com.opensymphony.xwork2.ActionSupport;
 8 
 9 /**
10 * 验证码检查
11 * @author AdminTC
12 */
13 public class CheckcodeAction extends ActionSupport{
14 //客户端验证码
15 private String checkcode;//2525
16 //注入客户端验证码
17 public void setCheckcode(String checkcode) {
18 this.checkcode = checkcode;
19 }
20 /**
21 * 验证
22 */    
23 public String check() throws Exception {
24 //图片路径
25 String tip = "images/MsgError.gif";
26 //从服务器获取session中的验证码
27 String checkcodeServer = (String) ActionContext.getContext().getSession().get("CHECKNUM");
28 //将客户端的验证码与服务端的验证码进行比较
29 if(checkcode.equals(checkcodeServer)){
30 tip = "images/MsgSent.gif";
31 }
32 //以IO流的方式将tip变量的值输出到AJAX异步对象中
33 HttpServletResponse response = ServletActionContext.getResponse();
34 response.setContentType("text/html;charset=UTF-8");
35 PrintWriter pw = response.getWriter();
36 pw.write(tip);
37 pw.flush();
38 pw.close();
39 //以下方式不是最好的,但可以完成AJAX异步交互
40 return null;
41 }
42 }
View Code

 

2.在webroot下兴建一个image文件夹放置错误或正确的图片

 

用struts2做一个带有图片效果的登陆验证码

标签:ring   rac   get   except   文件   exp   dea   exception   ini   

原文地址:http://www.cnblogs.com/daniel-lij/p/6129154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!