一、使用純Servlet實現驗證碼
(1)在web.xml配置:
<servlet><br /> <servlet-name>image</servlet-name><br /> <servlet-class>org.test.web.AuthImage</servlet-class><br /></servlet> </p><p><servlet-mapping><br /> <servlet-name>image</servlet-name><br /> <url-pattern>/authImage</url-pattern><br /></servlet-mapping>
(2)servlet源碼
public class AuthImage extends HttpServlet<br />{ </p><p> private static final String CONTENT_TYPE = "text/html; charset=gb2312";<br /> //設定字母的大小,大小<br /> private Font mFont = new Font("Times New Roman", Font.PLAIN, 17);<br /> public void init() throws ServletException<br /> {<br /> super.init();<br /> }<br /> Color getRandColor(int fc,int bc)<br /> {<br /> Random random = new Random();<br /> if(fc>255) fc=255;<br /> if(bc>255) bc=255;<br /> int r=fc+random.nextInt(bc-fc);<br /> int g=fc+random.nextInt(bc-fc);<br /> int b=fc+random.nextInt(bc-fc);<br /> return new Color(r,g,b);<br /> } </p><p> public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException<br /> {<br /> response.setHeader("Pragma","No-cache");<br /> response.setHeader("Cache-Control","no-cache");<br /> response.setDateHeader("Expires", 0);<br /> //表明產生的響應是圖片<br /> response.setContentType("image/jpeg"); </p><p> int width=100, height=18;<br /> BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); </p><p> Graphics g = image.getGraphics();<br /> Random random = new Random();<br /> g.setColor(getRandColor(200,250));<br /> g.fillRect(1, 1, width-1, height-1);<br /> g.setColor(new Color(102,102,102));<br /> g.drawRect(0, 0, width-1, height-1);<br /> g.setFont(mFont); </p><p> g.setColor(getRandColor(160,200)); </p><p> //畫隨機線<br /> for (int i=0;i<155;i++)<br /> {<br /> int x = random.nextInt(width - 1);<br /> int y = random.nextInt(height - 1);<br /> int xl = random.nextInt(6) + 1;<br /> int yl = random.nextInt(12) + 1;<br /> g.drawLine(x,y,x + xl,y + yl);<br /> } </p><p> //從另一方向畫隨機線<br /> for (int i = 0;i < 70;i++)<br /> {<br /> int x = random.nextInt(width - 1);<br /> int y = random.nextInt(height - 1);<br /> int xl = random.nextInt(12) + 1;<br /> int yl = random.nextInt(6) + 1;<br /> g.drawLine(x,y,x - xl,y - yl);<br /> } </p><p> //產生隨機數,並將隨機數字轉換為字母<br /> String sRand="";<br /> for (int i=0;i<6;i++)<br /> {<br /> int itmp = random.nextInt(26) + 65;<br /> char ctmp = (char)itmp;<br /> sRand += String.valueOf(ctmp);<br /> g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));<br /> g.drawString(String.valueOf(ctmp),15*i+10,16);<br /> } </p><p> HttpSession session = request.getSession(true);<br /> session.setAttribute("rand",sRand);<br /> g.dispose();<br /> ImageIO.write(image, "JPEG", response.getOutputStream());<br /> }<br /> public void destroy()<br /> {<br /> }<br />}
(3)頁面顯示
<img src="authImage"/>
二、使用純jsp實現驗證碼
<%@ page language="java" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"<br /> contentType="image/jpeg" pageEncoding="UTF-8"%></p><p><% //設定頁面不緩衝<br /> response.setHeader("Pragma","No-cache");<br /> response.setHeader("Cahce-Control","no-cache");<br /> response.setDateHeader("Expires",0);<br /> //在記憶體中建立圖片<br /> int width=60,height=20;<br /> BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);<br /> //擷取圖形上下文<br /> Graphics g= image.getGraphics();<br /> //產生隨機類<br /> Random random= new Random();<br /> //設定背景顏色<br /> g.setColor(new Color(160,200,100));<br /> g.fillRect(0,0,width,height);<br /> //設定字型<br /> g.setFont(new Font("Times New Roman",Font.PLAIN,18));<br /> //隨機產生50條幹擾線,使圖形中的驗證碼不易被其他的程式探測到<br /> g.setColor(new Color(160,200,200));<br /> for(int i=0;i<50;i++)<br /> {<br /> int x=random.nextInt(width);<br /> int y=random.nextInt(height);<br /> int x1=random.nextInt(width);<br /> int y1=random.nextInt(height);<br /> g.drawLine(x,y,x+x1,y+y1);<br /> }<br /> //隨機產生驗證碼(6位元字)<br /> String sRand="";<br /> for(int i=0;i<6;i++)<br /> {<br /> String rand=String.valueOf(random.nextInt(10));<br /> sRand+=rand;<br /> //將驗證碼顯示到圖象<br /> g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));<br /> g.drawString(rand,13*i+6,16);<br /> }<br /> session.setAttribute("rand",sRand); //////將產生的驗證碼儲存到sesson中<br /> g.dispose();<br /> ImageIO.write(image,"JPEG",response.getOutputStream());<br /> out.clear(); //***********<br /> out=pageContext.pushBody();//**********<br /> %><br />
三、使用Struts2來實現驗證碼
(1)定義一個產生驗證碼的工具類
package com.cn.hospital.util;</p><p>import java.awt.Color;<br />import java.awt.Font;<br />import java.awt.Graphics;<br />import java.awt.image.BufferedImage;<br />import java.io.ByteArrayInputStream;<br />import java.io.ByteArrayOutputStream;<br />import java.util.Random;<br />import javax.imageio.ImageIO;<br />import javax.imageio.stream.ImageOutputStream; </p><p>public class RandomNumUtil {</p><p>private ByteArrayInputStream image;//映像<br />private String str;//驗證碼 </p><p>private RandomNumUtil(){<br />init();//初始化屬性<br />}<br />/*<br />* 取得RandomNumUtil執行個體<br />*/<br />public static RandomNumUtil Instance(){<br />return new RandomNumUtil();<br />}<br />/*<br />* 取得驗證碼圖片<br />*/<br />public ByteArrayInputStream getImage(){<br />return this.image;<br />}<br />/*<br />* 取得圖片的驗證碼<br />*/<br />public String getString(){<br />return this.str;<br />} </p><p>private void init() {<br />// 在記憶體中建立圖象<br />int width=85, height=20;<br />BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);<br />// 擷取圖形上下文<br />Graphics g = image.getGraphics();<br />// 產生隨機類<br />Random random = new Random();<br />// 設定背景色<br />g.setColor(getRandColor(200,250));<br />g.fillRect(0, 0, width, height);<br />// 設定字型<br />g.setFont(new Font("Times New Roman",Font.PLAIN,18));<br />// 隨機產生155條幹擾線,使圖象中的認證碼不易被其它程式探測到<br />g.setColor(getRandColor(160,200));<br />for (int i=0;i<155;i++)<br />{<br />int x = random.nextInt(width);<br />int y = random.nextInt(height);<br />int xl = random.nextInt(12);<br />int yl = random.nextInt(12);<br />g.drawLine(x,y,x+xl,y+yl);<br />}<br />// 取隨機產生的認證碼(6位元字)<br />String sRand="";<br />for (int i=0;i<6;i++){<br />String rand=String.valueOf(random.nextInt(10));<br />sRand+=rand;<br />// 將認證碼顯示到圖象中<br />g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));<br />// 調用函數出來的顏色相同,可能是因為種子太接近,所以只能直接產生<br />g.drawString(rand,13*i+6,16);<br />}<br />//賦值驗證碼<br />this.str=sRand; </p><p>//圖象生效<br />g.dispose();<br />ByteArrayInputStream input=null;<br />ByteArrayOutputStream output = new ByteArrayOutputStream();<br />try{<br />ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);<br />ImageIO.write(image, "JPEG", imageOut);<br />imageOut.close();<br />input = new ByteArrayInputStream(output.toByteArray());<br />}catch(Exception e){<br />System.out.println("驗證碼圖片產生出現錯誤:"+e.toString());<br />} </p><p>this.image=input;/* 賦值映像 */<br />}<br />/*<br />* 給定範圍獲得隨機顏色<br />*/<br />private Color getRandColor(int fc,int bc){<br />Random random = new Random();<br />if(fc>255) fc=255;<br />if(bc>255) bc=255;<br />int r=fc+random.nextInt(bc-fc);<br />int g=fc+random.nextInt(bc-fc);<br />int b=fc+random.nextInt(bc-fc);<br />return new Color(r,g,b);<br />}<br />}<br />
(2)定義一個驗證碼輸出的action
package com.cn.hospital.action;</p><p>import java.io.ByteArrayInputStream;<br />import java.io.ByteArrayOutputStream;</p><p>import org.springframework.context.annotation.Scope;<br />import org.springframework.stereotype.Controller;</p><p>import com.cn.hospital.util.RandomCharUtil;<br />import com.cn.hospital.util.RandomNumUtil;<br />import com.opensymphony.xwork2.ActionContext;<br />import com.opensymphony.xwork2.ActionSupport;</p><p>@Controller("utilAction")<br />@Scope("prototype")<br />public class UtilAction extends ActionSupport{</p><p>private static final long serialVersionUID = -7193209177116825032L;<br />private ByteArrayInputStream inputStream; </p><p>private int width;<br /> private int height;<br /> private int fontSize;<br /> private int codeLength;<br /> private int disturbType;</p><p>public String validNumGenerate() throws Exception{<br />RandomNumUtil rdnu=RandomNumUtil.Instance();<br />this.setInputStream(rdnu.getImage());//取得帶有隨機字串的圖片<br />ActionContext.getContext().getSession().put("random", rdnu.getString());//取得隨機字串放入HttpSession<br />return SUCCESS;<br />} </p><p>public void setInputStream(ByteArrayInputStream inputStream) {<br />this.inputStream = inputStream;<br />} </p><p>public ByteArrayInputStream getInputStream() {<br />return inputStream;<br />}</p><p>}<br />
(3)struts.xml配置
<!-- 產生隨機驗證碼 --><br /><action name="randNum" class="utilAction" method="validNumGenerate"><br /> <result name="success" type="stream"><br /> <param name="contentType">image/jpeg</param><br /> <param name="inputName">inputStream</param><br /> </result><br /> </action>
四、小結
對於java的web技術,歸根究底還是在伺服器端執行的servlet.從上面的三種不同實現中,我們很容易察覺到他們存在一個共同點,那就是返回瀏覽器端的contentType。
servlet:使用response.setContentType(" ");方法來實現
jsp:在<@ page contentType=" ">中來實現
struts2:通過配置<param name="contentType"> </param>來實現
至於驗證碼的產生其實比較簡單,在這裡就不深究了。就此一點小感想,與同行共勉。