即興的靈感

          思維是一種藝術; 藝術需要靈感。

          博客好友

          最新評論

          JSP驗證碼大全之Servlet實現(一)

          ???在以上的內容中闡述了在JSP中產生并實現了數字驗證碼中文驗證碼的過程,以及如何在JSP中驗證碼調用和解決中文問題,并對驗證碼的使用做了分析。本文將介紹另一種J2EE中驗證碼的產生跟使用,即在Servlet中定義驗證碼的產生并使用,通過將驗證碼的生成封裝到JAVA類中,更好的達到代碼跟頁面分離的效果,因此提倡使用該方法。
          ???五、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");
          ?? }
          }

          ???以上即是通過Servlet中創建并生產數字驗證碼的源碼分析,下一篇文章將介紹在Servlet中生成數字字母混合驗證碼的過程,并說明如何對Servlet中的驗證碼進行使用

          ?
          鳳凰涅槃/浴火重生/馬不停蹄/只爭朝夕
          ???? 隱姓埋名/低調華麗/簡單生活/完美人生

          posted on 2008-12-01 18:00 poetguo 閱讀(5596) 評論(4)  編輯  收藏 所屬分類: JSPJAVA

          評論

          # re: JSP驗證碼大全之Servlet實現(一) 2010-05-03 10:58 dsfgsd

          dsfgds  回復  更多評論   

          # re: JSP驗證碼大全之Servlet實現(一) 2010-05-03 10:58 dsfgsd

          jfgyjdyj  回復  更多評論   

          # re: JSP驗證碼大全之Servlet實現(一) 2010-05-03 10:59 dsfgsd

          dgkmg  回復  更多評論   

          # re: JSP驗證碼大全之Servlet實現(一) 2014-07-06 20:52 肖日明

          好  回復  更多評論   

          主站蜘蛛池模板: 武陟县| 乐亭县| 锦州市| 镇远县| 香河县| 工布江达县| 武定县| 富裕县| 香港 | 沈阳市| 龙川县| 巴中市| 绥滨县| 乐平市| 石屏县| 平定县| 莱州市| 惠水县| 柯坪县| 东乡| 高雄县| 紫云| 依安县| 兰州市| 内江市| 绥德县| 宁夏| 商城县| 无为县| 宜章县| 区。| 托克托县| 大新县| 璧山县| 永丰县| 沙湾县| 盘山县| 惠东县| 安庆市| 福鼎市| 大埔区|