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

验证码(java登陆)

谁有一般系统登陆时的验证码的源码(就像我们登陆网银时需要输入的验证那个一样),灰常感谢,在线等!!! --------------------编程问答-------------------- 我有struts2生成验证码的源码
--------------------编程问答-------------------- 我需要一份struts生成验证码的源码,希望上楼的大哥给一份啊 --------------------编程问答-------------------- http://www.cnblogs.com/ITtangtang/archive/2012/04/28/2475520.html --------------------编程问答-------------------- ResponseDemo2.java//这是servlet
package responseDemo;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
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;
//验证码:
public class ResponseDemo2 extends HttpServlet {
public static final int WIDTH=100;
public static final int HEIGHT=25;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BufferedImage  image=new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
//1,設置背景顏色
setBackGround(g);

//2,设置边框
setBorder(g);
//3,画干扰线
drawRandomLine(g);
//4,写随机数
drawRandomNum((Graphics2D) g);
//5,图形写给浏览器
response.setContentType("image/jpeg");

//发头控制浏览器不要缓存

response.setDateHeader("expries", -1);
response.setHeader("cache-contorl","no-cache");
response.setHeader("Pragma","no-cache");

ImageIO.write(image, "jpg", response.getOutputStream());
}

private void setBackGround(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
}

private void setBorder(Graphics g)
{
g.setColor(Color.gray);
g.drawRect(1, 1,WIDTH-2, HEIGHT-2);
}

private void drawRandomLine(Graphics g)
{
g.setColor(Color.gray);
for(int i=0;i<4;i++)
{
int x1=new Random().nextInt(WIDTH);
int y1=new Random().nextInt(HEIGHT);

int x2=new Random().nextInt(WIDTH);
int y2=new Random().nextInt(HEIGHT);

g.drawLine(x1, y1, x2, y2);
}
}

private void drawRandomNum(Graphics2D  g)
{

g.setFont(new Font("宋体",Font.BOLD,20));
int x=5;
for(int i=0;i<4;i++)
{
int r=new Random().nextInt(225);
int gr=new Random().nextInt(225);
int b=new Random().nextInt(225);
g.setColor(new Color(r,gr,b));

int degree=new Random().nextInt()%30;
String number=new Random().nextInt(9)+1+"";

g.rotate(degree*Math.PI/180,x,20);
g.drawString(number,x,20);
g.rotate(-degree*Math.PI/180,x,20);
x+=25;
}

}
/**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @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 doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}

}


ResponseDemo2.jsp
<%@ 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>
<script type="text/javascript">
function changeImage(img)
{
img.src=img.src+"?"+new Date().getTime();
}
</script>

  </head>
  
  <body>
  <form action=""> 
    用戶名:<input type="text" name="username"/><br/>
    密碼:<input type="password" name="password"/><br/>
    認證碼:<input type="text" name="checkcode"/>
    <img src="/Week01/servlet/ResponseDemo2"  onclick="changeImage(this)" alt="換一張" style="cusor:hand"/><br/>
    <input type="submit" value="註冊"/>
</form>
  </body>
</html> --------------------编程问答-------------------- test。。。。 --------------------编程问答-------------------- 求struts2登陆带验证码的action源码~~~~ --------------------编程问答-------------------- 网上搜搜就有了 --------------------编程问答-------------------- 建议楼主先在网上搜一下  --------------------编程问答-------------------- 我这里有一段代码,用于生成图片验证码的,代码写的还是不错的,邮箱告诉我,我可以发给你参考下...
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,