在有些要登录的页面都有验证码,是如何编写的了
解决方案 »
- 如何把视频网页的地址转化为网页中视频的地址啊
- 梅花雪展示问题,急...在线等,谢谢各位大哥
- 在线等!!! 急急急!!! 关于JSP 显示 RESULT !!!
- 问些基础问题
- 求助,大伙帮忙看看这是怎么搞的??
- jfree问题,关于坐标~~~达人SOS!
- HashMap与session(急用,等等)
- JSP取Oracle里的字符串1变成了0x312020202020202020?
- 为什么编译时总出现"package javax.servlet dose not eixt"?
- 现在还没有好办法解决的人事系统
- struts2中如何刷新父页面
- 为何用“桥连接”时,经常会连接占线?而用直连的时候又从来不会?
<%!
Color getRandColor(int fc,int bc){//¸ø¶¨·¶Î§»ñµÃËæ»úÑÕÉ«
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}String sRand="";
for (int i=0;i<4;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand; g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110 g.drawString(rand,13*i+6,16);
}
session.setAttribute("rand",sRand);g.dispose();ImageIO.write(image, "JPEG", response.getOutputStream());
%>jf~~~
同时在 session 中记录下这些字符,待用户提交后首先判断用户输入的值是否与 session 中的值相同,
相同的话,说明是正确的,否则就是不对的。验证码一般采用 Servlet 来实现,也就是说前台页面 img 里的 src 是一个 Servlet 地址。