图像应该由captcha
动作生成。<img>
然后在标签中提供此操作的 URL 。要在您的项目中实施验证码,请按照以下步骤操作
将 jar 添加到您的 Web 项目类路径:simplecaptcha-1.2.1.jar
. 通常在web-inf/lib
文件夹内。
添加新的动作类RegisterAction
注意:以下代码使用约定插件来映射动作,为简单起见,DMI 用于在提交表单时调用动作类的某些方法。要打开 DMI,请使用以下常量struts.xml
:
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
RegisterAction.java
:
public class RegisterAction extends ActionSupport {
private String userId;
private String userEmail1;
private String userEmail2;
private String userPassword1;
private String userPassword2;
private String captchaResponse;
private InputStream inputStream;
//getters and setters
public String create() {
//RegisterAction is the form bean of the current action and captchaResponse is the field of user input
String answer = (String) ActionContext.getContext().getSession().get("CorrectAnswer");
if (answer == null || getCaptchaResponse()==null || !answer.equals(getCaptchaResponse())){
addFieldError("captchaResponse", getText("error.captcha"));
}
return SUCCESS;
}
@Action(value = "captcha", results = {@Result(type="stream", params = {"contentType", "image/jpeg"})})
public String captcha() {
try {
Captcha captcha = new Captcha.Builder(200, 50).addText(new DefaultTextProducer()).gimp(new DropShadowGimpyRenderer()).build();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//write the image
CaptchaServletUtil.writeImage(outputStream, captcha.getImage());
//store the answer for this in session
ActionContext.getContext().getSession().put("CorrectAnswer", captcha.getAnswer());
//return image
inputStream = new ByteArrayInputStream(outputStream.toByteArray());
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
RegisterAction.properties
包含错误键的以下值:
RegisterAction.properties
:
error.captcha=Invalid value of shown text!
所以我们检查是否成功通过或添加关于验证码的错误错误。
register.jsp
通常添加在web-inf\content
register.jsp
:
<!DOCTYPE html>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<b>REGISTER</b>
<p>Please fill up the form below.</p>
<s:form action="register" method="post">
<s:textfield label="Enter username" key="userId" maxlength="25"
size="30" />
<s:textfield label="Enter email" key="userEmail1" type="email"
placeholder="someone@domain.com" size="30" />
<s:textfield label="Re-enter email" key="userEmail2" type="email"
placeholder="someone@domain.com" size="30" />
<s:password label="Enter password" key="userPassword1" size="30" />
<s:password label="Re-enter password" key="userPassword2"
size="30" />
<tr><td>
<img id="captchaImg" src="<s:url action='captcha'/>" alt="Captcha Image" height="45">
<img src="<c:url value='/images/reload.jpg' />" alt="Reload" onclick="document.forms[0].captchaImg.src='<s:url action='captcha'/>'+'?id='+Math.random();" style="cursor:pointer"/>
<s:textfield label="Enter CAPTCHA" key="captchaResponse" size="30" requiredLabel="*"/>
<tr><td>
Cannot read? Refresh page for new CAPTCHA.
</td></tr>
<s:submit method="create" value="Register" />
</s:form>
</body>
</html>
这将构造验证码和文本字段以输入值和 Struts 错误消息以显示captchaResponse
字段中的错误,以及刷新图标。
注意:我们在这里使用的一个好技巧是 javascriptMath.random()
函数,这样可以防止某些浏览器(如 Firefox)缓存 URL 并继续发布相同的 Captcha 图像,这会强制它获取新值而无需付出更多努力。
这是它的样子:

有关详细信息,请参阅网站:SimpleCaptcha