JSP验证码问题

首页
<img src="RandomCodeCtrl"/>

web.xml
<servlet>
<servlet-name>RandomCodeCtrl</servlet-name>
<servlet-class>com.chainway.util.RandomCodeCtrl</servlet-class>
<load-on-startup>4</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RandomCodeCtrl</servlet-name>
<url-pattern>/RandomCodeCtrl</url-pattern>
</servlet-mapping>

RandomCodeCtrl:
package com.chainway.util;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RandomCodeCtrl extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("image/jpeg");
resp.setHeader("Pragma","No-cache");
resp.setHeader("Cache-Control","no-cache");
resp.setDateHeader("Expires", 0);
RandomCode rc = new RandomCode();
try{
rc.getRandcode(req,resp);
}catch(Exception e){
System.err.println(e);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

//生成类
//数字文字图片验证码
package com.chainway.util;

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;
public class RandomCode {
/**
* 随机取得一个字体
* @param Random random 随机数
* @return Font 返回一个新字体
*/
private synchronized Font getsFont(Random random){
return new Font("Fixedsys",Font.CENTER_BASELINE,18);
}
/**
* 返回一个随机颜色
* @param int fc 随机数
* @param int bc 随机数
* @param Random random 随机数
* @return Color 返回一个新颜色
*/
synchronized Color getRandColor(int fc,int bc,Random random){
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc-6);
int g=fc+random.nextInt(bc-fc-4);
int b=fc+random.nextInt(bc-fc-8);
return new Color(r,g,b);
}
/**
* 生成随机数图片
*/
public synchronized void getRandcode(HttpServletRequest request,HttpServletResponse response)throws Exception{
System.setProperty("java.awt.headless","true");
HttpSession session = request.getSession();
int width=80, height=26;//设置图片大小
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.fillRect(0, 0, width, height);//设定边框
g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));
g.setColor(getRandColor(111,133,random));
//产生随机线
for (int i=0;i<11;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(13);
int yl = random.nextInt(15);
g.drawLine(x,y,x+xl,y+yl);
}
//产生随机点
g.setColor(getRandColor(130,150,random));
//产生5个随机数
String sRand="";
for (int i=0;i<5;i++){
g.setFont(getsFont(random));
g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
//String rand=String.valueOf(getRandomString(random.nextInt(36)));
String rand=String.valueOf(getRandomString(random.nextInt(10)));
sRand+=rand;
g.translate(random.nextInt(3),random.nextInt(3));
g.drawString(rand,13*i,16);
}
session.removeAttribute("Rand");
session.setAttribute("Rand",sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
}

public synchronized String getRandomString(int num){
String randstring = "0123456789";
//String randstring = "0123456789abcdefghijklmnopqrstuvwxyz";
return String.valueOf(randstring.charAt(num));
}

}

//登陆判断方法
从session里面取验证码数字进行比较 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值