JSP驗證碼大全之Servlet實現(一)
???五、Servlet中實現四位數字驗證碼
???以下為在Servlet中實現四位數字驗證碼的源碼分析。?
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.*;
/*
?* 功能:調用AuthCodeServlet可以生成一個4位數字的驗證碼圖片,驗證碼的圖片寬度和高度可以通過配置文件進行定義
?* 驗證碼調用格式為: /servlet/AuthCodeServlet?w=78&h=32
?* 或者使用默認長寬/servlet/AuthCodeServlet
?*/
?? public class AuthCodeServlet extends HttpServlet {
?? // 處理post
??? public void doPost(HttpServletRequest req,HttpServletResponse res)
??? throws ServletException,IOException {
??? doGet(req,res);
?}
?? //設置字體
?? private Font mFont=new Font("Times New Roman", Font.PLAIN,16);
?? public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
???? HttpSession session=request.getSession();
???? response.setContentType("image/gif");
???? response.setHeader("Pragma","No-cache");
???? response.setHeader("Cache-Control","no-cache");
???? response.setDateHeader("Expires", 0);
???? int width=70; //驗證碼默認長度
???? int height=24; //驗證碼默認寬度
???? if(request.getParameter("w")!=null && !request.getParameter("w").equals(""))
????? width = Integer.parseInt(request.getParameter("w"));
???? if(request.getParameter("h")!=null && !request.getParameter("h").equals(""))
????? height = Integer.parseInt(request.getParameter("h"));
????????????????
???? ServletOutputStream out=response.getOutputStream(); //獲取輸出流
???? BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //新建驗證圖片,并設置驗證碼圖片的大小
???? Graphics gra=image.getGraphics(); //獲取圖形上下文
???? Random random=new Random();
???? gra.setColor(getRandColor(260,210));??? //設置驗證碼的圖片背景色
???? gra.fillRect(0,0,width,height);
???? gra.setColor(Color.BLUE); //設置字體色為藍色
???? gra.setFont(mFont); //設置定義的字體格式
???? // 隨機產生254條干擾直線,使圖象中的驗證碼不易被解析程序分析到
???? gra.setColor(getRandColor(110,240));
???? for (int i=0;i<254;i++)
???? {
????? int x = random.nextInt(width);
????? int y = random.nextInt(height);
???????????? int xl = random.nextInt(63);
???????????? int yl = random.nextInt(64);
????? gra.drawLine(x,y,x+xl,y+yl);
???? }
???? // 取隨機產生的驗證碼(4位數字)
???? String sRand="";
???? for (int i=0;i<4;i++){
???? String rand=String.valueOf(random.nextInt(353));
???? sRand+=rand;
???? // 將認證碼顯示到圖象中
????? gra.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
???? //調用隨機函數構建隨機顏色三素
???????? gra.drawString(rand,13*i+6,16);
???? }
???????? session.setAttribute("authCode",sRand);
???????? JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
???????? encoder.encode(image);
?}
?? static Color getRandColor(int ff,int cc){
????????? //給定范圍獲得隨機顏色
????????? Random random = new Random();
????????? if(fc>255) ff=255;
????????? if(bc>255) cc=255;
????????? int r=ff+random.nextInt(cc-ff);
????????? int g=ff+random.nextInt(cc-ff);
????????? int b=ff+random.nextInt(cc-ff);
????????? return new Color(r,g,b);
?? }
??
?? static public String getAuthCode(HttpSession session){
??? //返回驗證碼
??? return (String)session.getAttribute("AuthCode");
?? }
}
?
鳳凰涅槃/浴火重生/馬不停蹄/只爭朝夕
???? 隱姓埋名/低調華麗/簡單生活/完美人生
posted on 2008-12-01 18:00 poetguo 閱讀(5596) 評論(4) 編輯 收藏 所屬分類: JSP 、JAVA