JSP驗證碼問題

來源:互聯網
上載者:User
首頁
<img src="RandomCodeCtrl"/>

web.xml
<servlet>
<servlet-name>RandomCodeCtrl</servlet-name>
<servlet-class>com.chainway.util.RandomCodeCtrl</servlet-class>
<load-on-startup>4</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RandomCodeCtrl</servlet-name>
<url-pattern>/RandomCodeCtrl</url-pattern>
</servlet-mapping>

RandomCodeCtrl:
package com.chainway.util;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RandomCodeCtrl extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("image/jpeg");
resp.setHeader("Pragma","No-cache");
resp.setHeader("Cache-Control","no-cache");
resp.setDateHeader("Expires", 0);
RandomCode rc = new RandomCode();
try{
rc.getRandcode(req,resp);
}catch(Exception e){
System.err.println(e);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

//產生類
//數字文字圖片驗證碼
package com.chainway.util;

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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RandomCode {
/**
* 隨機取得一個字型
* @param Random random 隨機數
* @return Font 返回一個新字型
*/
private synchronized Font getsFont(Random random){
return new Font("Fixedsys",Font.CENTER_BASELINE,18);
}
/**
* 返回一個隨機顏色
* @param int fc 隨機數
* @param int bc 隨機數
* @param Random random 隨機數
* @return Color 返回一個新顏色
*/
synchronized Color getRandColor(int fc,int bc,Random random){
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc-6);
int g=fc+random.nextInt(bc-fc-4);
int b=fc+random.nextInt(bc-fc-8);
return new Color(r,g,b);
}
/**
* 產生隨機數圖片
*/
public synchronized void getRandcode(HttpServletRequest request,HttpServletResponse response)throws Exception{
System.setProperty("java.awt.headless","true");
HttpSession session = request.getSession();
int width=80, height=26;//設定圖片大小
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.fillRect(0, 0, width, height);//設定邊框
g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));
g.setColor(getRandColor(111,133,random));
//產生隨機線
for (int i=0;i<11;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(13);
int yl = random.nextInt(15);
g.drawLine(x,y,x+xl,y+yl);
}
//產生隨機點
g.setColor(getRandColor(130,150,random));
//產生5個隨機數
String sRand="";
for (int i=0;i<5;i++){
g.setFont(getsFont(random));
g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
//String rand=String.valueOf(getRandomString(random.nextInt(36)));
String rand=String.valueOf(getRandomString(random.nextInt(10)));
sRand+=rand;
g.translate(random.nextInt(3),random.nextInt(3));
g.drawString(rand,13*i,16);
}
session.removeAttribute("Rand");
session.setAttribute("Rand",sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
}

public synchronized String getRandomString(int num){
String randstring = "0123456789";
//String randstring = "0123456789abcdefghijklmnopqrstuvwxyz";
return String.valueOf(randstring.charAt(num));
}

}

//登陸判斷方法
從session裡面取驗證碼數字進行比較 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.