Java 登陸驗證碼

生成驗證的類:RandomCode

 

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;

import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@SuppressWarnings("serial")
public class RandomCode extends HttpServlet {

 /**
  * The doGet method of the servlet. <br>
  * 
  * This method is called when a form. has its tag value method equals to
  * get.
  * 
  * @param request
  *            the request send by the client to the server
  * @param response
  *            the response send by the server to the client
  * @throws ServletException
  *             if an error occurred
  * @throws IOException
  *             if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  this.doPost(request, response);
 }

 /**
  * The doPost method of the servlet. <br>
  * 
  * This method is called when a form. has its tag value method equals to
  * post.
  * 
  * @param request
  *            the request send by the client to the server
  * @param response
  *            the response send by the server to the client
  * @throws ServletException
  *             if an error occurred
  * @throws IOException
  *             if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  Random random = new Random();

  int width = 60, height = 20;
  // 創建BufferedImage對象,設置圖片的長度寬度和色彩。
  BufferedImage image = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_RGB);
  OutputStream os = response.getOutputStream();
  // 取得Graphics對象,用來繪製圖片
  Graphics g = image.getGraphics();
  // 繪製圖片背景和文字,釋放Graphics對象所佔用的資源。
  g.setColor(getRandColor(200, 250));
  // 設置內容生成的位置
  g.fillRect(0, 0, width, height);
  // 設置內容的字體和大小
  g.setFont(new Font("Times New Roman", Font.PLAIN, 20));

  // 設置內容的顏色:主要爲生成圖片背景的線條
  g.setColor(getRandColor(160, 200));
  // 畫邊框。
  g.setColor(Color.BLACK);
  g.drawRect(0, 0, width - 1, height - 1);
  // 隨機產生160條幹擾線,使圖象中的認證碼不易被其它程序探測到。
  g.setColor(Color.BLACK);
  // 圖片背景上隨機生成155條線條,避免通過圖片識別破解驗證碼
  for (int i = 0; i < 30; 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[] s = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
    "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
    "X", "Y", "Z" };
  String content = "";
  for (int i = 0; i < 4; i++) {
   String rand = "";
   if (random.nextBoolean()) {
    rand = String.valueOf(random.nextInt(10));
   } else {
    int index = random.nextInt(25);
    rand = s[index];
   }
   content += rand;
   g.setColor(new Color(20 + random.nextInt(10), 20 + random
     .nextInt(110), 20 + random.nextInt(110)));
   g.drawString(rand, 13 * i + 6, 16);
  }

  // 釋放此圖形的上下文以及它使用的所有系統資源,類似於關閉流
  g.dispose();

  // 將生成的驗證碼值(即運算結果的值)放到session中,以便於後臺做驗證。
  HttpSession session = request.getSession();
  session.setAttribute("result", content);

  // 通過ImageIO對象的write靜態方法將圖片輸出。
  ImageIO.write(image, "JPEG", os);
  os.close();
 }

 /**
  * 生成隨機顏色
  * 
  * @param fc
  * @param bc
  * @return
  */
 public 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);
 }
}

 

 

 

 

web.xml配置:

 

 <!-- 驗證碼 -->
 <servlet>
    <description>生成驗證碼的Servlet</description>
    <display-name>RandomCode</display-name>
    <servlet-name>RandomCode</servlet-name>
    <servlet-class>com.gxh.tools.RandomCode</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>RandomCode</servlet-name>
    <url-pattern>*.RandomCode</url-pattern>
</servlet-mapping>

 

 

 

 

login.jsp頁面:

 

 <script type="text/javascript">
   function changeImg()
   {    document.getElementById("validatecodeimg").src=Math.round(Math.random()*10000)+".RandomCode ";
   }
</script>
  <body>
  <div align="center">
  
   <img id="validatecodeimg" alt="看不清?點擊刷新" οnclick="javascript:changeImg()" src="first.RandomCode " align="top" />  
<a href="javascript:changeImg()">看不清?點擊刷新</a>

</div>
  </body>
</html>

發佈了4 篇原創文章 · 獲贊 7 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章