求助,javaweb注册做验证码,怎么弄
用javaweb做登录页面,做验证码怎么写啊?并且点击能够刷新,求完整方法并解释啊,还有验证码如果要配置xml什么的,怎么配置。谢谢了
[解决办法]
去这个传送门,有你想要的,但是它是整合了Struts2以后的验证码哦!
http://www.iteye.com/topic/300128
[解决办法]
servlet
package com.tjsoft.system.login;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.OutputStream;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class ValidateServlet extends HttpServlet { /** * Constructor of the object. */ public ValidateServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedImage bi=new BufferedImage(75,25,BufferedImage.TYPE_INT_RGB); Graphics g=bi.getGraphics(); g.fillRect(3, 3, 70, 20); String line=""; Random rnd=new Random(); for(int i=0;i<5;i++){ line+=rnd.nextInt(10); } HttpSession session=request.getSession(); session.setAttribute("line", line); g.setFont(new Font("黑体",Font.BOLD,20)); int x1 = 0; int x2 = 0; int y1 = 0; int y2 = 0; g.setColor(Color.gray); for(int i = 0; i < 150; i ++){ x1 = rnd.nextInt(60)+3; x2 = rnd.nextInt(70)+3; y1 = rnd.nextInt(10)+3; y2 = rnd.nextInt(20)+3; g.drawLine(x1, y1, x2, y2); } g.setColor(Color.RED); g.drawString(line, 3, 20); response.setContentType("image/jpeg");//将servlet设置成图片类型 //禁止缓存 response.setHeader("pragma", "no-cache"); response.setHeader("cache-control", "no-cache"); response.setDateHeader("expires", 0); OutputStream os = response.getOutputStream(); //将图片写到客户端 ImageIO.write(bi, "jpg", os); }}
[解决办法]
var code; // 在全局 定义验证码function createCode() { code = ""; var codeLength = 3;// 验证码的长度 var checkCode = document.getElementById("checkCode"); var selectChar = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); // , 'A', 'B', // 'C', 'D', 'E', 'F', 'G', 'G', 'I', 'J', 'K', 'L', 'M', 'N', // 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', // 'Z');//所有候选组成验证码的字符,当然也可以用中文的 for ( var i = 0; i < codeLength; i++) { var charIndex = Math.floor(Math.random() * 10); code += selectChar[charIndex]; } if (checkCode) { checkCode.className = "code"; checkCode.value = code; checkCode.blur(); }}function validate() { var inputCode = document.getElementById("validCode").value; if (inputCode.length <= 0) { alert("请输入验证码!"); return false; } else if (inputCode.toUpperCase() != code) { alert("验证码输入错误!"); createCode();// 刷新验证码 return false; } form1.submit();}function assign() {}