验证码 使用

前端之家收集整理的这篇文章主要介绍了验证码 使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下载区 有jar 包

	<dependency>
			<groupId>com.github.bingoohuang</groupId>
			<artifactId>patchca</artifactId>
			<version>0.0.1</version>
	</dependency>

验证码 使用

<div class="login_code_pic">
	 <a href="javascript:void(0)">
		 <img src="images/code_demo.png"	id="loginCode" onclick="refreshCode('login');" />
	 </a>
	 </div>
	 <div class="login_code_text">
		 <a href="javascript:void(0)" onclick="refreshCode('login');" >看不清,换一张?</a>
	 </div>
 </div>
function refreshCode(imgName) {
	var date = new Date();
	try {
		if (imgName == "login") {
			$("#loginCode").attr(
					"src","userAction_refreshPatchcaImage.html?time="
					+ date.getTime());
		} else {
			$("#patchaImage").attr(
					"src","userAction_refreshPatchcaImage.html?time="
					+ date.getTime());
		}
	} catch (e) {
		alert("验证码刷新超时,请重新刷新界面!");
		window.location.reload();
	}
}
private static final String DEFAULT_CHARACTERS="34578AaCcdEeFGHhJjKkMmNnPpQRsTtUuVvWwXxYy";

private static final String PATCHCACODE = "PATCHCA"; 



public void refreshPatchcaImage() throws Exception {
		HttpServletResponse response = null;
		HttpServletRequest request = null;
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=UTF-8");
		response.setContentType("image/png");//设置相应类型,告诉浏览器输出内容图片        
		response.setHeader("Pragma","No-cache");//设置响应头信息,告诉浏览器不要缓存此内容        
		response.setHeader("Cache-Control","no-cache");        
		response.setDateHeader("Expire",0);
		OutputStream os=null;
		try{
			//创建图片对象服务
			ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
			//修改图片文字颜色
			cs.setColorFactory(new SingleColorFactory(new Color(0,0)));
			//创建图片文字对象
			RandomWordFactory randomWordFactory = new RandomWordFactory();
			//设置文字对象范围
			randomWordFactory.setCharacters(DEFAULT_CHARACTERS);
			//设置图片文字最大长度
			randomWordFactory.setMaxLength(4);
			//设置图片文字最小长度
			randomWordFactory.setMinLength(4);
			//设置图片文字属性
			cs.setWordFactory(randomWordFactory);
			//设置图片的干扰效果的样式..一共有五种
			cs.setFilterFactory(new WobbleRippleFilterFactory ());
			Captcha captcha = cs.getCaptcha();
			//将四位数字的验证码保存到Session中。
			request.getSession().setAttribute(PATCHCACODE,captcha.getChallenge());
			//将图像输出到Servlet输出流中
			os=response.getOutputStream();
			ImageIO.write(captcha.getImage(),"png",os);
			os.flush();
			//关闭输出流
		}catch (Exception e) {
			e.printStackTrace();
			/*PrintWriter writer = response.getWriter();
			writer.print(false);
			writer.flush();
			writer.close();*/
			//log.error("生成验证码错误!......." + e);
		}finally{
			if(null!=os){
				os.close();
			}
		}
		
	}
@H_403_13@捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。


谢谢您的赞助,我会做的更好!

原文链接:https://www.f2er.com/ajax/163460.html

猜你在找的Ajax相关文章