日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

ssh生成隨機數(shù)字驗證碼操作步驟_JSP教程

編輯Tag賺U幣

推薦:JSP內(nèi)置對象:Request和Response的簡單介紹及使用
JSP內(nèi)置對象:Request和Response的簡單介紹及使用,需要的朋友可以參考一下

1、login.jsp頁面程序
復制代碼 代碼如下:hl5o.cn

<script type="text/javascript">
function changeValidateCode(obj) {
//獲取當前的時間作為參數(shù),無具體意義
var timenow = new Date().getTime();
//每次請求需要一個不同的參數(shù),否則可能會返回同樣的驗證碼
//這和瀏覽器的緩存機制有關系,也可以把頁面設置為不緩存,這樣就不用這個參數(shù)了。
obj.src="rand.action?d="+timenow;
}
</script>

在表單中添加下面這句話:
復制代碼 代碼如下:hl5o.cn

<s:text name="random"></s:text>:<s:textfield name="rand" size="5"></s:textfield><img src="rand.action" onclick="changeValidateCode(this)" title="點擊圖片刷新驗證碼"/>

2、RandomNumUtil.java 生成驗證碼的類文件
復制代碼 代碼如下:hl5o.cn

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
public class RandomNumUtil {
private ByteArrayInputStream image;//圖像
private String str;//驗證碼
private RandomNumUtil(){
init();//初始化屬性
}
/*
* 取得RandomNumUtil實例
*/
public static RandomNumUtil Instance(){
return new RandomNumUtil();
}
/*
* 取得驗證碼圖片
*/
public ByteArrayInputStream getImage(){
return this.image;
}
/*
* 取得圖片的驗證碼
*/
public String getString(){
return this.str;
}
private void init() {
// 在內(nèi)存中創(chuàng)建圖象
int width=85, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 獲取圖形上下文
Graphics g = image.getGraphics();
// 生成隨機類
Random random = new Random();
// 設定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
// 設定字體
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
// 隨機產(chǎn)生155條干擾線,使圖象中的認證碼不易被其它程序探測到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;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);
}
// 取隨機產(chǎn)生的認證碼(6位數(shù)字)
String sRand="";
for (int i=0;i<6;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
// 將認證碼顯示到圖象中
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
// 調(diào)用函數(shù)出來的顏色相同,可能是因為種子太接近,所以只能直接生成
g.drawString(rand,13*i+6,16);
}
//賦值驗證碼
this.str=sRand;
//圖象生效
g.dispose();
ByteArrayInputStream input=null;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try{
ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
ImageIO.write(image, "JPEG", imageOut);
imageOut.close();
input = new ByteArrayInputStream(output.toByteArray());
}catch(Exception e){
System.out.println("驗證碼圖片產(chǎn)生出現(xiàn)錯誤:"+e.toString());
}
this.image=input;/* 賦值圖像 */
}
/*
* 給定范圍獲得隨機顏色
*/
private 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);
}
}

3、RandomAction.java 生成驗證碼的action程序
復制代碼 代碼如下:hl5o.cn

import java.io.ByteArrayInputStream;
import com.mxl.util.RandomNumUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class RandomAction extends ActionSupport{
private ByteArrayInputStream inputStream;
public String execute() throws Exception{
RandomNumUtil rdnu=RandomNumUtil.Instance();
this.setInputStream(rdnu.getImage());//取得帶有隨機字符串的圖片
ActionContext.getContext().getSession().put("random", rdnu.getString());//取得隨機字符串放入HttpSession
return SUCCESS;
}
public void setInputStream(ByteArrayInputStream inputStream) {
this.inputStream = inputStream;
}
public ByteArrayInputStream getInputStream() {
return inputStream;
}
}

4、LoginAction.java 驗證驗證碼的action
復制代碼 代碼如下:hl5o.cn

private String rand; //表單中的rand
public String getRand() {
return rand;
}
public void setRand(String rand) {
this.rand = rand;
}
//從session中取出RandomAction.java 中生成的驗證碼random
String arandom=(String)(ActionContext.getContext().getSession().get("random"));
//下面就是將session中保存驗證碼字符串與客戶輸入的驗證碼字符串對比了
if(arandom.equals(this.getRand())) {
ActionContext.getContext().getSession().put("user", this.getUsername());
return SUCCESS;
}
else {
return ERROR;
}

5、配置struts.xml文件
復制代碼 代碼如下:hl5o.cn

<!-- Random驗證碼 -->
<action name="rand" class="com.mxl.rand.RandomAction">
<result type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
</result>
</action>

6、生成的驗證碼圖片演示(實現(xiàn)的6位數(shù)字的驗證碼)
說明:
如果想修改驗證碼生成的個數(shù),需要修改以下幾個地方:
第一點:
int width=85, height=20; int width=85, height=20;
第二點: for (int i=0;i<6;i++) for (int i=0;i<6;i++)
數(shù)字6,修改成你想生成的位數(shù)就可以了~

分享:JSP自定義標簽Taglib實現(xiàn)過程重點總結(jié)
Taglib指令,其實就是定義一個標簽庫以及自定義標簽的前綴,其中的具體的實現(xiàn)方式,我們不過多介紹,我們給大家從宏觀的角度以及解決其中的疑難點,感興趣的朋友可以了解下,或許對你學習jsp有所幫助

來源:模板無憂//所屬分類:JSP教程/更新時間:2013-04-22
相關JSP教程