JSP中图片验证的实现_JSP_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JSP > JSP中图片验证的实现

JSP中图片验证的实现

 2011/8/15 8:07:11    程序员俱乐部  我要评论(0)
  • 摘要:[前言]:图片验证在有关注册和发表留言经常用到.我在此用jsp写了个.供大家参考.其中com.sun.image不是java的标准包.需要另外下载.相关地址为:http://java.sun.com/products/java-media/jai/1.random.jsp(产生四位的随机字符,由0-9,a-z,A-Z构成.并把最终字符串放到session中保存以让后续页面验证真伪)代码如下:<%@pageautoFlush="false"import="java.util.*,java
  • 标签:实现 图片

  [前言]:图片验证在有关注册和发表留言经常用到.我在此用jsp写了个.供大家参考. 其中 com.sun.image 不是java的标准包.需要另外下载.相关地址为:

  http://java.sun.com/products/java-media/jai/

  1. random.jsp (产生四位的随机字符,由0-9,a-z,A-Z构成.并把最终字符串放到session中保存以让后续页面验证真伪)

  代码如下:

<%@ page autoFlush="false" import="java.util.*,java.awt.*,java.awt.image.*,
com.sun.image.codec.jpeg.*,java.util.*"%>

<%@ page import="" contentType="text/html; charset=gb2312"%>

<%

String chose=
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char display[]={'0',' ','0',' ','0',' ','0'},ran[]={'0','0','0','0'},temp;
Random rand=new Random();
for(int i=0;i<4;i++)

{
temp=chose.charAt(rand.nextInt(chose.length()));
display[i*2]=temp;
ran[i]=temp;

}



String random=String.valueOf(display);
session.setAttribute("random",String.valueOf(ran));

%>

<%
out.clear();

response.setContentType("image/jpeg");

response.addHeader("pragma","NO-cache");

response.addHeader("Cache-Control","no-cache");

response.addDateHeader("Expries",0);

int width=47, height=15;

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();

//以下填充背景颜色

g.setColor(Color.GREEN);

g.fillRect(0, 0, width, height);

//设置字体颜色

g.setColor(Color.RED);

g.drawString(random,3,10);

g.dispose();

ServletOutputStream outStream = response.getOutputStream();

JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(outStream);

encoder.encode(image);

outStream.close();

%>


[1] [2] >>继续下一页

  [前言]:图片验证在有关注册和发表留言经常用到.我在此用jsp写了个.供大家参考. 其中 com.sun.image 不是java的标准包.需要另外下载.相关地址为:

  http://java.sun.com/products/java-media/jai/

  1. random.jsp (产生四位的随机字符,由0-9,a-z,A-Z构成.并把最终字符串放到session中保存以让后续页面验证真伪)

  代码如下:

<%@ page autoFlush="false" import="java.util.*,java.awt.*,java.awt.image.*,
com.sun.image.codec.jpeg.*,java.util.*"%>

<%@ page import="" contentType="text/html; charset=gb2312"%>

<%

String chose=
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char display[]={'0',' ','0',' ','0',' ','0'},ran[]={'0','0','0','0'},temp;
Random rand=new Random();
for(int i=0;i<4;i++)

{
temp=chose.charAt(rand.nextInt(chose.length()));
display[i*2]=temp;
ran[i]=temp;

}



String random=String.valueOf(display);
session.setAttribute("random",String.valueOf(ran));

%>

<%
out.clear();

response.setContentType("image/jpeg");

response.addHeader("pragma","NO-cache");

response.addHeader("Cache-Control","no-cache");

response.addDateHeader("Expries",0);

int width=47, height=15;

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();

//以下填充背景颜色

g.setColor(Color.GREEN);

g.fillRect(0, 0, width, height);

//设置字体颜色

g.setColor(Color.RED);

g.drawString(random,3,10);

g.dispose();

ServletOutputStream outStream = response.getOutputStream();

JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(outStream);

encoder.encode(image);

outStream.close();

%>



  2. img.jsp (显示验证图片.由于本程序简单让验证程序也一并放在了一起)

  代码如下:

<%@ page contentType="text/html; charset=gb2312" language="java"%>

<%

String num=request.getParameter("num");

String random=(String)session.getAttribute("random");
if(num!=null&&random!=null)

{

if(!num.equals(random))

{

out.println("<script>alert('验证码错误!请重试。')</script>");
out.println("<script>history.go(-1)</script>");
//response.sendRedirect("img.jsp");

}

else

{

out.println("<center>验证成功!</center>");

}

}

%>

<html>

<head>

<title>图片验证</title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

</head>
<body>

<form action="img.jsp" method="post">

<table>

<tr>

<td>

<input type="text" name="num" size=10>

</td>

<td>

<img src="random.jsp">

</td>

</tr>

</table>

<input type="submit" value="OK">

</form>

</body>

</html>


  说明:有兴趣的网友可以在此基础上增加图片的内容.比如让背景加上纹理.让字符位置歪斜以加大扫描程序的难度.大家有什么看法和建议,都可以回复中提出来....
发表评论
用户名: 匿名