`
king_tt
  • 浏览: 2116711 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JSP实现彩色验证码—源码实例

 
阅读更多
点击这里使用RSS订阅本Blog:

jsp实现彩色验证码 你只用复制粘贴就可以了

转自【http://www.blogjava.net/zcq87642231/archive/2008/02/24/181748.html】
Demo1.jsp

<%@pagelanguage="java"import="java.util.*"pageEncoding="ISO-8859-1"%>
<%@pageimport="java.awt.*"%>
<%@pageimport="java.awt.image.*"%>
<%@pageimport="javax.imageio.*"%>
<%!
ColorgetRanColor(
intfc,intbc)
{
Randomrandom
=newRandom();
if(fc>255)fc=255;
if(bc>255)bc=255;
intr=fc+random.nextInt(bc-fc);
intg=fc+random.nextInt(bc-fc);
intb=fc+random.nextInt(bc-fc);
returnnewColor(r,g,b);
}

%>
<%
response
.setHeader("Pragma","No-cache");
response
.setHeader("Cache-Control","no-che");
response
.setDateHeader("Expires",0);
intwidth=60;
intheight=20;
BufferedImageimage
=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphicsg
=image.getGraphics();
Randomrandom
=newRandom();
g
.setColor(getRanColor(200,250));
g
.fillRect(0,0,width,height);
g
.setFont(newFont("TimesNewRoman",Font.PLAIN,18));
g
.setColor(getRanColor(160,200));

for(inti=0;i<155;i++)
{
intx=random.nextInt(width);
inty=random.nextInt(height);
intxl=random.nextInt(12);
intyl=random.nextInt(12);
g
.drawLine(x,y,x+xl,y+yl);
}
String
sRand="";
for(inti=0;i<4;i++)
{
String
rand=String.valueOf(random.nextInt(10));
sRand+=rand;
g
.setColor(newColor(20+random.nextInt(110),40+random.nextInt(110),60+random.nextInt(110)));
g
.drawString(rand,13*i+6,16);
}
session
.setAttribute("rand",sRand);
g
.dispose();
ImageIO
.write(image,"JPEG",response.getOutputStream());
%>





Demo.jsp

<%@pagelanguage="java"pageEncoding="gbk"%>

<%@tagliburi="http://struts.apache.org/tags-bean"prefix="bean"%>
<%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%>
<%@tagliburi="http://struts.apache.org/tags-logic"prefix="logic"%>
<%@tagliburi="http://struts.apache.org/tags-tiles"prefix="tiles"%>


<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html:htmllang="true">
<head>
<html:base/>

<title>Demo.jsp</title>

<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->

</head>

<body>&nbsp;
<formaction="Demo2.jsp">
<inputtype="text"name="asd"><imgborder="0"src="Demo1.jsp">
<br>
<inputtype="submit"name="b"value="点击">
</form>
</body>
</html:html>


Demo2.jsp

<%@pagelanguage="java"import="java.util.*"pageEncoding="ISO-8859-1"%>


<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>

<title>MyJSP'Demo2.jsp'startingpage</title>



</head>

<body>
<%
Stringinput
=request.getParameter("asd");
String
rand=(String)request.getSession().getAttribute("rand");
if(input.equals(rand))
{

}
else
{
%>
<jsp:forwardpage="Demo.jsp"></jsp:forward>
<%
}
%>


asdasd
</body>
</html>

1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics