servlet生成验证码


package image;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
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.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ImageCode extends HttpServlet {

/**
* Constructor of the object.
*/
public ImageCode() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

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

doPost(request,response);
}

/**
* 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 {
try{
int width = 50;
int height = 20;
response.setContentType("image/jpeg");
String s = "";
Random random = new Random();
for(int i=0; i<4; i++) {
s += String.valueOf(random.nextInt(9));
}
System.out.println("验证码:"+s);
HttpSession session = request.getSession();
session.setAttribute("validataCode", s);
ServletOutputStream out = response.getOutputStream();
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphics g = image.getGraphics();
g.setColor(getRandColor(200, 250));
//填充矩形
g.fillRect(0, 0, width, height);
Font mFont = new Font("Times New Roman", Font.BOLD, 18);// 设置字体
g.setFont(mFont);
g.setColor(getRandColor(160, 200));
Random r = new Random();
//随机的生成一些线
for (int i = 0; i < 155; i++) {
int x2 = r.nextInt(width);
int y2 = r.nextInt(height);
int x3 = r.nextInt(12);
int y3 = r.nextInt(12);
g.drawLine(x2, y2, x2 + x3, y2 + y3);
}

g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));

g.drawString(s, 2, 16);
g.dispose();
// 输出图象到页面
ImageIO.write((BufferedImage) image, "JPEG", out);
out.close();
}catch(Exception e) {
e.printStackTrace();
}

}

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


/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}


WEB.XML配置

<servlet>
<servlet-name>ImageCode</servlet-name>
<servlet-class>image.ImageCode</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ImageCode</servlet-name>
<url-pattern>/servlet/ImageCode</url-pattern>
</servlet-mapping>

页面显示,需要jquery

<script type="text/javascript" src="js/jquery-1.4.js"></script>
<img id="vcode_img" src="<%=request.getContextPath() %>/servlet/ImageCode">
<a href="#" onClick="$('#vcode_img').attr('src','<%=request.getContextPath() %>/servlet/ImageCode?t='+(new Date().getTime()));return false;">看不清楚,换一张</a>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值