官方getting start指导:5 minutes application integration tutorial
http://jcaptcha.octo.com/confluence/display/general/5+minutes+application+integration+tutorial
新建Maven工程:
输入项目名称等选项:
添加dependiencies
添加完之后在重新生成Maven工程的时候出现下面的错误:
Missing artifact com.jhlabs:imaging:jar:01012005:compile
显然是jcaptcha所倚赖的一个jar包不在常用的仓库里面,后来在其官方网站 上终于找到一个包含此包的仓库地址
<repositories>
<repository>
<id>atlassian</id>
<name>atlassian</name>
<url>http://repository.atlassian.com/maven2</url>
</repository>
</repositories>
Implement a CaptchaService
新建类:CaptchaServiceSingleton.java
package net.hubs1.web;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
import com.octo.captcha.service.image.ImageCaptchaService;
public class CaptchaServiceSingleton {
private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService();
public static ImageCaptchaService getInstance() {
return instance;
}
}
新建Servlet类:ImageCaptchaServlet
package net.hubs1.web.servlet;
import com.octo.captcha.service.CaptchaServiceException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.hubs1.web.CaptchaServiceSingleton;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ImageCaptchaServlet extends HttpServlet {
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
}
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
byte[] captchaChallengeAsJpeg = null;
// the output stream to render the captcha image as jpeg into
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
// get the session id that will identify the generated captcha.
//the same id must be used to validate the response, the session id is a good candidate!
String captchaId = httpServletRequest.getSession().getId();
// call the ImageCaptchaService getChallenge method
BufferedImage challenge =
CaptchaServiceSingleton.getInstance().getImageChallengeForID(captchaId,
httpServletRequest.getLocale());
// a jpeg encoder
JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
jpegEncoder.encode(challenge);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (CaptchaServiceException e) {
httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
// flush it in the response
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream =
httpServletResponse.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
}
}
把Servlet配置到web.xml文件中:
<servlet>
<servlet-name>jcaptcha</servlet-name>
<servlet-class>net.hubs1.web.servlet.ImageCaptchaServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jcaptcha</servlet-name>
<url-pattern>/jcaptcha</url-pattern>
</servlet-mapping>
修改index.jsp文件:
<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jcaptcha example</title>
</head>
<body>
<form action="testjcaptcha">
<label for="j_captcha_response">请填写验证码:</label><br/>
<img src="jcaptcha">
<input type='text' name='j_captcha_response' value=''>
<input type="submit" value="提交">
</form>
</body>
</html>
新建验证Servle类:
package net.hubs1.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.hubs1.web.CaptchaServiceSingleton;
import com.octo.captcha.service.CaptchaServiceException;
public class ValidationServlet extends HttpServlet {
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
}
protected void doGet(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws ServletException, IOException {
Boolean isResponseCorrect = Boolean.FALSE;
// remenber that we need an id to validate!
String captchaId = httpServletRequest.getSession().getId();
// retrieve the response
String response = httpServletRequest.getParameter("j_captcha_response");
// Call the Service method
try {
isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(
captchaId, response);
} catch (CaptchaServiceException e) {
// should not happen, may be thrown if the id is not valid
}
httpServletResponse.setContentType("text/html;charset=utf-8");
PrintWriter out = httpServletResponse.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>验证结果:</TITLE></HEAD>");
out.println(" <BODY>");
if(isResponseCorrect == Boolean.TRUE){
out.print("验证通过!");
}else {
out.print("验证失败!");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
protected void doPost(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws ServletException, IOException {
doGet(httpServletRequest, httpServletResponse);
}
}
把验证Servlet配置到web.xml文件中:
<servlet>
<servlet-name>testjcaptcha</servlet-name>
<servlet-class>net.hubs1.web.servlet.ValidationServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>testjcaptcha</servlet-name>
<url-pattern>/testjcaptcha</url-pattern>
</servlet-mapping>
在Pom.xml中添加Web服务器:Jetty插件
修改Pom.xml,在<build>/<plugins>标签下添加如下内容(为了解决“ 警告:com.sun.image.codec.jpeg.JPEGCodec 是 Sun 的专用 API,可能会在未来版本中删除”)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<verbose/>
<bootclasspath>${java.home}/lib/rt.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
选中Pom.xml文件,按下 Alt + Shift + X , M 键,弹出运行配置窗口:
在Goals中输入:clean package jetty:run
运行。
测试:http://localhost:8080/jcaptchaTest/
网页效果:
- 大小: 51 KB
- 大小: 36.4 KB
- 大小: 37.1 KB
- 大小: 30.5 KB
- 大小: 37.9 KB
- 大小: 15.2 KB
分享到:
相关推荐
使用最简方式使用了JCaptcha开源组件,非常适合学习
支持验证码制作的开源组件,很好用
ptcha是一个开源的用来生成图形验证码的Java开源组件,使用起来也是非常的简单方便。 jcapthca是非常强大的,不光是可以生成图片式的验证码,还可以生成声音式的(新浪就使用了双重验证码)。 Jcaptcha是CAPTCHA里面...
jcaptcha是一个开源的用来生成图形验证码的Java开源组件,使用起来也是非常的简单方便。
Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码图片。 Java 命令行解析...
Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码图片。 Java 命令行解析...
Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码图片。 Java 命令行解析...
Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码图片。 Java 命令行解析...
Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码图片。 Java 命令行解析...
Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码图片。 Java 命令行解析...
Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码图片。 Java 命令行解析...
Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码图片。 Java 命令行解析...
Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码图片。 Java 命令行解析...
百度云盘分享 简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级、中级、高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情、执着,对...
Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码图片。 Java 命令行解析...
Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码图片。 Java 命令行解析...
Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码图片。 Java 命令行解析...
简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级、中级、高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情、执着,对IT的憧憬、向往!...