java用JSP和servlet实现验证码

第一种:

index.jsp

 

                          src="${pageContext.request.contextPath}/image.jsp"  border="1" >

image.jsp



<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding="utf-8"%>

<%!

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

}

%>

<%

out.clear();                           /*这句针对resin服务器,如果是tomacat可以不要这句*/

response.setHeader("Pragma","No-cache");

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

response.setDateHeader("Expires", 0);

int width=60, height=20;

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();

Random random = new Random();

g.setColor(getRandColor(200,250));

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

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

g.setColor(getRandColor(160,200));

for (int i=0;i<155;i++)

{

int x = random.nextInt(width);

int y = random.nextInt(height);

int xl = random.nextInt(12);

int yl = random.nextInt(12);

g.drawLine(x,y,x+xl,y+yl);

}

String sRand="";

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

String rand=String.valueOf(random.nextInt(10));

sRand+=rand;

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

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

}

// 将认证码存入SESSION

session.setAttribute("sRand",sRand);

g.dispose();

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

%>

第二种:

CodeServlet.java

package com.lxs;


import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.io.OutputStream;

import java.util.Random;


import javax.imageio.ImageIO;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


public class CodeServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    

    protected void service(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        String codes="abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ0123456789";    

        BufferedImage img=new BufferedImage(80,30,BufferedImage.TYPE_3BYTE_BGR);

        Graphics g=img.getGraphics();

        //填充图片

        g.setColor(new Color(255,255,255));

        g.fillRect(0, 0, 80, 30);

        //画入五个随机数

        Random rm=new Random();

        StringBuffer sb=new StringBuffer();

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

            int index=rm.nextInt(codes.length());

            char code=codes.charAt(index);

            g.setColor(new Color(rm.nextInt(256),rm.nextInt(256),rm.nextInt(256)));

            g.setFont(new Font("宋体", Font.BOLD, 25));

            g.drawString(code+"", 2+15*i, 22);

            sb.append(code);

        }

        

        //画干扰线

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

            g.setColor(new Color(rm.nextInt(256),rm.nextInt(256),rm.nextInt(256)));

            g.drawLine(rm.nextInt(100), rm.nextInt(50), rm.nextInt(100), rm.nextInt(50));

        }

        

        //将验证码图片回写给浏览器

        response.setContentType("image/jpeg;charset=utf-8");

        OutputStream out=response.getOutputStream();

        ImageIO.write(img, "jpeg", out);

        

        //将随机字符串保存在session中

        request.getSession().setAttribute("code", sb.toString());

    }


}

index.jsp

style="width: 80px; height: 30px; border: 1px solid black;" />
换一张



web.xml

 CodeServlet
    com.yinhe.web.servlet.CodeServlet
 
 
    CodeServlet
    /checkCode
 






你可能感兴趣的:(JAVA学习)