javaweb开发之制作简单验证码
使用xml对页面进行配置,验证码模块具体配置如下,code.jhtml为验证码存放位置
<servlet>
<servlet-name>CodeAction</servlet-name>
<servlet-class>campsg.qunawan.action.CodeAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CodeAction</servlet-name>
<url-pattern>/code.jhtml</url-pattern>
</servlet-mapping>
html页面中,页面脚本如下(该页面为登录页面,不同于code.jhtml)
<li>
<span class="identifying">
<img width="120px" height="40px" src="/PRJ_WTP_JEE_005/code.jhtml"
onclick="javascript:this.src='/PRJ_WTP_JEE_005/code.jhtml?id='+new Date().getMilliseconds()">
</span>
<input type="text" name="code" id="ident" class="text identify" value="验证码" />
</li>
其中的onclick()响应是为了保证每次刷新的能获取到正确可用的路径
制作验证码另外还需如下准备工作:制作字符库,颜色库,字体库(为了方便,统一成了一样),图像长、宽度等信息
在src目录下的包中,新建一个类,暂且命名为Constant,将所需信息写入
public class Constants {
// 验证码的字体库
public static Font[] codeFont = { new Font("Times New Roman", Font.PLAIN, 30),
new Font("Times New Roman", Font.PLAIN, 30), new Font("Times New Roman", Font.PLAIN, 30),
new Font("Times New Roman", Font.PLAIN, 30) };
// 验证码数字颜色库
public static Color[] color = { Color.BLACK, Color.RED, Color.DARK_GRAY, Color.BLUE };
// 验证码的字符库
public static final String IMAGE_CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
// 验证码的宽度
public static final Integer IMAGE_WIDTH = 120;
// 验证码的高度
public static final Integer IMAGE_HEIGHT = 40;
}
在src目录的包下新建一个类,暂且命名为CodeAction,使其继承HttpServlet类,写入生成随机验证码代码
public class CodeAction extends HttpServlet{
private static final long serialVersionUID = -175449573736935176L;
private String codeNumbers="";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("执行CodeAction方法");
BufferedImage image=new BufferedImage(
Constants.IMAGE_WIDTH,
Constants.IMAGE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics2D g=(Graphics2D) image.getGraphics();
g.setColor(createRandomColor(200,300));
g.fillRect(0, 0, Constants.IMAGE_WIDTH, Constants.IMAGE_HEIGHT);
/**
* 画出验证码字符
*/
for(int i=0;i<4;i++)
{
drawString(g, i);
}
System.out.println("生成验证码:"+this.codeNumbers);
//绘制干扰线
drawNoiseLine(g, 5);
this.codeNumbers="";
ServletOutputStream sos=response.getOutputStream();
ImageIO.write(image, "GIF", sos);
sos.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request,response);
}
/**
* 绘制验证码字符串
* @param g 图层
* @param i 第几位字符
*/
private void drawString(Graphics2D g,int i) {
Random random=new Random();
Integer j=random.nextInt(Constants.IMAGE_CHAR.length());
String number=Constants.IMAGE_CHAR.substring(j, j+1);
g.setFont(Constants.codeFont[i]);
g.setColor(Constants.color[i]);
g.drawString(number, 15*i+20, 30);
this.codeNumbers+=number;
}
/**
* 随机生成图片背景颜色
* @param bcf 最小值
* @param bcl 最大值
* @return RGB颜色
*/
private Color createRandomColor(int bcf,int bcl ) {
Color color=null;
boolean flag=true;
while(flag)
{
int r=bcf+new Random().nextInt(bcl-bcf);
int g=bcf+new Random().nextInt(bcl-bcf);
int b=bcf+new Random().nextInt(bcl-bcf);
if((r>=0&&r<=255)&&(g>=0&&g<=255)&&(b>=0&&b<=255))
{
color=new Color(r,g,b);
flag=false;
}
}
return color;
}
/**
* 绘制干扰线条
* @param g 图层
* @param NoiseLineNumber 干扰线数量
*/
private void drawNoiseLine(Graphics2D g,int NoiseLineNumber) {
g.setColor(createRandomColor(100,250));
int x1=0,y1=0,x2=0,y2=0;
for(int i=0;i<NoiseLineNumber;i++)
{
x1=(int) (1+(Math.random()*120));
y1=(int) (1+(Math.random()*40));
x2=(int) (1+(Math.random()*120));
y2=(int) (1+(Math.random()*40));
g.setStroke(new BasicStroke(2, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));
g.drawLine(x1, y1, x2, y2);
}
}
}
结果如下