当前位置:编程学习 > JAVA >>

Java版纯字母、纯数字、字母数字组合验证码的生成

所谓验证码,就是将一串随机产生的数字或符号,生成一幅图片, 图片里加上一些干扰,例如随机画数条直线或者画一些点,由用户肉眼识别其中的验证码信息,输入表单提交网站验证,验证成功后才能使用某项功能。验证码中之所以加上凌乱的直线是为了防止某些人使用OCR软件识别随机产生的数字或符号,从而达到恶意破解密码、刷票、论坛灌水、刷页等恶意行为。下面就开始直接上代码吧:

 


下面是Demo的文件组织结构

 

 

 

 


下面就是index.jsp的代码。主要功能是单击浏览器上的验证码图片,实现验证码的更换。

 

 


[java]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 
    <base href="<%=basePath%>"> 
     
    <title>验证码</title> 
    <meta http-equiv="pragma" content="no-cache"> 
    <meta http-equiv="cache-control" content="no-cache"> 
    <meta http-equiv="expires" content="0">     
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
    <meta http-equiv="description" content="This is my page"> 
    <title>验证码</title> 
    <script type="text/javascript"> 
    function refresh(obj) { 
        obj.src = "${pageContext.request.contextPath}/RandomValidateCodeServlet?"+Math.random(); 
    } 
    </script> 
  </head> 
   
  <body> 
   <form action="checkServlet" method="post"> 
        <img title="点击更换" onclick="javascript:refresh(this);" src="RandomValidateCodeServlet"> 
    </form> 
  </body> 
</html> 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>验证码</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <title>验证码</title>
    <script type="text/javascript">
    function refresh(obj) {
        obj.src = "${pageContext.request.contextPath}/RandomValidateCodeServlet?"+Math.random();
    }
    </script>
  </head>
 
  <body>
   <form action="checkServlet" method="post">
        <img title="点击更换" onclick="javascript:refresh(this);" src="RandomValidateCodeServlet">
    </form>
  </body>
</html>
Web.xml中的servlet配置信息如下

 

[java]
<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5"  
    xmlns="http://java.sun.com/xml/ns/javaee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
     
    <servlet> 
    <servlet-name>RandomValidateCodeServlet</servlet-name> 
    <servlet-class>com.web.RandomValidateCodeServlet</servlet-class> 
  </servlet> 
 
  <servlet-mapping> 
    <servlet-name>RandomValidateCodeServlet</servlet-name> 
    <url-pattern>/RandomValidateCodeServlet</url-pattern> 
  </servlet-mapping> 
   
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app> 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 <servlet>
    <servlet-name>RandomValidateCodeServlet</servlet-name>
    <servlet-class>com.web.RandomValidateCodeServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>RandomValidateCodeServlet</servlet-name>
    <url-pattern>/RandomValidateCodeServlet</url-pattern>
  </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

下面是servlet中的代码,这里只是为了演示所以就只做了doGet方法,对于doPost方法就没有在这里考虑

 

[java]
public class RandomValidateCodeServlet extends HttpServlet { 
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
 
        response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片  
        response.setHeader("Pragma", "No-cache");//设置响应

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,