Java 验证码、二维码
需要: jelly-core-1.7.0.GA.jar
网站: http://lychie.github.io/products.html
将下载下来的 jelly-core-1.7.0.GA.jar 丢到项目里,不需要编写任何 java 代码,只需要在项目的 web.xml 文件中配置相关信息即可。
验证码
基于 Servlet 实现,在 web.xml 中配置即可使用。并支持自定义样式,常用的配置项见下表:
属性名称 | 属性描述 | 默认值 | 可选值 |
border | 边框 | no | no,yes |
borderColor | 边框颜色 | black | RGB,颜色名称 |
borderThickness | 边框厚度 | 1 | 数值 |
style | 验证码样式 | wave | wave ( 波纹 ) shadow ( 阴影 ) fishEye ( 鱼眼 ) |
width | 验证码宽度 ( 不建议的配置项 ) | 230 | 数值 |
height | 验证码高度 ( 不建议的配置项 ) | 90 | 数值 |
fontSize | 验证码字体大小 ( 不建议的配置项 ) | 70 | 数值 |
fontColor | 验证码字体颜色 | black | RGB,颜色名称 |
fontFamily | 验证码字体 | Arial,Helvetica,Times New Roman,Liberation Mono | 字体名称 |
charString | 验证码字符 | NU | N ( 随机打乱的数字 ) L ( 随机打乱的小写字母 ) U ( 随机打乱的大写字母 ) NL ( 随机打乱的数字 + 小写字母 ) NU ( 随机打乱的数字 + 大写字母 ) LU ( 随机打乱的大小写字母 ) NLU ( 随机打乱的数字 + 大小写字母 ) |
charLength | 验证码字符的长度 | 4 | 数值 |
charSpace | 验证码字符间隔空隙 | 1 | 数值 |
noiseColor | 干扰线颜色 | black | RGB,颜色名称 |
sessionKey | 验证码存储在 Session 的 Key | idCode | 字符串 |
波纹样式
web.xml
<servlet>
<servlet-name>ValidateCode</servlet-name>
<servlet-class>org.jelly.image.ValidateCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ValidateCode</servlet-name>
<url-pattern>/idCode</url-pattern>
</servlet-mapping>
<servlet-name>ValidateCode</servlet-name>
<servlet-class>org.jelly.image.ValidateCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ValidateCode</servlet-name>
<url-pattern>/idCode</url-pattern>
</servlet-mapping>
jsp
<img src="idCode" onclick="this.src+=''" style="cursor:pointer;" width="115" height="30" title="看不清?换一个">
结果图:
结果图:
阴影样式
web.xml
<servlet>
<servlet-name>ValidateCode</servlet-name>
<servlet-class>org.jelly.image.ValidateCode</servlet-class>
<init-param>
<param-name>style</param-name>
<param-value>shadow</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ValidateCode</servlet-name>
<url-pattern>/idCode</url-pattern>
</servlet-mapping>
<servlet-name>ValidateCode</servlet-name>
<servlet-class>org.jelly.image.ValidateCode</servlet-class>
<init-param>
<param-name>style</param-name>
<param-value>shadow</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ValidateCode</servlet-name>
<url-pattern>/idCode</url-pattern>
</servlet-mapping>
jsp
<img src="idCode" onclick="this.src+=''" style="cursor:pointer;" width="115" height="30" title="看不清?换一个">
结果图:
鱼眼样式
web.xml
<servlet>
<servlet-name>ValidateCode</servlet-name>
<servlet-class>org.jelly.image.ValidateCode</servlet-class>
<init-param>
<param-name>style</param-name>
<param-value>fishEye</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ValidateCode</servlet-name>
<url-pattern>/idCode</url-pattern>
</servlet-mapping>
<servlet-name>ValidateCode</servlet-name>
<servlet-class>org.jelly.image.ValidateCode</servlet-class>
<init-param>
<param-name>style</param-name>
<param-value>fishEye</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ValidateCode</servlet-name>
<url-pattern>/idCode</url-pattern>
</servlet-mapping>
jsp
<img src="idCode" onclick="this.src+=''" style="cursor:pointer;" width="115" height="30" title="看不清?换一个">
结果图:
二维码
基于 Servlet 实现,在 web.xml 中配置即可使用。并支持自定义样式,常用的配置项见下表:
使用默认样式
web.xml
<servlet>
<servlet-name>Qrcode</servlet-name>
<servlet-class>org.jelly.image.Qrcode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Qrcode</servlet-name>
<url-pattern>/qrcode</url-pattern>
</servlet-mapping>
<servlet-name>Qrcode</servlet-name>
<servlet-class>org.jelly.image.Qrcode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Qrcode</servlet-name>
<url-pattern>/qrcode</url-pattern>
</servlet-mapping>
jsp
<img src="qrcode?text=http://lychie.github.io/index.html">
结果图:
样式
web.xml
<servlet>
<servlet-name>Qrcode</servlet-name>
<servlet-class>org.jelly.image.Qrcode</servlet-class>
<init-param>
<param-name>foreground</param-name>
<param-value>pink</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Qrcode</servlet-name>
<url-pattern>/qrcode</url-pattern>
</servlet-mapping>
<servlet-name>Qrcode</servlet-name>
<servlet-class>org.jelly.image.Qrcode</servlet-class>
<init-param>
<param-name>foreground</param-name>
<param-value>pink</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Qrcode</servlet-name>
<url-pattern>/qrcode</url-pattern>
</servlet-mapping>
jsp
<img src="qrcode?text=http://lychie.github.io/index.html">
结果图:
转载出处:http://www.blogjava.net/fancydeepin