java web 生成验证码_java web 生成验证码

最新版本的eclipse在创建项目的时候没有自动生成web.xml,发觉是在新建Dynamic Web Project时,Dynamic

web module

version默认选择了3.0,原来是版本更新,自动默认的选择的是注解的形式,如果想在建立项目的时候自动生成web.xml,则将版本选择为2.5

即可。

index.jsp:

import = "java.util.*" pageEncoding="UTF-8"%>

html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

登录

function refresh(obj){

obj.src="Image?"+Math.random();

}

输入验证码

Image:(Servlet类)

package com.wf.web;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* Servlet implementation class Image

*/

public class Image extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()

*/

public Image() {

super();

// TODO Auto-generated constructor stub

}

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

response.setContentType("image/jpeg");// 设置相应的类型 告诉浏览器输出的类型为图片类型

response.setHeader("Pragma", "No-cache");// 设置响应头告诉浏览器不需要缓存此内容

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

response.setDateHeader("Expire", 0);

RandomValidateCode randomValidateCode = new RandomValidateCode();

try {

randomValidateCode.getRandcode(request, response);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

}

RandomValidateCode:生成验证码

package com.wf.web;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.util.Random;

import javax.imageio.ImageIO;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

/*

*

*@author meson

*

*@time 2014年9月28日上午9:41:19

*

*/

public class RandomValidateCode {

public static final String RANDOMCODEKEY = "RADNOMVALIDATECODEKEY";// 放到session中的key

private Random random = new Random();

private String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";// 随机产生的字符串

private int width = 150;// 随机产生验证码的宽度和高度

private int height = 55;

private int lineSize = 20;// 干扰线的数量

private int stringNum = 6;// 随机产生字符的数量

/*

* 获得字体

*/

private Font getFont() {

return new Font("Fixedsys", Font.CENTER_BASELINE,28);

}

// 获得随机的颜色

private Color getRandColor(int fc, int bc) {

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);

}

/*

* 生成随机图片

*/

public void getRandcode(HttpServletRequest request,

HttpServletResponse response) {

// TODO Auto-generated method stub

HttpSession session = request.getSession();

BufferedImage image = new BufferedImage(width, height,

BufferedImage.TYPE_3BYTE_BGR);

Graphics g = image.getGraphics();

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

g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));

g.setColor(getRandColor(110, 113));

for (int i = 0; i <= lineSize; i++) {

drowLine(g);

}

String randomString = " ";

for (int i = 1; i <= stringNum; i++) {

randomString = drowString(g, randomString, i);

}

System.out.println(randomString);

session.removeAttribute(RANDOMCODEKEY);

session.setAttribute(RANDOMCODEKEY, randomString);

g.dispose();

try {

ImageIO.write(image, "JPEG", response.getOutputStream());

} catch (Exception e) {

e.printStackTrace();

}

}

/*

* 绘制字符串

*/

private String drowString(Graphics g, String randomString, int i) {

// TODO Auto-generated method stub

g.setFont(getFont());

g.setColor(new Color(random.nextInt(101), random.nextInt(111), random

.nextInt(121)));

String rand = String.valueOf(getRandomString(random

.nextInt(randomString.length())));

randString += rand;

g.translate(random.nextInt(3), random.nextInt(3));

g.drawString(rand, 13 * i, 16);

return randString;

}

/*

* 获取随机的字符创

*/

private String getRandomString(int nextInt) {

// TODO Auto-generated method stub

return String.valueOf(randString.charAt(nextInt));

}

/*

* 绘制干扰线

*/

private void drowLine(Graphics g) {

// TODO Auto-generated method stub

int x = random.nextInt(width);

int y = random.nextInt(height);

int x1 = random.nextInt(13);

int y1 = random.nextInt(15);

g.drawLine(x, y, x1, y1);

}

}

web.xml配置文件:

Rand

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

Image

Image

com.wf.web.Image

Image

/Image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值