jcaptcha是专业的解决方案,做到了极致,没必要自己去写了
下载之:http://sourceforge.net/projects/jcaptcha/files/
根据他官方的例子,在struts中集成方法如下
环境:struts 1.3.x jcaptcha 1.0
1. 加载lib
把jcaptcha-1.0-all.jar 和 jcaptcha-integration-struts-1.0.jar 放到工程的lib里面,并设置包含之
2. 配置struts-config.xml
加plug-in描述
<!-- WizRtf2Html Charset=0 --><plug-in className
=“com.octo.captcha.module.struts.CaptchaServicePlugin”/>
加引用的 action
<!-- WizRtf2Html Charset=0 --><action-mappings
>这个里面加以下代码
<!– add the render action–>
<action
path=“/jcaptcha”
type=“com.octo.captcha.module.struts.image.RenderImageCaptchaAction”
>
</action
>
即:可用 http://server/proj/jcaptcha.do
调用(如果你设置的是 *.do) http://server/proj/do/jcapcha
(如果你设置的是 /do/*的话) 显示生成的图片
运行起来后,在浏览器中应该可以看到图片了
3. jsp中集成
如 login.jsp中
最前面加入taglib定义
<%– Add the jcaptcha taglib–%>
<%@ taglib
uri=“http://jcaptcha.sourceforge.net”
prefix=“jcaptcha”
%>
增加显示和校验代码
<%– Add the jcaptcha form part–%>
<input type
=“text” name
=“jcaptcha_response” />
<!– 加入错误提示,改成用errors了–>
<font
class
=“required_error_font”>
<html:errors
property=“jcaptcha_error_msg”
/></font
><br
/>
<font size
=“-1″ color
=“#6f6f6f”>
<jcaptcha:question
/></font
><br
/>
<%– Add the image–%>
<img name
=“jcaptcha” id
=“jcaptcha” onclick
=“refresh_jcaptcha(this)”
src
=“<%=
request.getContextPath()%>/do/jcaptcha” alt
=“click to refresh” style
=“cursor:pointer;”/>
增加了个点击刷新的功能,脚本如下
<!-- WizRtf2Html Charset=0 --><script language=“Javascript”
>
function
refresh_jcaptcha(obj){
//alert(obj);
obj.src=”<%=
request.getContextPath()%>
/do/jcaptcha?” + Math.random();
}
</script>
4. 在提交页面对应的Action.excute()中增加校验代码处理 request
我把他封装成了函数
<!-- WizRtf2Html Charset=0 -->public
boolean
isJcaptchaOK(HttpServletRequest request)
{
log
.debug(“enter captcha challenge verification”
);
CaptchaService service = CaptchaServicePlugin.getInstance
()
.getService();
String responseKey = CaptchaServicePlugin.getInstance
()
.getResponseKey();
String captchaID = CaptchaModuleConfigHelper.getId
(request);
String challengeResponse = request.getParameter(responseKey);
if
(log
.isDebugEnabled())
log
.debug(“response for id “
+ captchaID + ” : “
+ challengeResponse);
request.removeAttribute(responseKey);
boolean
isResponseCorrect = false
;
if
(challengeResponse != null
)
try
{
isResponseCorrect = service.validateResponseForID(captchaID,
challengeResponse).booleanValue();
} catch
(CaptchaServiceException e) {
log
.debug(“Error during challenge verification”
, e);
request.setAttribute(CaptchaServicePlugin
.getInstance
().getMessageKey(),
CaptchaModuleConfigHelper
.getMessage
(request));
log
.debug(“forward to error with message : “
+ CaptchaModuleConfigHelper.getMessage
(request));
throw
(e);//抛出程序应用异常
}
if
(isResponseCorrect) {
log
.debug(“correct : forward to success”
);
return
true;//正确
}
if
(log
.isDebugEnabled()) {
log
.debug(“false : forward to failure with message : “
+ CaptchaModuleConfigHelper.getMessage
(request));
log
.debug(“in request attribute key : “
+ CaptchaServicePlugin.getInstance
().getMessageKey());
}
ActionErrors errors = new
ActionErrors();//用ActionError替换了他自己的Message
errors.add(“jcaptcha_error_msg”
, new
ActionMessage(
“error.jcaptcha.error.inputInvalid”
));
this
.addErrors(request, errors);
return
false;
}
—–
excute(…)中:
<!-- WizRtf2Html Charset=0 -->if
(!isJcaptchaOK(request))
return
mapping.findForward(“return_form”
); //返回页面
return
mapping.findForward(“global_success”
);
——
大功告成
相关推荐
stucts2文件上传性信息笑嘻嘻笑嘻嘻笑嘻嘻笑嘻嘻笑嘻嘻笑嘻嘻笑嘻嘻笑嘻嘻新
基于Stucts2+Hibernate的学生信息管理系统,可实现增删改查等操作
对刚刚学习structs有一定的帮助,希望大家会喜欢!!!!!!!!!!!!!
宿舍管理 jsp stucts mysql 宿舍管理 分为学生 管理员
cas client增加stucts框架 服务端返回用户其它信息
商城基于stucts框架;采用eclipse框架,页面基于jsp,数据库使用SqlServer, 前端页面包括商品展示,购买以及订单管理,后端对商品信息及用户信息进行了基本的查插删改操作
级好的stucts入门实例程序(包含了基本的框架和知识点)
java+STUCTs 用上UML建模 配套数据库
J2EE技术+mysql数据库的简单图书管理系统,包括论文资料,给有需要的童鞋学习参考
有助于stucts的初学者,因为我也是初学者,我们在同一方向上共同努力着;内包含一些网页等资料,我想会对你有帮助的!
struts2的描述,可以对新手有一些帮助,谢谢提建议。
配合博文记录
最近自学java中的框架-struts写了一些小例子,这都是很经典的程序,如果大家瞧得起要下载去看看,顺便给俺找找不足的地方。我的qq 821865130 email qingtian_hechen@163.com 希望大家能多多给我帮助...在此谢谢各位!!
struts2 简单登录实例
stucts简介和基本应用和基本配置信息,基本功能来历
J2EE_Spring_Stucts_Hibernate架构-经典_435页.pdf 本课程给出J2EE 平台技术概念上的一格较为清晰而完整的思路,帮助学生掌握各技术 间的相互关系和重要的思想。通过一系列的实战训练,使学生掌握基于J2EE 平台的...
适合首次接触stucts架构的初学者 用户登录 密码验证 可运行
最新Struts2.5开发帮助文档,需要的可以自行下载最新Struts2.5开发帮助文档,需要的可以自行下载
该项目是MyEclispse做的客户管理系统项目,压缩包中包含sql脚本
检测一个url命令为 python3 CVE-2022-22954-POC.py -u http://test.com 检测多个url命令为 python3 CVE-2022-22954-POC.py -f file.txt(把url按一行一个保存到file.txt中) 结果有可能如下