首先,我们单单来写在网页中生成验证码图片的servlet
GetImageServlet
public class GetImageServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//get image
//create image in memory
int width = 80;
int height = 40;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//create stack of image
Graphics graphics = image.getGraphics();
//make sure the color of the pen
graphics.setColor(Color.BLACK);
//draw the rect
graphics.fillRect(0, 0, width, height);
graphics.setColor(Color.WHITE);
//draw the rect
graphics.fillRect(1, 1, width-2, height-2);
//input the number
String data="qwertyuioplkjhgfdsazxcvbnmPOIUYTREWQLKJHGFDSAMNBVCXZZZ1234567890";
graphics.setFont(new Font("宋体",Font.BOLD,30));
Random random = new Random();
for (int i = 0; i < 4; i++) {
graphics.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
int index = random.nextInt(62);
String str = data.substring(index,index+1);
graphics.drawString(str, 20*i, 30);
}
<span style="white-space:pre"> </span>//make some fringe in image
for (int i = 0; i < 10; i++) {
graphics.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
graphics.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width),random.nextInt(height));
}
//point the pic
response.setContentType("image/jpeg");
//send the pic to browse
ImageIO.write(image, "jpg", response.getOutputStream());
}
}
写完这个后,在web.xml中配置还了以后,我们在网页中写<img alt="" src="getImageServlet">便可得到随机生成的验证码!!
然后我们需要获得网页中用户输入的验证吗,并且和生成的验证码进行对比
新建另一个GetNumberservle 来进行匹配
String num1 = request.getParameter("用户输入的验证吗");
HttpSessin session = request.getsession();
String num2 = (String)sesion.getAttribute("number",buffer);
//and then you can <span id="blng_part_tran_2_1" data-aligning="#blng_part_src_2_1,#blng_part_tran_2_1" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; color: rgb(67, 67, 67); font-family: Tahoma, Arial; line-height: 24px; background-color: rgb(242, 242, 242); ">have</span><span style="color: rgb(67, 67, 67); font-family: Tahoma, Arial; line-height: 24px; background-color: rgb(242, 242, 242); "> </span><span id="blng_part_tran_2_2" data-aligning="#blng_part_src_2_6,#blng_part_tran_2_2" class="highLight" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; background-color: rgb(189, 213, 238); color: rgb(67, 67, 67); font-family: Tahoma, Arial; line-height: 24px; ">some comparison</span><span style="color: rgb(67, 67, 67); font-family: Tahoma, Arial; line-height: 24px; background-color: rgb(242, 242, 242); "> .</span>
在这之前我们需要在之前的getImageServlet 添加一个功能
就是在GetImageServlet中把随机生成的验证码放到session 中
StringBuffer buffer= new StringBuffer();
<pre name="code" class="java"> for (int i = 0; i < 4; i++) {
graphics.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
int index = random.nextInt(62);
String str = data.substring(index,index+1);
graphics.drawString(str, 20*i, 30);
<span style="white-space:pre"> </span>buffer.append(str);
}
HttpSessin session = request.getsession();
sesion.setAttribute("number",buffer);
这样就ok啦,简单的验证码功能书写完毕。